Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 17 | // clang-format off |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 18 | #include "../Macros.h" |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 19 | // clang-format on |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 20 | |
| 21 | #include "TouchInputMapper.h" |
| 22 | |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 23 | #include <ftl/enum.h> |
| 24 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 25 | #include "CursorButtonAccumulator.h" |
| 26 | #include "CursorScrollAccumulator.h" |
| 27 | #include "TouchButtonAccumulator.h" |
| 28 | #include "TouchCursorInputMapperCommon.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // --- Constants --- |
| 33 | |
| 34 | // Maximum amount of latency to add to touch events while waiting for data from an |
| 35 | // external stylus. |
| 36 | static constexpr nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72); |
| 37 | |
| 38 | // Maximum amount of time to wait on touch data before pushing out new pressure data. |
| 39 | static constexpr nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20); |
| 40 | |
| 41 | // Artificial latency on synthetic events created from stylus data without corresponding touch |
| 42 | // data. |
| 43 | static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10); |
| 44 | |
HQ Liu | e6983c7 | 2022-04-19 22:14:56 +0000 | [diff] [blame] | 45 | // Minimum width between two pointers to determine a gesture as freeform gesture in mm |
| 46 | static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 47 | // --- Static Definitions --- |
| 48 | |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 49 | static const DisplayViewport kUninitializedViewport; |
| 50 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | template <typename T> |
| 52 | inline static void swap(T& a, T& b) { |
| 53 | T temp = a; |
| 54 | a = b; |
| 55 | b = temp; |
| 56 | } |
| 57 | |
| 58 | static float calculateCommonVector(float a, float b) { |
| 59 | if (a > 0 && b > 0) { |
| 60 | return a < b ? a : b; |
| 61 | } else if (a < 0 && b < 0) { |
| 62 | return a > b ? a : b; |
| 63 | } else { |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | inline static float distance(float x1, float y1, float x2, float y2) { |
| 69 | return hypotf(x1 - x2, y1 - y2); |
| 70 | } |
| 71 | |
| 72 | inline static int32_t signExtendNybble(int32_t value) { |
| 73 | return value >= 8 ? value - 16 : value; |
| 74 | } |
| 75 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 76 | // --- RawPointerData --- |
| 77 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 78 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 79 | float x = 0, y = 0; |
| 80 | uint32_t count = touchingIdBits.count(); |
| 81 | if (count) { |
| 82 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty();) { |
| 83 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 84 | const Pointer& pointer = pointerForId(id); |
| 85 | x += pointer.x; |
| 86 | y += pointer.y; |
| 87 | } |
| 88 | x /= count; |
| 89 | y /= count; |
| 90 | } |
| 91 | *outX = x; |
| 92 | *outY = y; |
| 93 | } |
| 94 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 95 | // --- TouchInputMapper --- |
| 96 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 97 | TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext) |
| 98 | : InputMapper(deviceContext), |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 99 | mTouchButtonAccumulator(deviceContext), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | mSource(0), |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 101 | mDeviceMode(DeviceMode::DISABLED), |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 102 | mDisplayWidth(-1), |
| 103 | mDisplayHeight(-1), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 104 | mPhysicalWidth(-1), |
| 105 | mPhysicalHeight(-1), |
| 106 | mPhysicalLeft(0), |
| 107 | mPhysicalTop(0), |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 108 | mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 109 | |
| 110 | TouchInputMapper::~TouchInputMapper() {} |
| 111 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 112 | uint32_t TouchInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 113 | return mSource; |
| 114 | } |
| 115 | |
| 116 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 117 | InputMapper::populateDeviceInfo(info); |
| 118 | |
Prabir Pradhan | edb0ba7 | 2022-10-04 15:44:11 +0000 | [diff] [blame] | 119 | if (mDeviceMode == DeviceMode::DISABLED) { |
| 120 | return; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 121 | } |
Prabir Pradhan | edb0ba7 | 2022-10-04 15:44:11 +0000 | [diff] [blame] | 122 | |
| 123 | info->addMotionRange(mOrientedRanges.x); |
| 124 | info->addMotionRange(mOrientedRanges.y); |
| 125 | info->addMotionRange(mOrientedRanges.pressure); |
| 126 | |
| 127 | if (mDeviceMode == DeviceMode::UNSCALED && mSource == AINPUT_SOURCE_TOUCHPAD) { |
| 128 | // Populate RELATIVE_X and RELATIVE_Y motion ranges for touchpad capture mode. |
| 129 | // |
| 130 | // RELATIVE_X and RELATIVE_Y motion ranges should be the largest possible relative |
| 131 | // motion, i.e. the hardware dimensions, as the finger could move completely across the |
| 132 | // touchpad in one sample cycle. |
| 133 | const InputDeviceInfo::MotionRange& x = mOrientedRanges.x; |
| 134 | const InputDeviceInfo::MotionRange& y = mOrientedRanges.y; |
| 135 | info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, mSource, -x.max, x.max, x.flat, x.fuzz, |
| 136 | x.resolution); |
| 137 | info->addMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, mSource, -y.max, y.max, y.flat, y.fuzz, |
| 138 | y.resolution); |
| 139 | } |
| 140 | |
| 141 | if (mOrientedRanges.size) { |
| 142 | info->addMotionRange(*mOrientedRanges.size); |
| 143 | } |
| 144 | |
| 145 | if (mOrientedRanges.touchMajor) { |
| 146 | info->addMotionRange(*mOrientedRanges.touchMajor); |
| 147 | info->addMotionRange(*mOrientedRanges.touchMinor); |
| 148 | } |
| 149 | |
| 150 | if (mOrientedRanges.toolMajor) { |
| 151 | info->addMotionRange(*mOrientedRanges.toolMajor); |
| 152 | info->addMotionRange(*mOrientedRanges.toolMinor); |
| 153 | } |
| 154 | |
| 155 | if (mOrientedRanges.orientation) { |
| 156 | info->addMotionRange(*mOrientedRanges.orientation); |
| 157 | } |
| 158 | |
| 159 | if (mOrientedRanges.distance) { |
| 160 | info->addMotionRange(*mOrientedRanges.distance); |
| 161 | } |
| 162 | |
| 163 | if (mOrientedRanges.tilt) { |
| 164 | info->addMotionRange(*mOrientedRanges.tilt); |
| 165 | } |
| 166 | |
| 167 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 168 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
| 169 | } |
| 170 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 171 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
| 172 | } |
| 173 | if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) { |
| 174 | const InputDeviceInfo::MotionRange& x = mOrientedRanges.x; |
| 175 | const InputDeviceInfo::MotionRange& y = mOrientedRanges.y; |
| 176 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat, x.fuzz, |
| 177 | x.resolution); |
| 178 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat, y.fuzz, |
| 179 | y.resolution); |
| 180 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat, x.fuzz, |
| 181 | x.resolution); |
| 182 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat, y.fuzz, |
| 183 | y.resolution); |
| 184 | } |
| 185 | info->setButtonUnderPad(mParameters.hasButtonUnderPad); |
Prabir Pradhan | 167c270 | 2022-09-14 00:37:24 +0000 | [diff] [blame] | 186 | info->setSupportsUsi(mParameters.supportsUsi); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void TouchInputMapper::dump(std::string& dump) { |
Chris Ye | a03dd23 | 2020-09-08 19:21:09 -0700 | [diff] [blame] | 190 | dump += StringPrintf(INDENT2 "Touch Input Mapper (mode - %s):\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 191 | ftl::enum_string(mDeviceMode).c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 192 | dumpParameters(dump); |
| 193 | dumpVirtualKeys(dump); |
| 194 | dumpRawPointerAxes(dump); |
| 195 | dumpCalibration(dump); |
| 196 | dumpAffineTransformation(dump); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 197 | dumpDisplay(dump); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 198 | |
| 199 | dump += StringPrintf(INDENT3 "Translation and Scaling Factors:\n"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 200 | dump += StringPrintf(INDENT4 "XScale: %0.3f\n", mXScale); |
| 201 | dump += StringPrintf(INDENT4 "YScale: %0.3f\n", mYScale); |
| 202 | dump += StringPrintf(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 203 | dump += StringPrintf(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 204 | dump += StringPrintf(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
| 205 | dump += StringPrintf(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 206 | dump += StringPrintf(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
| 207 | dump += StringPrintf(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 208 | dump += StringPrintf(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
| 209 | dump += StringPrintf(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 210 | dump += StringPrintf(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 211 | dump += StringPrintf(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 212 | dump += StringPrintf(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 213 | dump += StringPrintf(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
| 214 | |
| 215 | dump += StringPrintf(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState); |
| 216 | dump += StringPrintf(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 217 | mLastRawState.rawPointerData.pointerCount); |
| 218 | for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) { |
| 219 | const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i]; |
| 220 | dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 221 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
| 222 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 223 | "toolType=%d, isHovering=%s\n", |
| 224 | i, pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 225 | pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, |
| 226 | pointer.toolMinor, pointer.orientation, pointer.tiltX, pointer.tiltY, |
| 227 | pointer.distance, pointer.toolType, toString(pointer.isHovering)); |
| 228 | } |
| 229 | |
| 230 | dump += StringPrintf(INDENT3 "Last Cooked Button State: 0x%08x\n", |
| 231 | mLastCookedState.buttonState); |
| 232 | dump += StringPrintf(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 233 | mLastCookedState.cookedPointerData.pointerCount); |
| 234 | for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) { |
| 235 | const PointerProperties& pointerProperties = |
| 236 | mLastCookedState.cookedPointerData.pointerProperties[i]; |
| 237 | const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i]; |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 238 | dump += StringPrintf(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, dx=%0.3f, dy=%0.3f, " |
| 239 | "pressure=%0.3f, touchMajor=%0.3f, touchMinor=%0.3f, " |
| 240 | "toolMajor=%0.3f, toolMinor=%0.3f, " |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 241 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 242 | "toolType=%d, isHovering=%s\n", |
| 243 | i, pointerProperties.id, pointerCoords.getX(), pointerCoords.getY(), |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 244 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 245 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 246 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 247 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 248 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 249 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 250 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 251 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
| 252 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
| 253 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 254 | pointerProperties.toolType, |
| 255 | toString(mLastCookedState.cookedPointerData.isHovering(i))); |
| 256 | } |
| 257 | |
| 258 | dump += INDENT3 "Stylus Fusion:\n"; |
| 259 | dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n", |
| 260 | toString(mExternalStylusConnected)); |
| 261 | dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId); |
| 262 | dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n", |
| 263 | mExternalStylusFusionTimeout); |
| 264 | dump += INDENT3 "External Stylus State:\n"; |
| 265 | dumpStylusState(dump, mExternalStylusState); |
| 266 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 267 | if (mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 268 | dump += StringPrintf(INDENT3 "Pointer Gesture Detector:\n"); |
| 269 | dump += StringPrintf(INDENT4 "XMovementScale: %0.3f\n", mPointerXMovementScale); |
| 270 | dump += StringPrintf(INDENT4 "YMovementScale: %0.3f\n", mPointerYMovementScale); |
| 271 | dump += StringPrintf(INDENT4 "XZoomScale: %0.3f\n", mPointerXZoomScale); |
| 272 | dump += StringPrintf(INDENT4 "YZoomScale: %0.3f\n", mPointerYZoomScale); |
| 273 | dump += StringPrintf(INDENT4 "MaxSwipeWidth: %f\n", mPointerGestureMaxSwipeWidth); |
| 274 | } |
| 275 | } |
| 276 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 277 | std::list<NotifyArgs> TouchInputMapper::configure(nsecs_t when, |
| 278 | const InputReaderConfiguration* config, |
| 279 | uint32_t changes) { |
| 280 | std::list<NotifyArgs> out = InputMapper::configure(when, config, changes); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 281 | |
| 282 | mConfig = *config; |
| 283 | |
| 284 | if (!changes) { // first time only |
| 285 | // Configure basic parameters. |
| 286 | configureParameters(); |
| 287 | |
| 288 | // Configure common accumulators. |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 289 | mCursorScrollAccumulator.configure(getDeviceContext()); |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 290 | mTouchButtonAccumulator.configure(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 291 | |
| 292 | // Configure absolute axis information. |
| 293 | configureRawPointerAxes(); |
| 294 | |
| 295 | // Prepare input device calibration. |
| 296 | parseCalibration(); |
| 297 | resolveCalibration(); |
| 298 | } |
| 299 | |
| 300 | if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) { |
| 301 | // Update location calibration to reflect current settings |
| 302 | updateAffineTransformation(); |
| 303 | } |
| 304 | |
| 305 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 306 | // Update pointer speed. |
| 307 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 308 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 309 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 310 | } |
| 311 | |
| 312 | bool resetNeeded = false; |
| 313 | if (!changes || |
| 314 | (changes & |
| 315 | (InputReaderConfiguration::CHANGE_DISPLAY_INFO | |
Nathaniel R. Lewis | d566533 | 2018-02-22 13:31:42 -0800 | [diff] [blame] | 316 | InputReaderConfiguration::CHANGE_POINTER_CAPTURE | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 317 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT | |
| 318 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES | |
| 319 | InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) { |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 320 | // Configure device sources, display dimensions, orientation and |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 321 | // scaling factors. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 322 | configureInputDevice(when, &resetNeeded); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | if (changes && resetNeeded) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 326 | out += reset(when); |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 327 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 328 | // Send reset, unless this is the first time the device has been configured, |
| 329 | // in which case the reader will call reset itself after all mappers are ready. |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 330 | out.emplace_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId())); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 331 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 332 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void TouchInputMapper::resolveExternalStylusPresence() { |
| 336 | std::vector<InputDeviceInfo> devices; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 337 | getContext()->getExternalStylusDevices(devices); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 338 | mExternalStylusConnected = !devices.empty(); |
| 339 | |
| 340 | if (!mExternalStylusConnected) { |
| 341 | resetExternalStylus(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void TouchInputMapper::configureParameters() { |
| 346 | // Use the pointer presentation mode for devices that do not support distinct |
| 347 | // multitouch. The spot-based presentation relies on being able to accurately |
| 348 | // locate two or more fingers on the touch pad. |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 349 | mParameters.gestureMode = getDeviceContext().hasInputProperty(INPUT_PROP_SEMI_MT) |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 350 | ? Parameters::GestureMode::SINGLE_TOUCH |
| 351 | : Parameters::GestureMode::MULTI_TOUCH; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 352 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 353 | std::string gestureModeString; |
| 354 | if (getDeviceContext().getConfiguration().tryGetProperty("touch.gestureMode", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 355 | gestureModeString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 356 | if (gestureModeString == "single-touch") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 357 | mParameters.gestureMode = Parameters::GestureMode::SINGLE_TOUCH; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 358 | } else if (gestureModeString == "multi-touch") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 359 | mParameters.gestureMode = Parameters::GestureMode::MULTI_TOUCH; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 360 | } else if (gestureModeString != "default") { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 361 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 365 | if (getDeviceContext().hasInputProperty(INPUT_PROP_DIRECT)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 366 | // The device is a touch screen. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 367 | mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 368 | } else if (getDeviceContext().hasInputProperty(INPUT_PROP_POINTER)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 369 | // The device is a pointing device like a track pad. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 370 | mParameters.deviceType = Parameters::DeviceType::POINTER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 371 | } else { |
| 372 | // The device is a touch pad of unknown purpose. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 373 | mParameters.deviceType = Parameters::DeviceType::POINTER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 376 | mParameters.hasButtonUnderPad = getDeviceContext().hasInputProperty(INPUT_PROP_BUTTONPAD); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 377 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 378 | std::string deviceTypeString; |
| 379 | if (getDeviceContext().getConfiguration().tryGetProperty("touch.deviceType", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 380 | deviceTypeString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 381 | if (deviceTypeString == "touchScreen") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 382 | mParameters.deviceType = Parameters::DeviceType::TOUCH_SCREEN; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 383 | } else if (deviceTypeString == "touchNavigation") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 384 | mParameters.deviceType = Parameters::DeviceType::TOUCH_NAVIGATION; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 385 | } else if (deviceTypeString == "pointer") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 386 | mParameters.deviceType = Parameters::DeviceType::POINTER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 387 | } else if (deviceTypeString != "default") { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 388 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 392 | mParameters.orientationAware = mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 393 | getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 394 | mParameters.orientationAware); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 395 | |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 396 | mParameters.orientation = Parameters::Orientation::ORIENTATION_0; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 397 | std::string orientationString; |
| 398 | if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation", |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 399 | orientationString)) { |
| 400 | if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) { |
| 401 | ALOGW("The configuration 'touch.orientation' is only supported for touchscreens."); |
| 402 | } else if (orientationString == "ORIENTATION_90") { |
| 403 | mParameters.orientation = Parameters::Orientation::ORIENTATION_90; |
| 404 | } else if (orientationString == "ORIENTATION_180") { |
| 405 | mParameters.orientation = Parameters::Orientation::ORIENTATION_180; |
| 406 | } else if (orientationString == "ORIENTATION_270") { |
| 407 | mParameters.orientation = Parameters::Orientation::ORIENTATION_270; |
| 408 | } else if (orientationString != "ORIENTATION_0") { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 409 | ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str()); |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 413 | mParameters.hasAssociatedDisplay = false; |
| 414 | mParameters.associatedDisplayIsExternal = false; |
| 415 | if (mParameters.orientationAware || |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 416 | mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN || |
| 417 | mParameters.deviceType == Parameters::DeviceType::POINTER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 418 | mParameters.hasAssociatedDisplay = true; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 419 | if (mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 420 | mParameters.associatedDisplayIsExternal = getDeviceContext().isExternal(); |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 421 | std::string uniqueDisplayId; |
| 422 | getDeviceContext().getConfiguration().tryGetProperty("touch.displayId", |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 423 | uniqueDisplayId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 424 | mParameters.uniqueDisplayId = uniqueDisplayId.c_str(); |
| 425 | } |
| 426 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 427 | if (getDeviceContext().getAssociatedDisplayPort()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 428 | mParameters.hasAssociatedDisplay = true; |
| 429 | } |
| 430 | |
| 431 | // Initial downs on external touch devices should wake the device. |
| 432 | // Normally we don't do this for internal touch screens to prevent them from waking |
| 433 | // up in your pocket but you can enable it using the input device configuration. |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 434 | mParameters.wake = getDeviceContext().isExternal(); |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 435 | getDeviceContext().getConfiguration().tryGetProperty("touch.wake", mParameters.wake); |
Prabir Pradhan | 167c270 | 2022-09-14 00:37:24 +0000 | [diff] [blame] | 436 | |
| 437 | mParameters.supportsUsi = false; |
| 438 | getDeviceContext().getConfiguration().tryGetProperty("touch.supportsUsi", |
| 439 | mParameters.supportsUsi); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void TouchInputMapper::dumpParameters(std::string& dump) { |
| 443 | dump += INDENT3 "Parameters:\n"; |
| 444 | |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 445 | dump += INDENT4 "GestureMode: " + ftl::enum_string(mParameters.gestureMode) + "\n"; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 446 | |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 447 | dump += INDENT4 "DeviceType: " + ftl::enum_string(mParameters.deviceType) + "\n"; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 448 | |
| 449 | dump += StringPrintf(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s, " |
| 450 | "displayId='%s'\n", |
| 451 | toString(mParameters.hasAssociatedDisplay), |
| 452 | toString(mParameters.associatedDisplayIsExternal), |
| 453 | mParameters.uniqueDisplayId.c_str()); |
| 454 | dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware)); |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 455 | dump += INDENT4 "Orientation: " + ftl::enum_string(mParameters.orientation) + "\n"; |
Prabir Pradhan | 167c270 | 2022-09-14 00:37:24 +0000 | [diff] [blame] | 456 | dump += StringPrintf(INDENT4 "SupportsUsi: %s\n", toString(mParameters.supportsUsi)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void TouchInputMapper::configureRawPointerAxes() { |
| 460 | mRawPointerAxes.clear(); |
| 461 | } |
| 462 | |
| 463 | void TouchInputMapper::dumpRawPointerAxes(std::string& dump) { |
| 464 | dump += INDENT3 "Raw Touch Axes:\n"; |
| 465 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 466 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 467 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 468 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 469 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 470 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 471 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 472 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 473 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
| 474 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 475 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
| 476 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 477 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
| 478 | } |
| 479 | |
| 480 | bool TouchInputMapper::hasExternalStylus() const { |
| 481 | return mExternalStylusConnected; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Determine which DisplayViewport to use. |
Arthur Hung | 6d5b4b2 | 2022-01-21 07:21:10 +0000 | [diff] [blame] | 486 | * 1. If a device has associated display, get the matching viewport. |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 487 | * 2. Always use the suggested viewport from WindowManagerService for pointers. |
Arthur Hung | 6d5b4b2 | 2022-01-21 07:21:10 +0000 | [diff] [blame] | 488 | * 3. Get the matching viewport by either unique id in idc file or by the display type |
| 489 | * (internal or external). |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 490 | * 4. Otherwise, use a non-display viewport. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 491 | */ |
| 492 | std::optional<DisplayViewport> TouchInputMapper::findViewport() { |
Nathaniel R. Lewis | d566533 | 2018-02-22 13:31:42 -0800 | [diff] [blame] | 493 | if (mParameters.hasAssociatedDisplay && mDeviceMode != DeviceMode::UNSCALED) { |
Arthur Hung | 6d5b4b2 | 2022-01-21 07:21:10 +0000 | [diff] [blame] | 494 | if (getDeviceContext().getAssociatedViewport()) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 495 | return getDeviceContext().getAssociatedViewport(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 498 | const std::optional<std::string> associatedDisplayUniqueId = |
| 499 | getDeviceContext().getAssociatedDisplayUniqueId(); |
| 500 | if (associatedDisplayUniqueId) { |
| 501 | return getDeviceContext().getAssociatedViewport(); |
| 502 | } |
| 503 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 504 | if (mDeviceMode == DeviceMode::POINTER) { |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 505 | std::optional<DisplayViewport> viewport = |
| 506 | mConfig.getDisplayViewportById(mConfig.defaultPointerDisplayId); |
| 507 | if (viewport) { |
| 508 | return viewport; |
| 509 | } else { |
| 510 | ALOGW("Can't find designated display viewport with ID %" PRId32 " for pointers.", |
| 511 | mConfig.defaultPointerDisplayId); |
| 512 | } |
| 513 | } |
| 514 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 515 | // Check if uniqueDisplayId is specified in idc file. |
| 516 | if (!mParameters.uniqueDisplayId.empty()) { |
| 517 | return mConfig.getDisplayViewportByUniqueId(mParameters.uniqueDisplayId); |
| 518 | } |
| 519 | |
| 520 | ViewportType viewportTypeToUse; |
| 521 | if (mParameters.associatedDisplayIsExternal) { |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 522 | viewportTypeToUse = ViewportType::EXTERNAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 523 | } else { |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 524 | viewportTypeToUse = ViewportType::INTERNAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | std::optional<DisplayViewport> viewport = |
| 528 | mConfig.getDisplayViewportByType(viewportTypeToUse); |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 529 | if (!viewport && viewportTypeToUse == ViewportType::EXTERNAL) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 530 | ALOGW("Input device %s should be associated with external display, " |
| 531 | "fallback to internal one for the external viewport is not found.", |
| 532 | getDeviceName().c_str()); |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 533 | viewport = mConfig.getDisplayViewportByType(ViewportType::INTERNAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return viewport; |
| 537 | } |
| 538 | |
| 539 | // No associated display, return a non-display viewport. |
| 540 | DisplayViewport newViewport; |
| 541 | // Raw width and height in the natural orientation. |
| 542 | int32_t rawWidth = mRawPointerAxes.getRawWidth(); |
| 543 | int32_t rawHeight = mRawPointerAxes.getRawHeight(); |
| 544 | newViewport.setNonDisplayViewport(rawWidth, rawHeight); |
| 545 | return std::make_optional(newViewport); |
| 546 | } |
| 547 | |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 548 | int32_t TouchInputMapper::clampResolution(const char* axisName, int32_t resolution) const { |
| 549 | if (resolution < 0) { |
| 550 | ALOGE("Invalid %s resolution %" PRId32 " for device %s", axisName, resolution, |
| 551 | getDeviceName().c_str()); |
| 552 | return 0; |
| 553 | } |
| 554 | return resolution; |
| 555 | } |
| 556 | |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 557 | void TouchInputMapper::initializeSizeRanges() { |
| 558 | if (mCalibration.sizeCalibration == Calibration::SizeCalibration::NONE) { |
| 559 | mSizeScale = 0.0f; |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | // Size of diagonal axis. |
| 564 | const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight); |
| 565 | |
| 566 | // Size factors. |
| 567 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 568 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 569 | } else if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 570 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 571 | } else { |
| 572 | mSizeScale = 0.0f; |
| 573 | } |
| 574 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 575 | mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{ |
| 576 | .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR, |
| 577 | .source = mSource, |
| 578 | .min = 0, |
| 579 | .max = diagonalSize, |
| 580 | .flat = 0, |
| 581 | .fuzz = 0, |
| 582 | .resolution = 0, |
| 583 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 584 | |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 585 | if (mRawPointerAxes.touchMajor.valid) { |
| 586 | mRawPointerAxes.touchMajor.resolution = |
| 587 | clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution); |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 588 | mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 589 | } |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 590 | |
| 591 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 592 | mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 593 | if (mRawPointerAxes.touchMinor.valid) { |
| 594 | mRawPointerAxes.touchMinor.resolution = |
| 595 | clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution); |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 596 | mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 597 | } |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 598 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 599 | mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{ |
| 600 | .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR, |
| 601 | .source = mSource, |
| 602 | .min = 0, |
| 603 | .max = diagonalSize, |
| 604 | .flat = 0, |
| 605 | .fuzz = 0, |
| 606 | .resolution = 0, |
| 607 | }; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 608 | if (mRawPointerAxes.toolMajor.valid) { |
| 609 | mRawPointerAxes.toolMajor.resolution = |
| 610 | clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution); |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 611 | mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 612 | } |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 613 | |
| 614 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 615 | mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 616 | if (mRawPointerAxes.toolMinor.valid) { |
| 617 | mRawPointerAxes.toolMinor.resolution = |
| 618 | clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution); |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 619 | mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) { |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 623 | mOrientedRanges.touchMajor->resolution *= mGeometricScale; |
| 624 | mOrientedRanges.touchMinor->resolution *= mGeometricScale; |
| 625 | mOrientedRanges.toolMajor->resolution *= mGeometricScale; |
| 626 | mOrientedRanges.toolMinor->resolution *= mGeometricScale; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 627 | } else { |
| 628 | // Support for other calibrations can be added here. |
| 629 | ALOGW("%s calibration is not supported for size ranges at the moment. " |
| 630 | "Using raw resolution instead", |
| 631 | ftl::enum_string(mCalibration.sizeCalibration).c_str()); |
| 632 | } |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 633 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 634 | mOrientedRanges.size = InputDeviceInfo::MotionRange{ |
| 635 | .axis = AMOTION_EVENT_AXIS_SIZE, |
| 636 | .source = mSource, |
| 637 | .min = 0, |
| 638 | .max = 1.0, |
| 639 | .flat = 0, |
| 640 | .fuzz = 0, |
| 641 | .resolution = 0, |
| 642 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void TouchInputMapper::initializeOrientedRanges() { |
| 646 | // Configure X and Y factors. |
| 647 | mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth(); |
| 648 | mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight(); |
| 649 | mXPrecision = 1.0f / mXScale; |
| 650 | mYPrecision = 1.0f / mYScale; |
| 651 | |
| 652 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
| 653 | mOrientedRanges.x.source = mSource; |
| 654 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
| 655 | mOrientedRanges.y.source = mSource; |
| 656 | |
| 657 | // Scale factor for terms that are not oriented in a particular axis. |
| 658 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 659 | // by choosing an average. |
| 660 | mGeometricScale = avg(mXScale, mYScale); |
| 661 | |
| 662 | initializeSizeRanges(); |
| 663 | |
| 664 | // Pressure factors. |
| 665 | mPressureScale = 0; |
| 666 | float pressureMax = 1.0; |
| 667 | if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL || |
| 668 | mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) { |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 669 | if (mCalibration.pressureScale) { |
| 670 | mPressureScale = *mCalibration.pressureScale; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 671 | pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue; |
| 672 | } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) { |
| 673 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
| 674 | } |
| 675 | } |
| 676 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 677 | mOrientedRanges.pressure = InputDeviceInfo::MotionRange{ |
| 678 | .axis = AMOTION_EVENT_AXIS_PRESSURE, |
| 679 | .source = mSource, |
| 680 | .min = 0, |
| 681 | .max = pressureMax, |
| 682 | .flat = 0, |
| 683 | .fuzz = 0, |
| 684 | .resolution = 0, |
| 685 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 686 | |
| 687 | // Tilt |
| 688 | mTiltXCenter = 0; |
| 689 | mTiltXScale = 0; |
| 690 | mTiltYCenter = 0; |
| 691 | mTiltYScale = 0; |
| 692 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 693 | if (mHaveTilt) { |
| 694 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, mRawPointerAxes.tiltX.maxValue); |
| 695 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, mRawPointerAxes.tiltY.maxValue); |
| 696 | mTiltXScale = M_PI / 180; |
| 697 | mTiltYScale = M_PI / 180; |
| 698 | |
| 699 | if (mRawPointerAxes.tiltX.resolution) { |
| 700 | mTiltXScale = 1.0 / mRawPointerAxes.tiltX.resolution; |
| 701 | } |
| 702 | if (mRawPointerAxes.tiltY.resolution) { |
| 703 | mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution; |
| 704 | } |
| 705 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 706 | mOrientedRanges.tilt = InputDeviceInfo::MotionRange{ |
| 707 | .axis = AMOTION_EVENT_AXIS_TILT, |
| 708 | .source = mSource, |
| 709 | .min = 0, |
| 710 | .max = M_PI_2, |
| 711 | .flat = 0, |
| 712 | .fuzz = 0, |
| 713 | .resolution = 0, |
| 714 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | // Orientation |
| 718 | mOrientationScale = 0; |
| 719 | if (mHaveTilt) { |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 720 | mOrientedRanges.orientation = InputDeviceInfo::MotionRange{ |
| 721 | .axis = AMOTION_EVENT_AXIS_ORIENTATION, |
| 722 | .source = mSource, |
| 723 | .min = -M_PI, |
| 724 | .max = M_PI, |
| 725 | .flat = 0, |
| 726 | .fuzz = 0, |
| 727 | .resolution = 0, |
| 728 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 729 | |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 730 | } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) { |
| 731 | if (mCalibration.orientationCalibration == |
| 732 | Calibration::OrientationCalibration::INTERPOLATED) { |
| 733 | if (mRawPointerAxes.orientation.valid) { |
| 734 | if (mRawPointerAxes.orientation.maxValue > 0) { |
| 735 | mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue; |
| 736 | } else if (mRawPointerAxes.orientation.minValue < 0) { |
| 737 | mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue; |
| 738 | } else { |
| 739 | mOrientationScale = 0; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 744 | mOrientedRanges.orientation = InputDeviceInfo::MotionRange{ |
| 745 | .axis = AMOTION_EVENT_AXIS_ORIENTATION, |
| 746 | .source = mSource, |
| 747 | .min = -M_PI_2, |
| 748 | .max = M_PI_2, |
| 749 | .flat = 0, |
| 750 | .fuzz = 0, |
| 751 | .resolution = 0, |
| 752 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // Distance |
| 756 | mDistanceScale = 0; |
| 757 | if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) { |
| 758 | if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) { |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 759 | mDistanceScale = mCalibration.distanceScale.value_or(1.0f); |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 762 | mOrientedRanges.distance = InputDeviceInfo::MotionRange{ |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 763 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 764 | .axis = AMOTION_EVENT_AXIS_DISTANCE, |
| 765 | .source = mSource, |
| 766 | .min = mRawPointerAxes.distance.minValue * mDistanceScale, |
| 767 | .max = mRawPointerAxes.distance.maxValue * mDistanceScale, |
| 768 | .flat = 0, |
| 769 | .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale, |
| 770 | .resolution = 0, |
| 771 | }; |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // Compute oriented precision, scales and ranges. |
| 775 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 776 | // unit less than the total width or height of the display. |
| 777 | switch (mInputDeviceOrientation) { |
| 778 | case DISPLAY_ORIENTATION_90: |
| 779 | case DISPLAY_ORIENTATION_270: |
| 780 | mOrientedXPrecision = mYPrecision; |
| 781 | mOrientedYPrecision = mXPrecision; |
| 782 | |
| 783 | mOrientedRanges.x.min = 0; |
| 784 | mOrientedRanges.x.max = mDisplayHeight - 1; |
| 785 | mOrientedRanges.x.flat = 0; |
| 786 | mOrientedRanges.x.fuzz = 0; |
| 787 | mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale; |
| 788 | |
| 789 | mOrientedRanges.y.min = 0; |
| 790 | mOrientedRanges.y.max = mDisplayWidth - 1; |
| 791 | mOrientedRanges.y.flat = 0; |
| 792 | mOrientedRanges.y.fuzz = 0; |
| 793 | mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale; |
| 794 | break; |
| 795 | |
| 796 | default: |
| 797 | mOrientedXPrecision = mXPrecision; |
| 798 | mOrientedYPrecision = mYPrecision; |
| 799 | |
| 800 | mOrientedRanges.x.min = 0; |
| 801 | mOrientedRanges.x.max = mDisplayWidth - 1; |
| 802 | mOrientedRanges.x.flat = 0; |
| 803 | mOrientedRanges.x.fuzz = 0; |
| 804 | mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale; |
| 805 | |
| 806 | mOrientedRanges.y.min = 0; |
| 807 | mOrientedRanges.y.max = mDisplayHeight - 1; |
| 808 | mOrientedRanges.y.flat = 0; |
| 809 | mOrientedRanges.y.fuzz = 0; |
| 810 | mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale; |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 815 | void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded) { |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 816 | const DeviceMode oldDeviceMode = mDeviceMode; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 817 | |
| 818 | resolveExternalStylusPresence(); |
| 819 | |
| 820 | // Determine device mode. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 821 | if (mParameters.deviceType == Parameters::DeviceType::POINTER && |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 822 | mConfig.pointerGesturesEnabled && !mConfig.pointerCaptureRequest.enable) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 823 | mSource = AINPUT_SOURCE_MOUSE; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 824 | mDeviceMode = DeviceMode::POINTER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 825 | if (hasStylus()) { |
| 826 | mSource |= AINPUT_SOURCE_STYLUS; |
| 827 | } |
Garfield Tan | c734e4f | 2021-01-15 20:01:39 -0800 | [diff] [blame] | 828 | } else if (isTouchScreen()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 829 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 830 | mDeviceMode = DeviceMode::DIRECT; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 831 | if (hasStylus()) { |
| 832 | mSource |= AINPUT_SOURCE_STYLUS; |
| 833 | } |
| 834 | if (hasExternalStylus()) { |
| 835 | mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS; |
| 836 | } |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 837 | } else if (mParameters.deviceType == Parameters::DeviceType::TOUCH_NAVIGATION) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 838 | mSource = AINPUT_SOURCE_TOUCH_NAVIGATION; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 839 | mDeviceMode = DeviceMode::NAVIGATION; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 840 | } else { |
| 841 | mSource = AINPUT_SOURCE_TOUCHPAD; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 842 | mDeviceMode = DeviceMode::UNSCALED; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 845 | const std::optional<DisplayViewport> newViewportOpt = findViewport(); |
| 846 | |
| 847 | // Ensure the device is valid and can be used. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 848 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
| 849 | ALOGW("Touch device '%s' did not report support for X or Y axis! " |
| 850 | "The device will be inoperable.", |
| 851 | getDeviceName().c_str()); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 852 | mDeviceMode = DeviceMode::DISABLED; |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 853 | } else if (!newViewportOpt) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 854 | ALOGI("Touch device '%s' could not query the properties of its associated " |
| 855 | "display. The device will be inoperable until the display size " |
| 856 | "becomes available.", |
| 857 | getDeviceName().c_str()); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 858 | mDeviceMode = DeviceMode::DISABLED; |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 859 | } else if (!newViewportOpt->isActive) { |
Siarhei Vishniakou | 6f77846 | 2020-12-09 23:39:07 +0000 | [diff] [blame] | 860 | ALOGI("Disabling %s (device %i) because the associated viewport is not active", |
| 861 | getDeviceName().c_str(), getDeviceId()); |
| 862 | mDeviceMode = DeviceMode::DISABLED; |
Siarhei Vishniakou | 6f77846 | 2020-12-09 23:39:07 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 865 | // Raw width and height in the natural orientation. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 866 | const int32_t rawWidth = mRawPointerAxes.getRawWidth(); |
| 867 | const int32_t rawHeight = mRawPointerAxes.getRawHeight(); |
HQ Liu | e6983c7 | 2022-04-19 22:14:56 +0000 | [diff] [blame] | 868 | const int32_t rawXResolution = mRawPointerAxes.x.resolution; |
| 869 | const int32_t rawYResolution = mRawPointerAxes.y.resolution; |
| 870 | // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0. |
| 871 | const float rawMeanResolution = |
| 872 | (rawXResolution > 0 && rawYResolution > 0) ? (rawXResolution + rawYResolution) / 2 : 0; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 873 | |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 874 | const DisplayViewport& newViewport = newViewportOpt.value_or(kUninitializedViewport); |
| 875 | const bool viewportChanged = mViewport != newViewport; |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 876 | bool skipViewportUpdate = false; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 877 | if (viewportChanged) { |
Prabir Pradhan | c0bdeef | 2022-08-05 22:32:11 +0000 | [diff] [blame] | 878 | const bool viewportOrientationChanged = mViewport.orientation != newViewport.orientation; |
| 879 | const bool viewportDisplayIdChanged = mViewport.displayId != newViewport.displayId; |
| 880 | mViewport = newViewport; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 881 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 882 | if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 883 | // Convert rotated viewport to the natural orientation. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 884 | int32_t naturalPhysicalWidth, naturalPhysicalHeight; |
| 885 | int32_t naturalPhysicalLeft, naturalPhysicalTop; |
| 886 | int32_t naturalDeviceWidth, naturalDeviceHeight; |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 887 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 888 | // Apply the inverse of the input device orientation so that the input device is |
| 889 | // configured in the same orientation as the viewport. The input device orientation will |
| 890 | // be re-applied by mInputDeviceOrientation. |
| 891 | const int32_t naturalDeviceOrientation = |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 892 | (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4; |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 893 | switch (naturalDeviceOrientation) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 894 | case DISPLAY_ORIENTATION_90: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 895 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 896 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 897 | naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 898 | naturalPhysicalTop = mViewport.physicalLeft; |
| 899 | naturalDeviceWidth = mViewport.deviceHeight; |
| 900 | naturalDeviceHeight = mViewport.deviceWidth; |
| 901 | break; |
| 902 | case DISPLAY_ORIENTATION_180: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 903 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 904 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 905 | naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight; |
| 906 | naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom; |
| 907 | naturalDeviceWidth = mViewport.deviceWidth; |
| 908 | naturalDeviceHeight = mViewport.deviceHeight; |
| 909 | break; |
| 910 | case DISPLAY_ORIENTATION_270: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 911 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 912 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
| 913 | naturalPhysicalLeft = mViewport.physicalTop; |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 914 | naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 915 | naturalDeviceWidth = mViewport.deviceHeight; |
| 916 | naturalDeviceHeight = mViewport.deviceWidth; |
| 917 | break; |
| 918 | case DISPLAY_ORIENTATION_0: |
| 919 | default: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 920 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 921 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 922 | naturalPhysicalLeft = mViewport.physicalLeft; |
| 923 | naturalPhysicalTop = mViewport.physicalTop; |
| 924 | naturalDeviceWidth = mViewport.deviceWidth; |
| 925 | naturalDeviceHeight = mViewport.deviceHeight; |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) { |
| 930 | ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str()); |
| 931 | naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight; |
| 932 | naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth; |
| 933 | } |
| 934 | |
| 935 | mPhysicalWidth = naturalPhysicalWidth; |
| 936 | mPhysicalHeight = naturalPhysicalHeight; |
| 937 | mPhysicalLeft = naturalPhysicalLeft; |
| 938 | mPhysicalTop = naturalPhysicalTop; |
| 939 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 940 | const int32_t oldDisplayWidth = mDisplayWidth; |
| 941 | const int32_t oldDisplayHeight = mDisplayHeight; |
| 942 | mDisplayWidth = naturalDeviceWidth; |
| 943 | mDisplayHeight = naturalDeviceHeight; |
Prabir Pradhan | 5632d62 | 2021-09-06 07:57:20 -0700 | [diff] [blame] | 944 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 945 | // InputReader works in the un-rotated display coordinate space, so we don't need to do |
| 946 | // anything if the device is already orientation-aware. If the device is not |
| 947 | // orientation-aware, then we need to apply the inverse rotation of the display so that |
| 948 | // when the display rotation is applied later as a part of the per-window transform, we |
| 949 | // get the expected screen coordinates. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 950 | mInputDeviceOrientation = mParameters.orientationAware |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 951 | ? DISPLAY_ORIENTATION_0 |
| 952 | : getInverseRotation(mViewport.orientation); |
| 953 | // For orientation-aware devices that work in the un-rotated coordinate space, the |
| 954 | // viewport update should be skipped if it is only a change in the orientation. |
Prabir Pradhan | 3e5ec70 | 2022-07-29 16:26:24 +0000 | [diff] [blame] | 955 | skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware && |
| 956 | mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight && |
| 957 | viewportOrientationChanged; |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 958 | |
| 959 | // Apply the input device orientation for the device. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 960 | mInputDeviceOrientation = |
| 961 | (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 962 | } else { |
| 963 | mPhysicalWidth = rawWidth; |
| 964 | mPhysicalHeight = rawHeight; |
| 965 | mPhysicalLeft = 0; |
| 966 | mPhysicalTop = 0; |
| 967 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 968 | mDisplayWidth = rawWidth; |
| 969 | mDisplayHeight = rawHeight; |
| 970 | mInputDeviceOrientation = DISPLAY_ORIENTATION_0; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 971 | } |
| 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 Pradhan | 59ecc3b | 2020-11-20 13:11:47 -0800 | [diff] [blame] | 980 | // Create pointer controller if needed, and keep it around if Pointer Capture is enabled to |
| 981 | // preserve the cursor position. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 982 | if (mDeviceMode == DeviceMode::POINTER || |
Prabir Pradhan | 59ecc3b | 2020-11-20 13:11:47 -0800 | [diff] [blame] | 983 | (mDeviceMode == DeviceMode::DIRECT && mConfig.showTouches) || |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 984 | (mParameters.deviceType == Parameters::DeviceType::POINTER && |
| 985 | mConfig.pointerCaptureRequest.enable)) { |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 986 | if (mPointerController == nullptr) { |
| 987 | mPointerController = getContext()->getPointerController(getDeviceId()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 988 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 989 | if (mConfig.pointerCaptureRequest.enable) { |
Prabir Pradhan | 59ecc3b | 2020-11-20 13:11:47 -0800 | [diff] [blame] | 990 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 991 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 992 | } else { |
lilinnan | def700b | 2022-06-17 19:32:01 +0800 | [diff] [blame] | 993 | if (mPointerController != nullptr && mDeviceMode == DeviceMode::DIRECT && |
| 994 | !mConfig.showTouches) { |
| 995 | mPointerController->clearSpots(); |
| 996 | } |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 997 | mPointerController.reset(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 1000 | if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1001 | ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, " |
| 1002 | "display id %d", |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1003 | getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight, |
| 1004 | mInputDeviceOrientation, mDeviceMode, mViewport.displayId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1005 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1006 | configureVirtualKeys(); |
| 1007 | |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 1008 | initializeOrientedRanges(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1009 | |
| 1010 | // Location |
| 1011 | updateAffineTransformation(); |
| 1012 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1013 | if (mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1014 | // Compute pointer gesture detection parameters. |
| 1015 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1016 | float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1017 | |
| 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 Liu | e6983c7 | 2022-04-19 22:14:56 +0000 | [diff] [blame] | 1034 | // 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | // Inform the dispatcher about the changes. |
| 1045 | *outResetNeeded = true; |
| 1046 | bumpGeneration(); |
| 1047 | } |
| 1048 | } |
| 1049 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1050 | void TouchInputMapper::dumpDisplay(std::string& dump) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1051 | dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str()); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1052 | dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth); |
| 1053 | dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1054 | dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth); |
| 1055 | dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight); |
| 1056 | dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft); |
| 1057 | dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1058 | dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void TouchInputMapper::configureVirtualKeys() { |
| 1062 | std::vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1063 | getDeviceContext().getVirtualKeyDefinitions(virtualKeyDefinitions); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1064 | |
| 1065 | mVirtualKeys.clear(); |
| 1066 | |
| 1067 | if (virtualKeyDefinitions.size() == 0) { |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 1072 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 1073 | int32_t touchScreenWidth = mRawPointerAxes.getRawWidth(); |
| 1074 | int32_t touchScreenHeight = mRawPointerAxes.getRawHeight(); |
| 1075 | |
| 1076 | for (const VirtualKeyDefinition& virtualKeyDefinition : virtualKeyDefinitions) { |
| 1077 | VirtualKey virtualKey; |
| 1078 | |
| 1079 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 1080 | int32_t keyCode; |
| 1081 | int32_t dummyKeyMetaState; |
| 1082 | uint32_t flags; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1083 | if (getDeviceContext().mapKey(virtualKey.scanCode, 0, 0, &keyCode, &dummyKeyMetaState, |
| 1084 | &flags)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1085 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", virtualKey.scanCode); |
| 1086 | continue; // drop the key |
| 1087 | } |
| 1088 | |
| 1089 | virtualKey.keyCode = keyCode; |
| 1090 | virtualKey.flags = flags; |
| 1091 | |
| 1092 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 1093 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 1094 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
| 1095 | |
| 1096 | virtualKey.hitLeft = |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1097 | (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth + |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1098 | touchScreenLeft; |
| 1099 | virtualKey.hitRight = |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1100 | (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth + |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1101 | touchScreenLeft; |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1102 | virtualKey.hitTop = |
| 1103 | (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight + |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1104 | touchScreenTop; |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1105 | virtualKey.hitBottom = |
| 1106 | (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight + |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1107 | touchScreenTop; |
| 1108 | mVirtualKeys.push_back(virtualKey); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | void TouchInputMapper::dumpVirtualKeys(std::string& dump) { |
| 1113 | if (!mVirtualKeys.empty()) { |
| 1114 | dump += INDENT3 "Virtual Keys:\n"; |
| 1115 | |
| 1116 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 1117 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 1118 | dump += StringPrintf(INDENT4 "%zu: scanCode=%d, keyCode=%d, " |
| 1119 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 1120 | i, virtualKey.scanCode, virtualKey.keyCode, virtualKey.hitLeft, |
| 1121 | virtualKey.hitRight, virtualKey.hitTop, virtualKey.hitBottom); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | void TouchInputMapper::parseCalibration() { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1127 | const PropertyMap& in = getDeviceContext().getConfiguration(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1128 | Calibration& out = mCalibration; |
| 1129 | |
| 1130 | // Size |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1131 | out.sizeCalibration = Calibration::SizeCalibration::DEFAULT; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1132 | std::string sizeCalibrationString; |
| 1133 | if (in.tryGetProperty("touch.size.calibration", sizeCalibrationString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1134 | if (sizeCalibrationString == "none") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1135 | out.sizeCalibration = Calibration::SizeCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1136 | } else if (sizeCalibrationString == "geometric") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1137 | out.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1138 | } else if (sizeCalibrationString == "diameter") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1139 | out.sizeCalibration = Calibration::SizeCalibration::DIAMETER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1140 | } else if (sizeCalibrationString == "box") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1141 | out.sizeCalibration = Calibration::SizeCalibration::BOX; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1142 | } else if (sizeCalibrationString == "area") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1143 | out.sizeCalibration = Calibration::SizeCalibration::AREA; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1144 | } else if (sizeCalibrationString != "default") { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1145 | ALOGW("Invalid value for touch.size.calibration: '%s'", sizeCalibrationString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1149 | float sizeScale; |
| 1150 | |
| 1151 | if (in.tryGetProperty("touch.size.scale", sizeScale)) { |
| 1152 | out.sizeScale = sizeScale; |
| 1153 | } |
| 1154 | float sizeBias; |
| 1155 | if (in.tryGetProperty("touch.size.bias", sizeBias)) { |
| 1156 | out.sizeBias = sizeBias; |
| 1157 | } |
| 1158 | bool sizeIsSummed; |
| 1159 | if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) { |
| 1160 | out.sizeIsSummed = sizeIsSummed; |
| 1161 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1162 | |
| 1163 | // Pressure |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1164 | out.pressureCalibration = Calibration::PressureCalibration::DEFAULT; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1165 | std::string pressureCalibrationString; |
| 1166 | if (in.tryGetProperty("touch.pressure.calibration", pressureCalibrationString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1167 | if (pressureCalibrationString == "none") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1168 | out.pressureCalibration = Calibration::PressureCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1169 | } else if (pressureCalibrationString == "physical") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1170 | out.pressureCalibration = Calibration::PressureCalibration::PHYSICAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1171 | } else if (pressureCalibrationString == "amplitude") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1172 | out.pressureCalibration = Calibration::PressureCalibration::AMPLITUDE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1173 | } else if (pressureCalibrationString != "default") { |
| 1174 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1175 | pressureCalibrationString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1179 | float pressureScale; |
| 1180 | if (in.tryGetProperty("touch.pressure.scale", pressureScale)) { |
| 1181 | out.pressureScale = pressureScale; |
| 1182 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1183 | |
| 1184 | // Orientation |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1185 | out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1186 | std::string orientationCalibrationString; |
| 1187 | if (in.tryGetProperty("touch.orientation.calibration", orientationCalibrationString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1188 | if (orientationCalibrationString == "none") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1189 | out.orientationCalibration = Calibration::OrientationCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1190 | } else if (orientationCalibrationString == "interpolated") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1191 | out.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1192 | } else if (orientationCalibrationString == "vector") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1193 | out.orientationCalibration = Calibration::OrientationCalibration::VECTOR; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1194 | } else if (orientationCalibrationString != "default") { |
| 1195 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1196 | orientationCalibrationString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | // Distance |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1201 | out.distanceCalibration = Calibration::DistanceCalibration::DEFAULT; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1202 | std::string distanceCalibrationString; |
| 1203 | if (in.tryGetProperty("touch.distance.calibration", distanceCalibrationString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1204 | if (distanceCalibrationString == "none") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1205 | out.distanceCalibration = Calibration::DistanceCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1206 | } else if (distanceCalibrationString == "scaled") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1207 | out.distanceCalibration = Calibration::DistanceCalibration::SCALED; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1208 | } else if (distanceCalibrationString != "default") { |
| 1209 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1210 | distanceCalibrationString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1214 | float distanceScale; |
| 1215 | if (in.tryGetProperty("touch.distance.scale", distanceScale)) { |
| 1216 | out.distanceScale = distanceScale; |
| 1217 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1218 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1219 | out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1220 | std::string coverageCalibrationString; |
| 1221 | if (in.tryGetProperty("touch.coverage.calibration", coverageCalibrationString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1222 | if (coverageCalibrationString == "none") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1223 | out.coverageCalibration = Calibration::CoverageCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1224 | } else if (coverageCalibrationString == "box") { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1225 | out.coverageCalibration = Calibration::CoverageCalibration::BOX; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1226 | } else if (coverageCalibrationString != "default") { |
| 1227 | ALOGW("Invalid value for touch.coverage.calibration: '%s'", |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 1228 | coverageCalibrationString.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void TouchInputMapper::resolveCalibration() { |
| 1234 | // Size |
| 1235 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1236 | if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DEFAULT) { |
| 1237 | mCalibration.sizeCalibration = Calibration::SizeCalibration::GEOMETRIC; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1238 | } |
| 1239 | } else { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1240 | mCalibration.sizeCalibration = Calibration::SizeCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | // Pressure |
| 1244 | if (mRawPointerAxes.pressure.valid) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1245 | if (mCalibration.pressureCalibration == Calibration::PressureCalibration::DEFAULT) { |
| 1246 | mCalibration.pressureCalibration = Calibration::PressureCalibration::PHYSICAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } else { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1249 | mCalibration.pressureCalibration = Calibration::PressureCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | // Orientation |
| 1253 | if (mRawPointerAxes.orientation.valid) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1254 | if (mCalibration.orientationCalibration == Calibration::OrientationCalibration::DEFAULT) { |
| 1255 | mCalibration.orientationCalibration = Calibration::OrientationCalibration::INTERPOLATED; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } else { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1258 | mCalibration.orientationCalibration = Calibration::OrientationCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | // Distance |
| 1262 | if (mRawPointerAxes.distance.valid) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1263 | if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::DEFAULT) { |
| 1264 | mCalibration.distanceCalibration = Calibration::DistanceCalibration::SCALED; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } else { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1267 | mCalibration.distanceCalibration = Calibration::DistanceCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | // Coverage |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1271 | if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::DEFAULT) { |
| 1272 | mCalibration.coverageCalibration = Calibration::CoverageCalibration::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | void TouchInputMapper::dumpCalibration(std::string& dump) { |
| 1277 | dump += INDENT3 "Calibration:\n"; |
| 1278 | |
Siarhei Vishniakou | 4e837cc | 2021-12-20 23:24:33 -0800 | [diff] [blame] | 1279 | dump += INDENT4 "touch.size.calibration: "; |
| 1280 | dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n"; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1281 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1282 | if (mCalibration.sizeScale) { |
| 1283 | dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1286 | if (mCalibration.sizeBias) { |
| 1287 | dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1290 | if (mCalibration.sizeIsSummed) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1291 | dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n", |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1292 | toString(*mCalibration.sizeIsSummed)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | // Pressure |
| 1296 | switch (mCalibration.pressureCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1297 | case Calibration::PressureCalibration::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1298 | dump += INDENT4 "touch.pressure.calibration: none\n"; |
| 1299 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1300 | case Calibration::PressureCalibration::PHYSICAL: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1301 | dump += INDENT4 "touch.pressure.calibration: physical\n"; |
| 1302 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1303 | case Calibration::PressureCalibration::AMPLITUDE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1304 | dump += INDENT4 "touch.pressure.calibration: amplitude\n"; |
| 1305 | break; |
| 1306 | default: |
| 1307 | ALOG_ASSERT(false); |
| 1308 | } |
| 1309 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1310 | if (mCalibration.pressureScale) { |
| 1311 | dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | // Orientation |
| 1315 | switch (mCalibration.orientationCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1316 | case Calibration::OrientationCalibration::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1317 | dump += INDENT4 "touch.orientation.calibration: none\n"; |
| 1318 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1319 | case Calibration::OrientationCalibration::INTERPOLATED: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1320 | dump += INDENT4 "touch.orientation.calibration: interpolated\n"; |
| 1321 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1322 | case Calibration::OrientationCalibration::VECTOR: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1323 | dump += INDENT4 "touch.orientation.calibration: vector\n"; |
| 1324 | break; |
| 1325 | default: |
| 1326 | ALOG_ASSERT(false); |
| 1327 | } |
| 1328 | |
| 1329 | // Distance |
| 1330 | switch (mCalibration.distanceCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1331 | case Calibration::DistanceCalibration::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1332 | dump += INDENT4 "touch.distance.calibration: none\n"; |
| 1333 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1334 | case Calibration::DistanceCalibration::SCALED: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1335 | dump += INDENT4 "touch.distance.calibration: scaled\n"; |
| 1336 | break; |
| 1337 | default: |
| 1338 | ALOG_ASSERT(false); |
| 1339 | } |
| 1340 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 1341 | if (mCalibration.distanceScale) { |
| 1342 | dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | switch (mCalibration.coverageCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1346 | case Calibration::CoverageCalibration::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1347 | dump += INDENT4 "touch.coverage.calibration: none\n"; |
| 1348 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1349 | case Calibration::CoverageCalibration::BOX: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1350 | dump += INDENT4 "touch.coverage.calibration: box\n"; |
| 1351 | break; |
| 1352 | default: |
| 1353 | ALOG_ASSERT(false); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | void TouchInputMapper::dumpAffineTransformation(std::string& dump) { |
| 1358 | dump += INDENT3 "Affine Transformation:\n"; |
| 1359 | |
| 1360 | dump += StringPrintf(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale); |
| 1361 | dump += StringPrintf(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix); |
| 1362 | dump += StringPrintf(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset); |
| 1363 | dump += StringPrintf(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix); |
| 1364 | dump += StringPrintf(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale); |
| 1365 | dump += StringPrintf(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset); |
| 1366 | } |
| 1367 | |
| 1368 | void TouchInputMapper::updateAffineTransformation() { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1369 | mAffineTransform = getPolicy()->getTouchAffineTransformation(getDeviceContext().getDescriptor(), |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1370 | mInputDeviceOrientation); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1373 | std::list<NotifyArgs> TouchInputMapper::reset(nsecs_t when) { |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 1374 | std::list<NotifyArgs> out = cancelTouch(when, when); |
| 1375 | updateTouchSpots(); |
| 1376 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1377 | mCursorButtonAccumulator.reset(getDeviceContext()); |
| 1378 | mCursorScrollAccumulator.reset(getDeviceContext()); |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 1379 | mTouchButtonAccumulator.reset(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1380 | |
| 1381 | mPointerVelocityControl.reset(); |
| 1382 | mWheelXVelocityControl.reset(); |
| 1383 | mWheelYVelocityControl.reset(); |
| 1384 | |
| 1385 | mRawStatesPending.clear(); |
| 1386 | mCurrentRawState.clear(); |
| 1387 | mCurrentCookedState.clear(); |
| 1388 | mLastRawState.clear(); |
| 1389 | mLastCookedState.clear(); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1390 | mPointerUsage = PointerUsage::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1391 | mSentHoverEnter = false; |
| 1392 | mHavePointerIds = false; |
| 1393 | mCurrentMotionAborted = false; |
| 1394 | mDownTime = 0; |
| 1395 | |
| 1396 | mCurrentVirtualKey.down = false; |
| 1397 | |
| 1398 | mPointerGesture.reset(); |
| 1399 | mPointerSimple.reset(); |
| 1400 | resetExternalStylus(); |
| 1401 | |
| 1402 | if (mPointerController != nullptr) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 1403 | mPointerController->fade(PointerControllerInterface::Transition::GRADUAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1404 | mPointerController->clearSpots(); |
| 1405 | } |
| 1406 | |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 1407 | return out += InputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | void TouchInputMapper::resetExternalStylus() { |
| 1411 | mExternalStylusState.clear(); |
| 1412 | mExternalStylusId = -1; |
| 1413 | mExternalStylusFusionTimeout = LLONG_MAX; |
| 1414 | mExternalStylusDataPending = false; |
| 1415 | } |
| 1416 | |
| 1417 | void TouchInputMapper::clearStylusDataPendingFlags() { |
| 1418 | mExternalStylusDataPending = false; |
| 1419 | mExternalStylusFusionTimeout = LLONG_MAX; |
| 1420 | } |
| 1421 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1422 | std::list<NotifyArgs> TouchInputMapper::process(const RawEvent* rawEvent) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1423 | mCursorButtonAccumulator.process(rawEvent); |
| 1424 | mCursorScrollAccumulator.process(rawEvent); |
| 1425 | mTouchButtonAccumulator.process(rawEvent); |
| 1426 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1427 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1428 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1429 | out += sync(rawEvent->when, rawEvent->readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1430 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1431 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1434 | std::list<NotifyArgs> TouchInputMapper::sync(nsecs_t when, nsecs_t readTime) { |
| 1435 | std::list<NotifyArgs> out; |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 1436 | if (mDeviceMode == DeviceMode::DISABLED) { |
| 1437 | // Only save the last pending state when the device is disabled. |
| 1438 | mRawStatesPending.clear(); |
| 1439 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1440 | // Push a new state. |
| 1441 | mRawStatesPending.emplace_back(); |
| 1442 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 1443 | RawState& next = mRawStatesPending.back(); |
| 1444 | next.clear(); |
| 1445 | next.when = when; |
| 1446 | next.readTime = readTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1447 | |
| 1448 | // Sync button state. |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 1449 | next.buttonState = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1450 | mTouchButtonAccumulator.getButtonState() | mCursorButtonAccumulator.getButtonState(); |
| 1451 | |
| 1452 | // Sync scroll |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 1453 | next.rawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 1454 | next.rawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1455 | mCursorScrollAccumulator.finishSync(); |
| 1456 | |
| 1457 | // Sync touch |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 1458 | syncTouch(when, &next); |
| 1459 | |
| 1460 | // The last RawState is the actually second to last, since we just added a new state |
| 1461 | const RawState& last = |
| 1462 | mRawStatesPending.size() == 1 ? mCurrentRawState : mRawStatesPending.rbegin()[1]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1463 | |
| 1464 | // Assign pointer ids. |
| 1465 | if (!mHavePointerIds) { |
| 1466 | assignPointerIds(last, next); |
| 1467 | } |
| 1468 | |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1469 | ALOGD_IF(DEBUG_RAW_EVENTS, |
| 1470 | "syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
| 1471 | "hovering ids 0x%08x -> 0x%08x, canceled ids 0x%08x", |
| 1472 | last.rawPointerData.pointerCount, next.rawPointerData.pointerCount, |
| 1473 | last.rawPointerData.touchingIdBits.value, next.rawPointerData.touchingIdBits.value, |
| 1474 | last.rawPointerData.hoveringIdBits.value, next.rawPointerData.hoveringIdBits.value, |
| 1475 | next.rawPointerData.canceledIdBits.value); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1476 | |
Arthur Hung | 9ad1894 | 2021-06-19 02:04:46 +0000 | [diff] [blame] | 1477 | if (!next.rawPointerData.touchingIdBits.isEmpty() && |
| 1478 | !next.rawPointerData.hoveringIdBits.isEmpty() && |
| 1479 | last.rawPointerData.hoveringIdBits != next.rawPointerData.hoveringIdBits) { |
| 1480 | ALOGI("Multi-touch contains some hovering ids 0x%08x", |
| 1481 | next.rawPointerData.hoveringIdBits.value); |
| 1482 | } |
| 1483 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1484 | out += processRawTouches(false /*timeout*/); |
| 1485 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1488 | std::list<NotifyArgs> TouchInputMapper::processRawTouches(bool timeout) { |
| 1489 | std::list<NotifyArgs> out; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1490 | if (mDeviceMode == DeviceMode::DISABLED) { |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 1491 | // Do not process raw event while the device is disabled. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1492 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | // Drain any pending touch states. The invariant here is that the mCurrentRawState is always |
| 1496 | // valid and must go through the full cook and dispatch cycle. This ensures that anything |
| 1497 | // touching the current state will only observe the events that have been dispatched to the |
| 1498 | // rest of the pipeline. |
| 1499 | const size_t N = mRawStatesPending.size(); |
| 1500 | size_t count; |
| 1501 | for (count = 0; count < N; count++) { |
| 1502 | const RawState& next = mRawStatesPending[count]; |
| 1503 | |
| 1504 | // A failure to assign the stylus id means that we're waiting on stylus data |
| 1505 | // and so should defer the rest of the pipeline. |
| 1506 | if (assignExternalStylusId(next, timeout)) { |
| 1507 | break; |
| 1508 | } |
| 1509 | |
| 1510 | // All ready to go. |
| 1511 | clearStylusDataPendingFlags(); |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 1512 | mCurrentRawState = next; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1513 | if (mCurrentRawState.when < mLastRawState.when) { |
| 1514 | mCurrentRawState.when = mLastRawState.when; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1515 | mCurrentRawState.readTime = mLastRawState.readTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1516 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1517 | out += cookAndDispatch(mCurrentRawState.when, mCurrentRawState.readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1518 | } |
| 1519 | if (count != 0) { |
| 1520 | mRawStatesPending.erase(mRawStatesPending.begin(), mRawStatesPending.begin() + count); |
| 1521 | } |
| 1522 | |
| 1523 | if (mExternalStylusDataPending) { |
| 1524 | if (timeout) { |
| 1525 | nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY; |
| 1526 | clearStylusDataPendingFlags(); |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 1527 | mCurrentRawState = mLastRawState; |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1528 | ALOGD_IF(DEBUG_STYLUS_FUSION, |
| 1529 | "Timeout expired, synthesizing event with new stylus data"); |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1530 | const nsecs_t readTime = when; // consider this synthetic event to be zero latency |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1531 | out += cookAndDispatch(when, readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1532 | } else if (mExternalStylusFusionTimeout == LLONG_MAX) { |
| 1533 | mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT; |
| 1534 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); |
| 1535 | } |
| 1536 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1537 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1540 | std::list<NotifyArgs> TouchInputMapper::cookAndDispatch(nsecs_t when, nsecs_t readTime) { |
| 1541 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1542 | // Always start with a clean state. |
| 1543 | mCurrentCookedState.clear(); |
| 1544 | |
| 1545 | // Apply stylus buttons to current raw state. |
| 1546 | applyExternalStylusButtonState(when); |
| 1547 | |
| 1548 | // Handle policy on initial down or hover events. |
| 1549 | bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 && |
| 1550 | mCurrentRawState.rawPointerData.pointerCount != 0; |
| 1551 | |
| 1552 | uint32_t policyFlags = 0; |
| 1553 | bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState; |
| 1554 | if (initialDown || buttonsPressed) { |
| 1555 | // If this is a touch screen, hide the pointer on an initial down. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1556 | if (mDeviceMode == DeviceMode::DIRECT) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1557 | getContext()->fadePointer(); |
| 1558 | } |
| 1559 | |
| 1560 | if (mParameters.wake) { |
| 1561 | policyFlags |= POLICY_FLAG_WAKE; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | // Consume raw off-screen touches before cooking pointer data. |
| 1566 | // If touches are consumed, subsequent code will not receive any pointer data. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1567 | bool consumed; |
| 1568 | out += consumeRawTouches(when, readTime, policyFlags, consumed /*byref*/); |
| 1569 | if (consumed) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1570 | mCurrentRawState.rawPointerData.clear(); |
| 1571 | } |
| 1572 | |
| 1573 | // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure |
| 1574 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 1575 | // The following code can use either the raw or cooked data, as needed. |
| 1576 | cookPointerData(); |
| 1577 | |
| 1578 | // Apply stylus pressure to current cooked state. |
| 1579 | applyExternalStylusTouchState(when); |
| 1580 | |
| 1581 | // Synthesize key down from raw buttons if needed. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1582 | out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, readTime, getDeviceId(), |
| 1583 | mSource, mViewport.displayId, policyFlags, |
| 1584 | mLastCookedState.buttonState, mCurrentCookedState.buttonState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1585 | |
| 1586 | // Dispatch the touches either directly or by translation through a pointer on screen. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1587 | if (mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1588 | for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits); !idBits.isEmpty();) { |
| 1589 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1590 | const RawPointerData::Pointer& pointer = |
| 1591 | mCurrentRawState.rawPointerData.pointerForId(id); |
| 1592 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 1593 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 1594 | mCurrentCookedState.stylusIdBits.markBit(id); |
| 1595 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER || |
| 1596 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 1597 | mCurrentCookedState.fingerIdBits.markBit(id); |
| 1598 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 1599 | mCurrentCookedState.mouseIdBits.markBit(id); |
| 1600 | } |
| 1601 | } |
| 1602 | for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits); !idBits.isEmpty();) { |
| 1603 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1604 | const RawPointerData::Pointer& pointer = |
| 1605 | mCurrentRawState.rawPointerData.pointerForId(id); |
| 1606 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || |
| 1607 | pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 1608 | mCurrentCookedState.stylusIdBits.markBit(id); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 1613 | PointerUsage pointerUsage = mPointerUsage; |
| 1614 | if (!mCurrentCookedState.stylusIdBits.isEmpty()) { |
| 1615 | mCurrentCookedState.mouseIdBits.clear(); |
| 1616 | mCurrentCookedState.fingerIdBits.clear(); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1617 | pointerUsage = PointerUsage::STYLUS; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1618 | } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) { |
| 1619 | mCurrentCookedState.fingerIdBits.clear(); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1620 | pointerUsage = PointerUsage::MOUSE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1621 | } else if (!mCurrentCookedState.fingerIdBits.isEmpty() || |
| 1622 | isPointerDown(mCurrentRawState.buttonState)) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1623 | pointerUsage = PointerUsage::GESTURES; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1626 | out += dispatchPointerUsage(when, readTime, policyFlags, pointerUsage); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1627 | } else { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1628 | if (!mCurrentMotionAborted) { |
Prabir Pradhan | 9eb4e69 | 2022-04-27 13:19:15 +0000 | [diff] [blame] | 1629 | updateTouchSpots(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1630 | out += dispatchButtonRelease(when, readTime, policyFlags); |
| 1631 | out += dispatchHoverExit(when, readTime, policyFlags); |
| 1632 | out += dispatchTouches(when, readTime, policyFlags); |
| 1633 | out += dispatchHoverEnterAndMove(when, readTime, policyFlags); |
| 1634 | out += dispatchButtonPress(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | if (mCurrentCookedState.cookedPointerData.pointerCount == 0) { |
| 1638 | mCurrentMotionAborted = false; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // Synthesize key up from raw buttons if needed. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1643 | out += synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, readTime, getDeviceId(), |
| 1644 | mSource, mViewport.displayId, policyFlags, |
| 1645 | mLastCookedState.buttonState, mCurrentCookedState.buttonState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1646 | |
| 1647 | // Clear some transient state. |
| 1648 | mCurrentRawState.rawVScroll = 0; |
| 1649 | mCurrentRawState.rawHScroll = 0; |
| 1650 | |
| 1651 | // Copy current touch to last touch in preparation for the next cycle. |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 1652 | mLastRawState = mCurrentRawState; |
| 1653 | mLastCookedState = mCurrentCookedState; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1654 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Garfield Tan | c734e4f | 2021-01-15 20:01:39 -0800 | [diff] [blame] | 1657 | void TouchInputMapper::updateTouchSpots() { |
| 1658 | if (!mConfig.showTouches || mPointerController == nullptr) { |
| 1659 | return; |
| 1660 | } |
| 1661 | |
| 1662 | // Update touch spots when this is a touchscreen even when it's not enabled so that we can |
| 1663 | // clear touch spots. |
| 1664 | if (mDeviceMode != DeviceMode::DIRECT && |
| 1665 | (mDeviceMode != DeviceMode::DISABLED || !isTouchScreen())) { |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | mPointerController->setPresentation(PointerControllerInterface::Presentation::SPOT); |
| 1670 | mPointerController->fade(PointerControllerInterface::Transition::GRADUAL); |
| 1671 | |
| 1672 | mPointerController->setButtonState(mCurrentRawState.buttonState); |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 1673 | mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords.cbegin(), |
| 1674 | mCurrentCookedState.cookedPointerData.idToIndex.cbegin(), |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 1675 | mCurrentCookedState.cookedPointerData.touchingIdBits, |
| 1676 | mViewport.displayId); |
Garfield Tan | c734e4f | 2021-01-15 20:01:39 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | bool TouchInputMapper::isTouchScreen() { |
| 1680 | return mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN && |
| 1681 | mParameters.hasAssociatedDisplay; |
| 1682 | } |
| 1683 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1684 | void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1685 | if (mDeviceMode == DeviceMode::DIRECT && hasExternalStylus() && mExternalStylusId != -1) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1686 | mCurrentRawState.buttonState |= mExternalStylusState.buttons; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) { |
| 1691 | CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData; |
| 1692 | const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData; |
| 1693 | |
| 1694 | if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) { |
| 1695 | float pressure = mExternalStylusState.pressure; |
| 1696 | if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) { |
| 1697 | const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId); |
| 1698 | pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE); |
| 1699 | } |
| 1700 | PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId); |
| 1701 | coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 1702 | |
| 1703 | PointerProperties& properties = |
| 1704 | currentPointerData.editPointerPropertiesWithId(mExternalStylusId); |
| 1705 | if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 1706 | properties.toolType = mExternalStylusState.toolType; |
| 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1712 | if (mDeviceMode != DeviceMode::DIRECT || !hasExternalStylus()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0 && |
| 1717 | state.rawPointerData.pointerCount != 0; |
| 1718 | if (initialDown) { |
| 1719 | if (mExternalStylusState.pressure != 0.0f) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1720 | ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1721 | mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit(); |
| 1722 | } else if (timeout) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1723 | ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus."); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1724 | resetExternalStylus(); |
| 1725 | } else { |
| 1726 | if (mExternalStylusFusionTimeout == LLONG_MAX) { |
| 1727 | mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT; |
| 1728 | } |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1729 | ALOGD_IF(DEBUG_STYLUS_FUSION, |
| 1730 | "No stylus data but stylus is connected, requesting timeout (%" PRId64 "ms)", |
| 1731 | mExternalStylusFusionTimeout); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1732 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); |
| 1733 | return true; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | // Check if the stylus pointer has gone up. |
| 1738 | if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1739 | ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1740 | mExternalStylusId = -1; |
| 1741 | } |
| 1742 | |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1746 | std::list<NotifyArgs> TouchInputMapper::timeoutExpired(nsecs_t when) { |
| 1747 | std::list<NotifyArgs> out; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1748 | if (mDeviceMode == DeviceMode::POINTER) { |
| 1749 | if (mPointerUsage == PointerUsage::GESTURES) { |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1750 | // Since this is a synthetic event, we can consider its latency to be zero |
| 1751 | const nsecs_t readTime = when; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1752 | out += dispatchPointerGestures(when, readTime, 0 /*policyFlags*/, true /*isTimeout*/); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1753 | } |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 1754 | } else if (mDeviceMode == DeviceMode::DIRECT) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1755 | if (mExternalStylusFusionTimeout < when) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1756 | out += processRawTouches(true /*timeout*/); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1757 | } else if (mExternalStylusFusionTimeout != LLONG_MAX) { |
| 1758 | getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout); |
| 1759 | } |
| 1760 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1761 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1764 | std::list<NotifyArgs> TouchInputMapper::updateExternalStylusState(const StylusState& state) { |
| 1765 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1766 | mExternalStylusState.copyFrom(state); |
| 1767 | if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) { |
| 1768 | // We're either in the middle of a fused stream of data or we're waiting on data before |
| 1769 | // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus |
| 1770 | // data. |
| 1771 | mExternalStylusDataPending = true; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1772 | out += processRawTouches(false /*timeout*/); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1773 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1774 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1777 | std::list<NotifyArgs> TouchInputMapper::consumeRawTouches(nsecs_t when, nsecs_t readTime, |
| 1778 | uint32_t policyFlags, bool& outConsumed) { |
| 1779 | outConsumed = false; |
| 1780 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1781 | // Check for release of a virtual key. |
| 1782 | if (mCurrentVirtualKey.down) { |
| 1783 | if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { |
| 1784 | // Pointer went up while virtual key was down. |
| 1785 | mCurrentVirtualKey.down = false; |
| 1786 | if (!mCurrentVirtualKey.ignored) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1787 | ALOGD_IF(DEBUG_VIRTUAL_KEYS, |
| 1788 | "VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
| 1789 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1790 | out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP, |
| 1791 | AKEY_EVENT_FLAG_FROM_SYSTEM | |
| 1792 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1793 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1794 | outConsumed = true; |
| 1795 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) { |
| 1799 | uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit(); |
| 1800 | const RawPointerData::Pointer& pointer = |
| 1801 | mCurrentRawState.rawPointerData.pointerForId(id); |
| 1802 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 1803 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 1804 | // Pointer is still within the space of the virtual key. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1805 | outConsumed = true; |
| 1806 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | // Pointer left virtual key area or another pointer also went down. |
| 1811 | // Send key cancellation but do not consume the touch yet. |
| 1812 | // This is useful when the user swipes through from the virtual key area |
| 1813 | // into the main display surface. |
| 1814 | mCurrentVirtualKey.down = false; |
| 1815 | if (!mCurrentVirtualKey.ignored) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1816 | ALOGD_IF(DEBUG_VIRTUAL_KEYS, "VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
| 1817 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1818 | out.push_back(dispatchVirtualKey(when, readTime, policyFlags, AKEY_EVENT_ACTION_UP, |
| 1819 | AKEY_EVENT_FLAG_FROM_SYSTEM | |
| 1820 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY | |
| 1821 | AKEY_EVENT_FLAG_CANCELED)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | if (mLastRawState.rawPointerData.touchingIdBits.isEmpty() && |
| 1826 | !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { |
| 1827 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 1828 | uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit(); |
| 1829 | const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 1830 | // Skip checking whether the pointer is inside the physical frame if the device is in |
| 1831 | // unscaled mode. |
| 1832 | if (!isPointInsidePhysicalFrame(pointer.x, pointer.y) && |
| 1833 | mDeviceMode != DeviceMode::UNSCALED) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1834 | // If exactly one pointer went down, check for virtual key hit. |
| 1835 | // Otherwise we will drop the entire stroke. |
| 1836 | if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) { |
| 1837 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 1838 | if (virtualKey) { |
| 1839 | mCurrentVirtualKey.down = true; |
| 1840 | mCurrentVirtualKey.downTime = when; |
| 1841 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 1842 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 1843 | mCurrentVirtualKey.ignored = |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1844 | getContext()->shouldDropVirtualKey(when, virtualKey->keyCode, |
| 1845 | virtualKey->scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1846 | |
| 1847 | if (!mCurrentVirtualKey.ignored) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 1848 | ALOGD_IF(DEBUG_VIRTUAL_KEYS, |
| 1849 | "VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
| 1850 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1851 | out.push_back(dispatchVirtualKey(when, readTime, policyFlags, |
| 1852 | AKEY_EVENT_ACTION_DOWN, |
| 1853 | AKEY_EVENT_FLAG_FROM_SYSTEM | |
| 1854 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1855 | } |
| 1856 | } |
| 1857 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1858 | outConsumed = true; |
| 1859 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | // Disable all virtual key touches that happen within a short time interval of the |
| 1864 | // most recent touch within the screen area. The idea is to filter out stray |
| 1865 | // virtual key presses when interacting with the touch screen. |
| 1866 | // |
| 1867 | // Problems we're trying to solve: |
| 1868 | // |
| 1869 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 1870 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 1871 | // triggers a virtual key. |
| 1872 | // |
| 1873 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 1874 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 1875 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 1876 | // is displayed. |
| 1877 | if (mConfig.virtualKeyQuietTime > 0 && |
| 1878 | !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1879 | getContext()->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1880 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1881 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1884 | NotifyKeyArgs TouchInputMapper::dispatchVirtualKey(nsecs_t when, nsecs_t readTime, |
| 1885 | uint32_t policyFlags, int32_t keyEventAction, |
| 1886 | int32_t keyEventFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1887 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 1888 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 1889 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 1890 | int32_t metaState = getContext()->getGlobalMetaState(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1891 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 1892 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1893 | return NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 1894 | AINPUT_SOURCE_KEYBOARD, mViewport.displayId, policyFlags, keyEventAction, |
| 1895 | keyEventFlags, keyCode, scanCode, metaState, downTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1898 | std::list<NotifyArgs> TouchInputMapper::abortTouches(nsecs_t when, nsecs_t readTime, |
| 1899 | uint32_t policyFlags) { |
| 1900 | std::list<NotifyArgs> out; |
lilinnan | 687e58f | 2022-07-19 16:00:50 +0800 | [diff] [blame] | 1901 | if (mCurrentMotionAborted) { |
| 1902 | // Current motion event was already aborted. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1903 | return out; |
lilinnan | 687e58f | 2022-07-19 16:00:50 +0800 | [diff] [blame] | 1904 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1905 | BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits; |
| 1906 | if (!currentIdBits.isEmpty()) { |
| 1907 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 1908 | int32_t buttonState = mCurrentCookedState.buttonState; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1909 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 1910 | AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED, |
| 1911 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1912 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 1913 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 1914 | mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, |
| 1915 | -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 1916 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1917 | mCurrentMotionAborted = true; |
| 1918 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1919 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 1922 | // Updates pointer coords and properties for pointers with specified ids that have moved. |
| 1923 | // Returns true if any of them changed. |
| 1924 | static bool updateMovedPointers(const PropertiesArray& inProperties, CoordsArray& inCoords, |
| 1925 | const IdToIndexArray& inIdToIndex, PropertiesArray& outProperties, |
| 1926 | CoordsArray& outCoords, IdToIndexArray& outIdToIndex, |
| 1927 | BitSet32 idBits) { |
| 1928 | bool changed = false; |
| 1929 | while (!idBits.isEmpty()) { |
| 1930 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1931 | uint32_t inIndex = inIdToIndex[id]; |
| 1932 | uint32_t outIndex = outIdToIndex[id]; |
| 1933 | |
| 1934 | const PointerProperties& curInProperties = inProperties[inIndex]; |
| 1935 | const PointerCoords& curInCoords = inCoords[inIndex]; |
| 1936 | PointerProperties& curOutProperties = outProperties[outIndex]; |
| 1937 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 1938 | |
| 1939 | if (curInProperties != curOutProperties) { |
| 1940 | curOutProperties.copyFrom(curInProperties); |
| 1941 | changed = true; |
| 1942 | } |
| 1943 | |
| 1944 | if (curInCoords != curOutCoords) { |
| 1945 | curOutCoords.copyFrom(curInCoords); |
| 1946 | changed = true; |
| 1947 | } |
| 1948 | } |
| 1949 | return changed; |
| 1950 | } |
| 1951 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1952 | std::list<NotifyArgs> TouchInputMapper::dispatchTouches(nsecs_t when, nsecs_t readTime, |
| 1953 | uint32_t policyFlags) { |
| 1954 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1955 | BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits; |
| 1956 | BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits; |
| 1957 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 1958 | int32_t buttonState = mCurrentCookedState.buttonState; |
| 1959 | |
| 1960 | if (currentIdBits == lastIdBits) { |
| 1961 | if (!currentIdBits.isEmpty()) { |
| 1962 | // No pointer id changes so this is a move event. |
| 1963 | // The listener takes care of batching moves so we don't have to deal with that here. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 1964 | out.push_back( |
| 1965 | dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, |
| 1966 | 0, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 1967 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 1968 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 1969 | mCurrentCookedState.cookedPointerData.idToIndex, currentIdBits, |
| 1970 | -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 1971 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1972 | } |
| 1973 | } else { |
| 1974 | // There may be pointers going up and pointers going down and pointers moving |
| 1975 | // all at the same time. |
| 1976 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 1977 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
| 1978 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
| 1979 | BitSet32 dispatchedIdBits(lastIdBits.value); |
| 1980 | |
| 1981 | // Update last coordinates of pointers that have moved so that we observe the new |
| 1982 | // pointer positions at the same time as other pointers that have just gone up. |
| 1983 | bool moveNeeded = |
| 1984 | updateMovedPointers(mCurrentCookedState.cookedPointerData.pointerProperties, |
| 1985 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 1986 | mCurrentCookedState.cookedPointerData.idToIndex, |
| 1987 | mLastCookedState.cookedPointerData.pointerProperties, |
| 1988 | mLastCookedState.cookedPointerData.pointerCoords, |
| 1989 | mLastCookedState.cookedPointerData.idToIndex, moveIdBits); |
| 1990 | if (buttonState != mLastCookedState.buttonState) { |
| 1991 | moveNeeded = true; |
| 1992 | } |
| 1993 | |
| 1994 | // Dispatch pointer up events. |
| 1995 | while (!upIdBits.isEmpty()) { |
| 1996 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 1997 | bool isCanceled = mCurrentCookedState.cookedPointerData.canceledIdBits.hasBit(upId); |
arthurhung | 17d6484 | 2021-01-21 16:01:27 +0800 | [diff] [blame] | 1998 | if (isCanceled) { |
| 1999 | ALOGI("Canceling pointer %d for the palm event was detected.", upId); |
| 2000 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2001 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2002 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
| 2003 | isCanceled ? AMOTION_EVENT_FLAG_CANCELED : 0, metaState, |
| 2004 | buttonState, 0, |
| 2005 | mLastCookedState.cookedPointerData.pointerProperties, |
| 2006 | mLastCookedState.cookedPointerData.pointerCoords, |
| 2007 | mLastCookedState.cookedPointerData.idToIndex, |
| 2008 | dispatchedIdBits, upId, mOrientedXPrecision, |
| 2009 | mOrientedYPrecision, mDownTime, |
| 2010 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2011 | dispatchedIdBits.clearBit(upId); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 2012 | mCurrentCookedState.cookedPointerData.canceledIdBits.clearBit(upId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 2016 | // Although applications receive new locations as part of individual pointer up |
| 2017 | // events, they do not generally handle them except when presented in a move event. |
| 2018 | if (moveNeeded && !moveIdBits.isEmpty()) { |
| 2019 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2020 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2021 | AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, 0, |
| 2022 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 2023 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 2024 | mCurrentCookedState.cookedPointerData.idToIndex, |
| 2025 | dispatchedIdBits, -1, mOrientedXPrecision, |
| 2026 | mOrientedYPrecision, mDownTime, |
| 2027 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | // Dispatch pointer down events using the new pointer locations. |
| 2031 | while (!downIdBits.isEmpty()) { |
| 2032 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
| 2033 | dispatchedIdBits.markBit(downId); |
| 2034 | |
| 2035 | if (dispatchedIdBits.count() == 1) { |
| 2036 | // First pointer is going down. Set down time. |
| 2037 | mDownTime = when; |
| 2038 | } |
| 2039 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2040 | out.push_back( |
| 2041 | dispatchMotion(when, readTime, policyFlags, mSource, |
| 2042 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState, |
| 2043 | 0, mCurrentCookedState.cookedPointerData.pointerProperties, |
| 2044 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 2045 | mCurrentCookedState.cookedPointerData.idToIndex, |
| 2046 | dispatchedIdBits, downId, mOrientedXPrecision, |
| 2047 | mOrientedYPrecision, mDownTime, MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2048 | } |
| 2049 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2050 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2053 | std::list<NotifyArgs> TouchInputMapper::dispatchHoverExit(nsecs_t when, nsecs_t readTime, |
| 2054 | uint32_t policyFlags) { |
| 2055 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2056 | if (mSentHoverEnter && |
| 2057 | (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty() || |
| 2058 | !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) { |
| 2059 | int32_t metaState = getContext()->getGlobalMetaState(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2060 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2061 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState, |
| 2062 | mLastCookedState.buttonState, 0, |
| 2063 | mLastCookedState.cookedPointerData.pointerProperties, |
| 2064 | mLastCookedState.cookedPointerData.pointerCoords, |
| 2065 | mLastCookedState.cookedPointerData.idToIndex, |
| 2066 | mLastCookedState.cookedPointerData.hoveringIdBits, -1, |
| 2067 | mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 2068 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2069 | mSentHoverEnter = false; |
| 2070 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2071 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2074 | std::list<NotifyArgs> TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, |
| 2075 | uint32_t policyFlags) { |
| 2076 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2077 | if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty() && |
| 2078 | !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) { |
| 2079 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 2080 | if (!mSentHoverEnter) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2081 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2082 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState, |
| 2083 | mCurrentRawState.buttonState, 0, |
| 2084 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 2085 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 2086 | mCurrentCookedState.cookedPointerData.idToIndex, |
| 2087 | mCurrentCookedState.cookedPointerData.hoveringIdBits, -1, |
| 2088 | mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 2089 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2090 | mSentHoverEnter = true; |
| 2091 | } |
| 2092 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2093 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2094 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState, |
| 2095 | mCurrentRawState.buttonState, 0, |
| 2096 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 2097 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 2098 | mCurrentCookedState.cookedPointerData.idToIndex, |
| 2099 | mCurrentCookedState.cookedPointerData.hoveringIdBits, -1, |
| 2100 | mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 2101 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2102 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2103 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2106 | std::list<NotifyArgs> TouchInputMapper::dispatchButtonRelease(nsecs_t when, nsecs_t readTime, |
| 2107 | uint32_t policyFlags) { |
| 2108 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2109 | BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState); |
| 2110 | const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData); |
| 2111 | const int32_t metaState = getContext()->getGlobalMetaState(); |
| 2112 | int32_t buttonState = mLastCookedState.buttonState; |
| 2113 | while (!releasedButtons.isEmpty()) { |
| 2114 | int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit()); |
| 2115 | buttonState &= ~actionButton; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2116 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2117 | AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0, |
| 2118 | metaState, buttonState, 0, |
Prabir Pradhan | 211ba62 | 2022-10-31 21:09:21 +0000 | [diff] [blame] | 2119 | mLastCookedState.cookedPointerData.pointerProperties, |
| 2120 | mLastCookedState.cookedPointerData.pointerCoords, |
| 2121 | mLastCookedState.cookedPointerData.idToIndex, idBits, -1, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2122 | mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 2123 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2124 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2125 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2128 | std::list<NotifyArgs> TouchInputMapper::dispatchButtonPress(nsecs_t when, nsecs_t readTime, |
| 2129 | uint32_t policyFlags) { |
| 2130 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2131 | BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState); |
| 2132 | const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData); |
| 2133 | const int32_t metaState = getContext()->getGlobalMetaState(); |
| 2134 | int32_t buttonState = mLastCookedState.buttonState; |
| 2135 | while (!pressedButtons.isEmpty()) { |
| 2136 | int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit()); |
| 2137 | buttonState |= actionButton; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2138 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2139 | AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0, metaState, |
| 2140 | buttonState, 0, |
| 2141 | mCurrentCookedState.cookedPointerData.pointerProperties, |
| 2142 | mCurrentCookedState.cookedPointerData.pointerCoords, |
| 2143 | mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1, |
| 2144 | mOrientedXPrecision, mOrientedYPrecision, mDownTime, |
| 2145 | MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2146 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2147 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) { |
| 2151 | if (!cookedPointerData.touchingIdBits.isEmpty()) { |
| 2152 | return cookedPointerData.touchingIdBits; |
| 2153 | } |
| 2154 | return cookedPointerData.hoveringIdBits; |
| 2155 | } |
| 2156 | |
| 2157 | void TouchInputMapper::cookPointerData() { |
| 2158 | uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount; |
| 2159 | |
| 2160 | mCurrentCookedState.cookedPointerData.clear(); |
| 2161 | mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount; |
| 2162 | mCurrentCookedState.cookedPointerData.hoveringIdBits = |
| 2163 | mCurrentRawState.rawPointerData.hoveringIdBits; |
| 2164 | mCurrentCookedState.cookedPointerData.touchingIdBits = |
| 2165 | mCurrentRawState.rawPointerData.touchingIdBits; |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 2166 | mCurrentCookedState.cookedPointerData.canceledIdBits = |
| 2167 | mCurrentRawState.rawPointerData.canceledIdBits; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2168 | |
| 2169 | if (mCurrentCookedState.cookedPointerData.pointerCount == 0) { |
| 2170 | mCurrentCookedState.buttonState = 0; |
| 2171 | } else { |
| 2172 | mCurrentCookedState.buttonState = mCurrentRawState.buttonState; |
| 2173 | } |
| 2174 | |
| 2175 | // Walk through the the active pointers and map device coordinates onto |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2176 | // display coordinates and adjust for display orientation. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2177 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 2178 | const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i]; |
| 2179 | |
| 2180 | // Size |
| 2181 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 2182 | switch (mCalibration.sizeCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2183 | case Calibration::SizeCalibration::GEOMETRIC: |
| 2184 | case Calibration::SizeCalibration::DIAMETER: |
| 2185 | case Calibration::SizeCalibration::BOX: |
| 2186 | case Calibration::SizeCalibration::AREA: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2187 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 2188 | touchMajor = in.touchMajor; |
| 2189 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 2190 | toolMajor = in.toolMajor; |
| 2191 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 2192 | size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor) |
| 2193 | : in.touchMajor; |
| 2194 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 2195 | toolMajor = touchMajor = in.touchMajor; |
| 2196 | toolMinor = touchMinor = |
| 2197 | mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 2198 | size = mRawPointerAxes.touchMinor.valid ? avg(in.touchMajor, in.touchMinor) |
| 2199 | : in.touchMajor; |
| 2200 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 2201 | touchMajor = toolMajor = in.toolMajor; |
| 2202 | touchMinor = toolMinor = |
| 2203 | mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 2204 | size = mRawPointerAxes.toolMinor.valid ? avg(in.toolMajor, in.toolMinor) |
| 2205 | : in.toolMajor; |
| 2206 | } else { |
| 2207 | ALOG_ASSERT(false, |
| 2208 | "No touch or tool axes. " |
| 2209 | "Size calibration should have been resolved to NONE."); |
| 2210 | touchMajor = 0; |
| 2211 | touchMinor = 0; |
| 2212 | toolMajor = 0; |
| 2213 | toolMinor = 0; |
| 2214 | size = 0; |
| 2215 | } |
| 2216 | |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2217 | if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2218 | uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count(); |
| 2219 | if (touchingCount > 1) { |
| 2220 | touchMajor /= touchingCount; |
| 2221 | touchMinor /= touchingCount; |
| 2222 | toolMajor /= touchingCount; |
| 2223 | toolMinor /= touchingCount; |
| 2224 | size /= touchingCount; |
| 2225 | } |
| 2226 | } |
| 2227 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2228 | if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2229 | touchMajor *= mGeometricScale; |
| 2230 | touchMinor *= mGeometricScale; |
| 2231 | toolMajor *= mGeometricScale; |
| 2232 | toolMinor *= mGeometricScale; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2233 | } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::AREA) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2234 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
| 2235 | touchMinor = touchMajor; |
| 2236 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 2237 | toolMinor = toolMajor; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2238 | } else if (mCalibration.sizeCalibration == Calibration::SizeCalibration::DIAMETER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2239 | touchMinor = touchMajor; |
| 2240 | toolMinor = toolMajor; |
| 2241 | } |
| 2242 | |
Siarhei Vishniakou | 0724734 | 2022-07-15 14:27:37 -0700 | [diff] [blame] | 2243 | mCalibration.applySizeScaleAndBias(touchMajor); |
| 2244 | mCalibration.applySizeScaleAndBias(touchMinor); |
| 2245 | mCalibration.applySizeScaleAndBias(toolMajor); |
| 2246 | mCalibration.applySizeScaleAndBias(toolMinor); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2247 | size *= mSizeScale; |
| 2248 | break; |
Siarhei Vishniakou | 0724734 | 2022-07-15 14:27:37 -0700 | [diff] [blame] | 2249 | case Calibration::SizeCalibration::DEFAULT: |
| 2250 | LOG_ALWAYS_FATAL("Resolution should not be 'DEFAULT' at this point"); |
| 2251 | break; |
| 2252 | case Calibration::SizeCalibration::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2253 | touchMajor = 0; |
| 2254 | touchMinor = 0; |
| 2255 | toolMajor = 0; |
| 2256 | toolMinor = 0; |
| 2257 | size = 0; |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | // Pressure |
| 2262 | float pressure; |
| 2263 | switch (mCalibration.pressureCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2264 | case Calibration::PressureCalibration::PHYSICAL: |
| 2265 | case Calibration::PressureCalibration::AMPLITUDE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2266 | pressure = in.pressure * mPressureScale; |
| 2267 | break; |
| 2268 | default: |
| 2269 | pressure = in.isHovering ? 0 : 1; |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | // Tilt and Orientation |
| 2274 | float tilt; |
| 2275 | float orientation; |
| 2276 | if (mHaveTilt) { |
| 2277 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 2278 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 2279 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 2280 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 2281 | } else { |
| 2282 | tilt = 0; |
| 2283 | |
| 2284 | switch (mCalibration.orientationCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2285 | case Calibration::OrientationCalibration::INTERPOLATED: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2286 | orientation = in.orientation * mOrientationScale; |
| 2287 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2288 | case Calibration::OrientationCalibration::VECTOR: { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2289 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 2290 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 2291 | if (c1 != 0 || c2 != 0) { |
| 2292 | orientation = atan2f(c1, c2) * 0.5f; |
| 2293 | float confidence = hypotf(c1, c2); |
| 2294 | float scale = 1.0f + confidence / 16.0f; |
| 2295 | touchMajor *= scale; |
| 2296 | touchMinor /= scale; |
| 2297 | toolMajor *= scale; |
| 2298 | toolMinor /= scale; |
| 2299 | } else { |
| 2300 | orientation = 0; |
| 2301 | } |
| 2302 | break; |
| 2303 | } |
| 2304 | default: |
| 2305 | orientation = 0; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | // Distance |
| 2310 | float distance; |
| 2311 | switch (mCalibration.distanceCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2312 | case Calibration::DistanceCalibration::SCALED: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2313 | distance = in.distance * mDistanceScale; |
| 2314 | break; |
| 2315 | default: |
| 2316 | distance = 0; |
| 2317 | } |
| 2318 | |
| 2319 | // Coverage |
| 2320 | int32_t rawLeft, rawTop, rawRight, rawBottom; |
| 2321 | switch (mCalibration.coverageCalibration) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2322 | case Calibration::CoverageCalibration::BOX: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2323 | rawLeft = (in.toolMinor & 0xffff0000) >> 16; |
| 2324 | rawRight = in.toolMinor & 0x0000ffff; |
| 2325 | rawBottom = in.toolMajor & 0x0000ffff; |
| 2326 | rawTop = (in.toolMajor & 0xffff0000) >> 16; |
| 2327 | break; |
| 2328 | default: |
| 2329 | rawLeft = rawTop = rawRight = rawBottom = 0; |
| 2330 | break; |
| 2331 | } |
| 2332 | |
| 2333 | // Adjust X,Y coords for device calibration |
| 2334 | // TODO: Adjust coverage coords? |
| 2335 | float xTransformed = in.x, yTransformed = in.y; |
| 2336 | mAffineTransform.applyTo(xTransformed, yTransformed); |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 2337 | rotateAndScale(xTransformed, yTransformed); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2338 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2339 | // Adjust X, Y, and coverage coords for input device orientation. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2340 | float left, top, right, bottom; |
| 2341 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2342 | switch (mInputDeviceOrientation) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2343 | case DISPLAY_ORIENTATION_90: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2344 | left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale; |
| 2345 | right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale; |
| 2346 | bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale; |
| 2347 | top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2348 | orientation -= M_PI_2; |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2349 | if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2350 | orientation += |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2351 | (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2352 | } |
| 2353 | break; |
| 2354 | case DISPLAY_ORIENTATION_180: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2355 | left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale; |
| 2356 | right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale; |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2357 | bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale; |
| 2358 | top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2359 | orientation -= M_PI; |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2360 | if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2361 | orientation += |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2362 | (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2363 | } |
| 2364 | break; |
| 2365 | case DISPLAY_ORIENTATION_270: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2366 | left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale; |
| 2367 | right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale; |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2368 | bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale; |
| 2369 | top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2370 | orientation += M_PI_2; |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2371 | if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2372 | orientation -= |
Siarhei Vishniakou | 2421088 | 2022-07-15 09:42:04 -0700 | [diff] [blame] | 2373 | (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2374 | } |
| 2375 | break; |
| 2376 | default: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 2377 | left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale; |
| 2378 | right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale; |
| 2379 | bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale; |
| 2380 | top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | // Write output coords. |
| 2385 | PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i]; |
| 2386 | out.clear(); |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 2387 | out.setAxisValue(AMOTION_EVENT_AXIS_X, xTransformed); |
| 2388 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, yTransformed); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2389 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 2390 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 2391 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 2392 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 2393 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
| 2394 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
| 2395 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2396 | if (mCalibration.coverageCalibration == Calibration::CoverageCalibration::BOX) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2397 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left); |
| 2398 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top); |
| 2399 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right); |
| 2400 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom); |
| 2401 | } else { |
| 2402 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 2403 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 2404 | } |
| 2405 | |
Chris Ye | 364fdb5 | 2020-08-05 15:07:56 -0700 | [diff] [blame] | 2406 | // Write output relative fields if applicable. |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 2407 | uint32_t id = in.id; |
| 2408 | if (mSource == AINPUT_SOURCE_TOUCHPAD && |
| 2409 | mLastCookedState.cookedPointerData.hasPointerCoordsForId(id)) { |
| 2410 | const PointerCoords& p = mLastCookedState.cookedPointerData.pointerCoordsForId(id); |
| 2411 | float dx = xTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_X); |
| 2412 | float dy = yTransformed - p.getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 2413 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, dx); |
| 2414 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, dy); |
| 2415 | } |
| 2416 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2417 | // Write output properties. |
| 2418 | PointerProperties& properties = mCurrentCookedState.cookedPointerData.pointerProperties[i]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2419 | properties.clear(); |
| 2420 | properties.id = id; |
| 2421 | properties.toolType = in.toolType; |
| 2422 | |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 2423 | // Write id index and mark id as valid. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2424 | mCurrentCookedState.cookedPointerData.idToIndex[id] = i; |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 2425 | mCurrentCookedState.cookedPointerData.validIdBits.markBit(id); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2429 | std::list<NotifyArgs> TouchInputMapper::dispatchPointerUsage(nsecs_t when, nsecs_t readTime, |
| 2430 | uint32_t policyFlags, |
| 2431 | PointerUsage pointerUsage) { |
| 2432 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2433 | if (pointerUsage != mPointerUsage) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2434 | out += abortPointerUsage(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2435 | mPointerUsage = pointerUsage; |
| 2436 | } |
| 2437 | |
| 2438 | switch (mPointerUsage) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2439 | case PointerUsage::GESTURES: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2440 | out += dispatchPointerGestures(when, readTime, policyFlags, false /*isTimeout*/); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2441 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2442 | case PointerUsage::STYLUS: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2443 | out += dispatchPointerStylus(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2444 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2445 | case PointerUsage::MOUSE: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2446 | out += dispatchPointerMouse(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2447 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2448 | case PointerUsage::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2449 | break; |
| 2450 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2451 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2454 | std::list<NotifyArgs> TouchInputMapper::abortPointerUsage(nsecs_t when, nsecs_t readTime, |
| 2455 | uint32_t policyFlags) { |
| 2456 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2457 | switch (mPointerUsage) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2458 | case PointerUsage::GESTURES: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2459 | out += abortPointerGestures(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2460 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2461 | case PointerUsage::STYLUS: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2462 | out += abortPointerStylus(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2463 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2464 | case PointerUsage::MOUSE: |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2465 | out += abortPointerMouse(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2466 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2467 | case PointerUsage::NONE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2468 | break; |
| 2469 | } |
| 2470 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2471 | mPointerUsage = PointerUsage::NONE; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2472 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2475 | std::list<NotifyArgs> TouchInputMapper::dispatchPointerGestures(nsecs_t when, nsecs_t readTime, |
| 2476 | uint32_t policyFlags, |
| 2477 | bool isTimeout) { |
| 2478 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2479 | // Update current gesture coordinates. |
| 2480 | bool cancelPreviousGesture, finishPreviousGesture; |
| 2481 | bool sendEvents = |
| 2482 | preparePointerGestures(when, &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 2483 | if (!sendEvents) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2484 | return {}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2485 | } |
| 2486 | if (finishPreviousGesture) { |
| 2487 | cancelPreviousGesture = false; |
| 2488 | } |
| 2489 | |
| 2490 | // Update the pointer presentation and spots. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2491 | if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2492 | mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2493 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 2494 | mPointerController->clearSpots(); |
| 2495 | } |
| 2496 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2497 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) { |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 2498 | mPointerController->setSpots(mPointerGesture.currentGestureCoords.cbegin(), |
| 2499 | mPointerGesture.currentGestureIdToIndex.cbegin(), |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 2500 | mPointerGesture.currentGestureIdBits, |
| 2501 | mPointerController->getDisplayId()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2502 | } |
| 2503 | } else { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2504 | mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | // Show or hide the pointer if needed. |
| 2508 | switch (mPointerGesture.currentGestureMode) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2509 | case PointerGesture::Mode::NEUTRAL: |
| 2510 | case PointerGesture::Mode::QUIET: |
| 2511 | if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH && |
| 2512 | mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2513 | // Remind the user of where the pointer is after finishing a gesture with spots. |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2514 | mPointerController->unfade(PointerControllerInterface::Transition::GRADUAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2515 | } |
| 2516 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2517 | case PointerGesture::Mode::TAP: |
| 2518 | case PointerGesture::Mode::TAP_DRAG: |
| 2519 | case PointerGesture::Mode::BUTTON_CLICK_OR_DRAG: |
| 2520 | case PointerGesture::Mode::HOVER: |
| 2521 | case PointerGesture::Mode::PRESS: |
| 2522 | case PointerGesture::Mode::SWIPE: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2523 | // Unfade the pointer when the current gesture manipulates the |
| 2524 | // area directly under the pointer. |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2525 | mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2526 | break; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2527 | case PointerGesture::Mode::FREEFORM: |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2528 | // Fade the pointer when the current gesture manipulates a different |
| 2529 | // area and there are spots to guide the user experience. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2530 | if (mParameters.gestureMode == Parameters::GestureMode::MULTI_TOUCH) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2531 | mPointerController->fade(PointerControllerInterface::Transition::GRADUAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2532 | } else { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2533 | mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2534 | } |
| 2535 | break; |
| 2536 | } |
| 2537 | |
| 2538 | // Send events! |
| 2539 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 2540 | int32_t buttonState = mCurrentCookedState.buttonState; |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 2541 | const MotionClassification classification = |
| 2542 | mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE |
| 2543 | ? MotionClassification::TWO_FINGER_SWIPE |
| 2544 | : MotionClassification::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2545 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2546 | uint32_t flags = 0; |
| 2547 | |
| 2548 | if (!PointerGesture::canGestureAffectWindowFocus(mPointerGesture.currentGestureMode)) { |
| 2549 | flags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE; |
| 2550 | } |
| 2551 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2552 | // Update last coordinates of pointers that have moved so that we observe the new |
| 2553 | // pointer positions at the same time as other pointers that have just gone up. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2554 | bool down = mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP || |
| 2555 | mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG || |
| 2556 | mPointerGesture.currentGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG || |
| 2557 | mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS || |
| 2558 | mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE || |
| 2559 | mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2560 | bool moveNeeded = false; |
| 2561 | if (down && !cancelPreviousGesture && !finishPreviousGesture && |
| 2562 | !mPointerGesture.lastGestureIdBits.isEmpty() && |
| 2563 | !mPointerGesture.currentGestureIdBits.isEmpty()) { |
| 2564 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value & |
| 2565 | mPointerGesture.lastGestureIdBits.value); |
| 2566 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
| 2567 | mPointerGesture.currentGestureCoords, |
| 2568 | mPointerGesture.currentGestureIdToIndex, |
| 2569 | mPointerGesture.lastGestureProperties, |
| 2570 | mPointerGesture.lastGestureCoords, |
| 2571 | mPointerGesture.lastGestureIdToIndex, movedGestureIdBits); |
| 2572 | if (buttonState != mLastCookedState.buttonState) { |
| 2573 | moveNeeded = true; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | // Send motion events for all pointers that went up or were canceled. |
| 2578 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 2579 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 2580 | if (cancelPreviousGesture) { |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 2581 | const uint32_t cancelFlags = flags | AMOTION_EVENT_FLAG_CANCELED; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2582 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 2583 | AMOTION_EVENT_ACTION_CANCEL, 0, cancelFlags, metaState, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2584 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2585 | mPointerGesture.lastGestureProperties, |
| 2586 | mPointerGesture.lastGestureCoords, |
| 2587 | mPointerGesture.lastGestureIdToIndex, |
| 2588 | dispatchedGestureIdBits, -1, 0, 0, |
| 2589 | mPointerGesture.downTime, classification)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2590 | |
| 2591 | dispatchedGestureIdBits.clear(); |
| 2592 | } else { |
| 2593 | BitSet32 upGestureIdBits; |
| 2594 | if (finishPreviousGesture) { |
| 2595 | upGestureIdBits = dispatchedGestureIdBits; |
| 2596 | } else { |
| 2597 | upGestureIdBits.value = |
| 2598 | dispatchedGestureIdBits.value & ~mPointerGesture.currentGestureIdBits.value; |
| 2599 | } |
| 2600 | while (!upGestureIdBits.isEmpty()) { |
| 2601 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
| 2602 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2603 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2604 | AMOTION_EVENT_ACTION_POINTER_UP, 0, flags, metaState, |
| 2605 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2606 | mPointerGesture.lastGestureProperties, |
| 2607 | mPointerGesture.lastGestureCoords, |
| 2608 | mPointerGesture.lastGestureIdToIndex, |
| 2609 | dispatchedGestureIdBits, id, 0, 0, |
| 2610 | mPointerGesture.downTime, classification)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2611 | |
| 2612 | dispatchedGestureIdBits.clearBit(id); |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // Send motion events for all pointers that moved. |
| 2618 | if (moveNeeded) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2619 | out.push_back( |
| 2620 | dispatchMotion(when, readTime, policyFlags, mSource, AMOTION_EVENT_ACTION_MOVE, 0, |
| 2621 | flags, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2622 | mPointerGesture.currentGestureProperties, |
| 2623 | mPointerGesture.currentGestureCoords, |
| 2624 | mPointerGesture.currentGestureIdToIndex, dispatchedGestureIdBits, -1, |
| 2625 | 0, 0, mPointerGesture.downTime, classification)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | // Send motion events for all pointers that went down. |
| 2629 | if (down) { |
| 2630 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value & |
| 2631 | ~dispatchedGestureIdBits.value); |
| 2632 | while (!downGestureIdBits.isEmpty()) { |
| 2633 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
| 2634 | dispatchedGestureIdBits.markBit(id); |
| 2635 | |
| 2636 | if (dispatchedGestureIdBits.count() == 1) { |
| 2637 | mPointerGesture.downTime = when; |
| 2638 | } |
| 2639 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2640 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2641 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, flags, metaState, |
| 2642 | buttonState, 0, mPointerGesture.currentGestureProperties, |
| 2643 | mPointerGesture.currentGestureCoords, |
| 2644 | mPointerGesture.currentGestureIdToIndex, |
| 2645 | dispatchedGestureIdBits, id, 0, 0, |
| 2646 | mPointerGesture.downTime, classification)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | // Send motion events for hover. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2651 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::HOVER) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2652 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
| 2653 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState, |
| 2654 | buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2655 | mPointerGesture.currentGestureProperties, |
| 2656 | mPointerGesture.currentGestureCoords, |
| 2657 | mPointerGesture.currentGestureIdToIndex, |
| 2658 | mPointerGesture.currentGestureIdBits, -1, 0, 0, |
| 2659 | mPointerGesture.downTime, MotionClassification::NONE)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2660 | } else if (dispatchedGestureIdBits.isEmpty() && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 2661 | // Synthesize a hover move event after all pointers go up to indicate that |
| 2662 | // the pointer is hovering again even if the user is not currently touching |
| 2663 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 2664 | // event after a tap. |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 2665 | float x, y; |
| 2666 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2667 | |
| 2668 | PointerProperties pointerProperties; |
| 2669 | pointerProperties.clear(); |
| 2670 | pointerProperties.id = 0; |
| 2671 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 2672 | |
| 2673 | PointerCoords pointerCoords; |
| 2674 | pointerCoords.clear(); |
| 2675 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2676 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2677 | |
| 2678 | const int32_t displayId = mPointerController->getDisplayId(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2679 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 2680 | mSource, displayId, policyFlags, |
| 2681 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, flags, metaState, |
| 2682 | buttonState, MotionClassification::NONE, |
| 2683 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 2684 | &pointerCoords, 0, 0, x, y, mPointerGesture.downTime, |
| 2685 | /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | // Update state. |
| 2689 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 2690 | if (!down) { |
| 2691 | mPointerGesture.lastGestureIdBits.clear(); |
| 2692 | } else { |
| 2693 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 2694 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty();) { |
| 2695 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 2696 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
| 2697 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 2698 | mPointerGesture.currentGestureProperties[index]); |
| 2699 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 2700 | mPointerGesture.currentGestureCoords[index]); |
| 2701 | mPointerGesture.lastGestureIdToIndex[id] = index; |
| 2702 | } |
| 2703 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2704 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2707 | std::list<NotifyArgs> TouchInputMapper::abortPointerGestures(nsecs_t when, nsecs_t readTime, |
| 2708 | uint32_t policyFlags) { |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 2709 | const MotionClassification classification = |
| 2710 | mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE |
| 2711 | ? MotionClassification::TWO_FINGER_SWIPE |
| 2712 | : MotionClassification::NONE; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2713 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2714 | // Cancel previously dispatches pointers. |
| 2715 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 2716 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 2717 | int32_t buttonState = mCurrentRawState.buttonState; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2718 | out.push_back(dispatchMotion(when, readTime, policyFlags, mSource, |
Prabir Pradhan | f5b4d7a | 2022-10-03 15:45:50 +0000 | [diff] [blame] | 2719 | AMOTION_EVENT_ACTION_CANCEL, 0, AMOTION_EVENT_FLAG_CANCELED, |
| 2720 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2721 | mPointerGesture.lastGestureProperties, |
| 2722 | mPointerGesture.lastGestureCoords, |
| 2723 | mPointerGesture.lastGestureIdToIndex, |
| 2724 | mPointerGesture.lastGestureIdBits, -1, 0, 0, |
| 2725 | mPointerGesture.downTime, classification)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | // Reset the current pointer gesture. |
| 2729 | mPointerGesture.reset(); |
| 2730 | mPointerVelocityControl.reset(); |
| 2731 | |
| 2732 | // Remove any current spots. |
| 2733 | if (mPointerController != nullptr) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 2734 | mPointerController->fade(PointerControllerInterface::Transition::GRADUAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2735 | mPointerController->clearSpots(); |
| 2736 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 2737 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture, |
| 2741 | bool* outFinishPreviousGesture, bool isTimeout) { |
| 2742 | *outCancelPreviousGesture = false; |
| 2743 | *outFinishPreviousGesture = false; |
| 2744 | |
| 2745 | // Handle TAP timeout. |
| 2746 | if (isTimeout) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2747 | ALOGD_IF(DEBUG_GESTURES, "Gestures: Processing timeout"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2748 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2749 | if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2750 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
| 2751 | // The tap/drag timeout has not yet expired. |
| 2752 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime + |
| 2753 | mConfig.pointerGestureTapDragInterval); |
| 2754 | } else { |
| 2755 | // The tap is finished. |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2756 | ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP finished"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2757 | *outFinishPreviousGesture = true; |
| 2758 | |
| 2759 | mPointerGesture.activeGestureId = -1; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2760 | mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2761 | mPointerGesture.currentGestureIdBits.clear(); |
| 2762 | |
| 2763 | mPointerVelocityControl.reset(); |
| 2764 | return true; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | // We did not handle this timeout. |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
| 2772 | const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count(); |
| 2773 | const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count(); |
| 2774 | |
| 2775 | // Update the velocity tracker. |
| 2776 | { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 2777 | std::vector<float> positionsX; |
| 2778 | std::vector<float> positionsY; |
Siarhei Vishniakou | ae0f990 | 2020-09-14 19:23:31 -0500 | [diff] [blame] | 2779 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2780 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 2781 | const RawPointerData::Pointer& pointer = |
| 2782 | mCurrentRawState.rawPointerData.pointerForId(id); |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 2783 | positionsX.push_back(pointer.x * mPointerXMovementScale); |
| 2784 | positionsY.push_back(pointer.y * mPointerYMovementScale); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2785 | } |
| 2786 | mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 2787 | {{AMOTION_EVENT_AXIS_X, positionsX}, |
| 2788 | {AMOTION_EVENT_AXIS_Y, positionsY}}); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning |
| 2792 | // to NEUTRAL, then we should not generate tap event. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2793 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER && |
| 2794 | mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP && |
| 2795 | mPointerGesture.lastGestureMode != PointerGesture::Mode::TAP_DRAG) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2796 | mPointerGesture.resetTap(); |
| 2797 | } |
| 2798 | |
| 2799 | // Pick a new active touch id if needed. |
| 2800 | // Choose an arbitrary pointer that just went down, if there is one. |
| 2801 | // Otherwise choose an arbitrary remaining pointer. |
| 2802 | // This guarantees we always have an active touch id when there is at least one pointer. |
| 2803 | // We keep the same active touch id for as long as possible. |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2804 | if (mPointerGesture.activeTouchId < 0) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2805 | if (!mCurrentCookedState.fingerIdBits.isEmpty()) { |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2806 | mPointerGesture.activeTouchId = mCurrentCookedState.fingerIdBits.firstMarkedBit(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2807 | mPointerGesture.firstTouchTime = when; |
| 2808 | } |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2809 | } else if (!mCurrentCookedState.fingerIdBits.hasBit(mPointerGesture.activeTouchId)) { |
| 2810 | mPointerGesture.activeTouchId = !mCurrentCookedState.fingerIdBits.isEmpty() |
| 2811 | ? mCurrentCookedState.fingerIdBits.firstMarkedBit() |
| 2812 | : -1; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2813 | } |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2814 | const int32_t& activeTouchId = mPointerGesture.activeTouchId; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2815 | |
| 2816 | // Switch states based on button and pointer state. |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2817 | if (checkForTouchpadQuietTime(when)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2818 | // Case 1: Quiet time. (QUIET) |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2819 | ALOGD_IF(DEBUG_GESTURES, "Gestures: QUIET for next %0.3fms", |
| 2820 | (mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval - when) * |
| 2821 | 0.000001f); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2822 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::QUIET) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2823 | *outFinishPreviousGesture = true; |
| 2824 | } |
| 2825 | |
| 2826 | mPointerGesture.activeGestureId = -1; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2827 | mPointerGesture.currentGestureMode = PointerGesture::Mode::QUIET; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2828 | mPointerGesture.currentGestureIdBits.clear(); |
| 2829 | |
| 2830 | mPointerVelocityControl.reset(); |
| 2831 | } else if (isPointerDown(mCurrentRawState.buttonState)) { |
| 2832 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
| 2833 | // The pointer follows the active touch point. |
| 2834 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 2835 | // |
| 2836 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 2837 | // to handle the case where the user places a second finger on the touch pad |
| 2838 | // to apply the necessary force to depress an integrated button below the surface. |
| 2839 | // We don't want the second finger to be delivered to applications. |
| 2840 | // |
| 2841 | // For this to work well, we need to make sure to track the pointer that is really |
| 2842 | // active. If the user first puts one finger down to click then adds another |
| 2843 | // finger to drag then the active pointer should switch to the finger that is |
| 2844 | // being dragged. |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2845 | ALOGD_IF(DEBUG_GESTURES, |
| 2846 | "Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, currentFingerCount=%d", |
| 2847 | activeTouchId, currentFingerCount); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2848 | // Reset state when just starting. |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2849 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::BUTTON_CLICK_OR_DRAG) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2850 | *outFinishPreviousGesture = true; |
| 2851 | mPointerGesture.activeGestureId = 0; |
| 2852 | } |
| 2853 | |
| 2854 | // Switch pointers if needed. |
| 2855 | // Find the fastest pointer and follow it. |
| 2856 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2857 | const auto [bestId, bestSpeed] = getFastestFinger(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2858 | if (bestId >= 0 && bestId != activeTouchId) { |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 2859 | mPointerGesture.activeTouchId = bestId; |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2860 | ALOGD_IF(DEBUG_GESTURES, |
| 2861 | "Gestures: BUTTON_CLICK_OR_DRAG switched pointers, bestId=%d, " |
| 2862 | "bestSpeed=%0.3f", |
| 2863 | bestId, bestSpeed); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2867 | if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2868 | // When using spots, the click will occur at the position of the anchor |
| 2869 | // spot and all other spots will move there. |
Harry Cutts | 714d1ad | 2022-08-24 16:36:43 +0000 | [diff] [blame] | 2870 | moveMousePointerFromPointerDelta(when, activeTouchId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2871 | } else { |
| 2872 | mPointerVelocityControl.reset(); |
| 2873 | } |
| 2874 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 2875 | float x, y; |
| 2876 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2877 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2878 | mPointerGesture.currentGestureMode = PointerGesture::Mode::BUTTON_CLICK_OR_DRAG; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2879 | mPointerGesture.currentGestureIdBits.clear(); |
| 2880 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 2881 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 2882 | mPointerGesture.currentGestureProperties[0].clear(); |
| 2883 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 2884 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 2885 | mPointerGesture.currentGestureCoords[0].clear(); |
| 2886 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2887 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2888 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 2889 | } else if (currentFingerCount == 0) { |
| 2890 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2891 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::NEUTRAL) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2892 | *outFinishPreviousGesture = true; |
| 2893 | } |
| 2894 | |
| 2895 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
| 2896 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
| 2897 | bool tapped = false; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2898 | if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::HOVER || |
| 2899 | mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) && |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2900 | lastFingerCount == 1) { |
| 2901 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 2902 | float x, y; |
| 2903 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2904 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop && |
| 2905 | fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2906 | ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2907 | |
| 2908 | mPointerGesture.tapUpTime = when; |
| 2909 | getContext()->requestTimeoutAtTime(when + |
| 2910 | mConfig.pointerGestureTapDragInterval); |
| 2911 | |
| 2912 | mPointerGesture.activeGestureId = 0; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2913 | mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2914 | mPointerGesture.currentGestureIdBits.clear(); |
| 2915 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 2916 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 2917 | mPointerGesture.currentGestureProperties[0].clear(); |
| 2918 | mPointerGesture.currentGestureProperties[0].id = |
| 2919 | mPointerGesture.activeGestureId; |
| 2920 | mPointerGesture.currentGestureProperties[0].toolType = |
| 2921 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 2922 | mPointerGesture.currentGestureCoords[0].clear(); |
| 2923 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 2924 | mPointerGesture.tapX); |
| 2925 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 2926 | mPointerGesture.tapY); |
| 2927 | mPointerGesture.currentGestureCoords[0] |
| 2928 | .setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 2929 | |
| 2930 | tapped = true; |
| 2931 | } else { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2932 | ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
| 2933 | x - mPointerGesture.tapX, y - mPointerGesture.tapY); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2934 | } |
| 2935 | } else { |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 2936 | if (DEBUG_GESTURES) { |
| 2937 | if (mPointerGesture.tapDownTime != LLONG_MIN) { |
| 2938 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
| 2939 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
| 2940 | } else { |
| 2941 | ALOGD("Gestures: Not a TAP, incompatible mode transitions"); |
| 2942 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2943 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | mPointerVelocityControl.reset(); |
| 2948 | |
| 2949 | if (!tapped) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2950 | ALOGD_IF(DEBUG_GESTURES, "Gestures: NEUTRAL"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2951 | mPointerGesture.activeGestureId = -1; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2952 | mPointerGesture.currentGestureMode = PointerGesture::Mode::NEUTRAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2953 | mPointerGesture.currentGestureIdBits.clear(); |
| 2954 | } |
| 2955 | } else if (currentFingerCount == 1) { |
| 2956 | // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG) |
| 2957 | // The pointer follows the active touch point. |
| 2958 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 2959 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
| 2960 | ALOG_ASSERT(activeTouchId >= 0); |
| 2961 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2962 | mPointerGesture.currentGestureMode = PointerGesture::Mode::HOVER; |
| 2963 | if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2964 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 2965 | float x, y; |
| 2966 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2967 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop && |
| 2968 | fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2969 | mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2970 | } else { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2971 | ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
| 2972 | x - mPointerGesture.tapX, y - mPointerGesture.tapY); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2973 | } |
| 2974 | } else { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2975 | ALOGD_IF(DEBUG_GESTURES, "Gestures: Not a TAP_DRAG, %0.3fms time since up", |
| 2976 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2977 | } |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2978 | } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::TAP_DRAG) { |
| 2979 | mPointerGesture.currentGestureMode = PointerGesture::Mode::TAP_DRAG; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2980 | } |
| 2981 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2982 | if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2983 | // When using spots, the hover or drag will occur at the position of the anchor spot. |
Harry Cutts | 714d1ad | 2022-08-24 16:36:43 +0000 | [diff] [blame] | 2984 | moveMousePointerFromPointerDelta(when, activeTouchId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2985 | } else { |
| 2986 | mPointerVelocityControl.reset(); |
| 2987 | } |
| 2988 | |
| 2989 | bool down; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2990 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::TAP_DRAG) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2991 | ALOGD_IF(DEBUG_GESTURES, "Gestures: TAP_DRAG"); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2992 | down = true; |
| 2993 | } else { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 2994 | ALOGD_IF(DEBUG_GESTURES, "Gestures: HOVER"); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 2995 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::HOVER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 2996 | *outFinishPreviousGesture = true; |
| 2997 | } |
| 2998 | mPointerGesture.activeGestureId = 0; |
| 2999 | down = false; |
| 3000 | } |
| 3001 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3002 | float x, y; |
| 3003 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3004 | |
| 3005 | mPointerGesture.currentGestureIdBits.clear(); |
| 3006 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3007 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 3008 | mPointerGesture.currentGestureProperties[0].clear(); |
| 3009 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 3010 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 3011 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3012 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3013 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3014 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 3015 | down ? 1.0f : 0.0f); |
| 3016 | |
| 3017 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
| 3018 | mPointerGesture.resetTap(); |
| 3019 | mPointerGesture.tapDownTime = when; |
| 3020 | mPointerGesture.tapX = x; |
| 3021 | mPointerGesture.tapY = y; |
| 3022 | } |
| 3023 | } else { |
| 3024 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 3025 | prepareMultiFingerPointerGestures(when, outCancelPreviousGesture, outFinishPreviousGesture); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | mPointerController->setButtonState(mCurrentRawState.buttonState); |
| 3029 | |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 3030 | if (DEBUG_GESTURES) { |
| 3031 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
| 3032 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 3033 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
| 3034 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
| 3035 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 3036 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
| 3037 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty();) { |
| 3038 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3039 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
| 3040 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
| 3041 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
| 3042 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
| 3043 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 3044 | id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3045 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3046 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 3047 | } |
| 3048 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty();) { |
| 3049 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3050 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
| 3051 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
| 3052 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
| 3053 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
| 3054 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 3055 | id, index, properties.toolType, coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3056 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3057 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 3058 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3059 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3060 | return true; |
| 3061 | } |
| 3062 | |
Harry Cutts | bea6ce5 | 2022-10-14 15:17:30 +0000 | [diff] [blame] | 3063 | bool TouchInputMapper::checkForTouchpadQuietTime(nsecs_t when) { |
| 3064 | if (mPointerGesture.activeTouchId < 0) { |
| 3065 | mPointerGesture.resetQuietTime(); |
| 3066 | return false; |
| 3067 | } |
| 3068 | |
| 3069 | if (when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval) { |
| 3070 | return true; |
| 3071 | } |
| 3072 | |
| 3073 | const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count(); |
| 3074 | bool isQuietTime = false; |
| 3075 | if ((mPointerGesture.lastGestureMode == PointerGesture::Mode::PRESS || |
| 3076 | mPointerGesture.lastGestureMode == PointerGesture::Mode::SWIPE || |
| 3077 | mPointerGesture.lastGestureMode == PointerGesture::Mode::FREEFORM) && |
| 3078 | currentFingerCount < 2) { |
| 3079 | // Enter quiet time when exiting swipe or freeform state. |
| 3080 | // This is to prevent accidentally entering the hover state and flinging the |
| 3081 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 3082 | isQuietTime = true; |
| 3083 | } else if (mPointerGesture.lastGestureMode == PointerGesture::Mode::BUTTON_CLICK_OR_DRAG && |
| 3084 | currentFingerCount >= 2 && !isPointerDown(mCurrentRawState.buttonState)) { |
| 3085 | // Enter quiet time when releasing the button and there are still two or more |
| 3086 | // fingers down. This may indicate that one finger was used to press the button |
| 3087 | // but it has not gone up yet. |
| 3088 | isQuietTime = true; |
| 3089 | } |
| 3090 | if (isQuietTime) { |
| 3091 | mPointerGesture.quietTime = when; |
| 3092 | } |
| 3093 | return isQuietTime; |
| 3094 | } |
| 3095 | |
| 3096 | std::pair<int32_t, float> TouchInputMapper::getFastestFinger() { |
| 3097 | int32_t bestId = -1; |
| 3098 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
| 3099 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) { |
| 3100 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3101 | std::optional<float> vx = |
| 3102 | mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, id); |
| 3103 | std::optional<float> vy = |
| 3104 | mPointerGesture.velocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, id); |
| 3105 | if (vx && vy) { |
| 3106 | float speed = hypotf(*vx, *vy); |
| 3107 | if (speed > bestSpeed) { |
| 3108 | bestId = id; |
| 3109 | bestSpeed = speed; |
| 3110 | } |
| 3111 | } |
| 3112 | } |
| 3113 | return std::make_pair(bestId, bestSpeed); |
| 3114 | } |
| 3115 | |
| 3116 | void TouchInputMapper::prepareMultiFingerPointerGestures(nsecs_t when, bool* cancelPreviousGesture, |
| 3117 | bool* finishPreviousGesture) { |
| 3118 | // We need to provide feedback for each finger that goes down so we cannot wait for the fingers |
| 3119 | // to move before deciding what to do. |
| 3120 | // |
| 3121 | // The ambiguous case is deciding what to do when there are two fingers down but they have not |
| 3122 | // moved enough to determine whether they are part of a drag or part of a freeform gesture, or |
| 3123 | // just a press or long-press at the pointer location. |
| 3124 | // |
| 3125 | // When there are two fingers we start with the PRESS hypothesis and we generate a down at the |
| 3126 | // pointer location. |
| 3127 | // |
| 3128 | // When the two fingers move enough or when additional fingers are added, we make a decision to |
| 3129 | // transition into SWIPE or FREEFORM mode accordingly. |
| 3130 | const int32_t activeTouchId = mPointerGesture.activeTouchId; |
| 3131 | ALOG_ASSERT(activeTouchId >= 0); |
| 3132 | |
| 3133 | const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count(); |
| 3134 | const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count(); |
| 3135 | bool settled = |
| 3136 | when >= mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval; |
| 3137 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::PRESS && |
| 3138 | mPointerGesture.lastGestureMode != PointerGesture::Mode::SWIPE && |
| 3139 | mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) { |
| 3140 | *finishPreviousGesture = true; |
| 3141 | } else if (!settled && currentFingerCount > lastFingerCount) { |
| 3142 | // Additional pointers have gone down but not yet settled. |
| 3143 | // Reset the gesture. |
| 3144 | ALOGD_IF(DEBUG_GESTURES, |
| 3145 | "Gestures: Resetting gesture since additional pointers went down for " |
| 3146 | "MULTITOUCH, settle time remaining %0.3fms", |
| 3147 | (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval - |
| 3148 | when) * 0.000001f); |
| 3149 | *cancelPreviousGesture = true; |
| 3150 | } else { |
| 3151 | // Continue previous gesture. |
| 3152 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 3153 | } |
| 3154 | |
| 3155 | if (*finishPreviousGesture || *cancelPreviousGesture) { |
| 3156 | mPointerGesture.currentGestureMode = PointerGesture::Mode::PRESS; |
| 3157 | mPointerGesture.activeGestureId = 0; |
| 3158 | mPointerGesture.referenceIdBits.clear(); |
| 3159 | mPointerVelocityControl.reset(); |
| 3160 | |
| 3161 | // Use the centroid and pointer location as the reference points for the gesture. |
| 3162 | ALOGD_IF(DEBUG_GESTURES, |
| 3163 | "Gestures: Using centroid as reference for MULTITOUCH, settle time remaining " |
| 3164 | "%0.3fms", |
| 3165 | (mPointerGesture.firstTouchTime + mConfig.pointerGestureMultitouchSettleInterval - |
| 3166 | when) * 0.000001f); |
| 3167 | mCurrentRawState.rawPointerData |
| 3168 | .getCentroidOfTouchingPointers(&mPointerGesture.referenceTouchX, |
| 3169 | &mPointerGesture.referenceTouchY); |
| 3170 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 3171 | &mPointerGesture.referenceGestureY); |
| 3172 | } |
| 3173 | |
| 3174 | // Clear the reference deltas for fingers not yet included in the reference calculation. |
| 3175 | for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value & |
| 3176 | ~mPointerGesture.referenceIdBits.value); |
| 3177 | !idBits.isEmpty();) { |
| 3178 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3179 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 3180 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 3181 | } |
| 3182 | mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits; |
| 3183 | |
| 3184 | // Add delta for all fingers and calculate a common movement delta. |
| 3185 | int32_t commonDeltaRawX = 0, commonDeltaRawY = 0; |
| 3186 | BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value & |
| 3187 | mCurrentCookedState.fingerIdBits.value); |
| 3188 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty();) { |
| 3189 | bool first = (idBits == commonIdBits); |
| 3190 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3191 | const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id); |
| 3192 | const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id); |
| 3193 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 3194 | delta.dx += cpd.x - lpd.x; |
| 3195 | delta.dy += cpd.y - lpd.y; |
| 3196 | |
| 3197 | if (first) { |
| 3198 | commonDeltaRawX = delta.dx; |
| 3199 | commonDeltaRawY = delta.dy; |
| 3200 | } else { |
| 3201 | commonDeltaRawX = calculateCommonVector(commonDeltaRawX, delta.dx); |
| 3202 | commonDeltaRawY = calculateCommonVector(commonDeltaRawY, delta.dy); |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 3207 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS) { |
| 3208 | float dist[MAX_POINTER_ID + 1]; |
| 3209 | int32_t distOverThreshold = 0; |
| 3210 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) { |
| 3211 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3212 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 3213 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, delta.dy * mPointerYZoomScale); |
| 3214 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
| 3215 | distOverThreshold += 1; |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | // Only transition when at least two pointers have moved further than |
| 3220 | // the minimum distance threshold. |
| 3221 | if (distOverThreshold >= 2) { |
| 3222 | if (currentFingerCount > 2) { |
| 3223 | // There are more than two pointers, switch to FREEFORM. |
| 3224 | ALOGD_IF(DEBUG_GESTURES, |
| 3225 | "Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
| 3226 | currentFingerCount); |
| 3227 | *cancelPreviousGesture = true; |
| 3228 | mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM; |
| 3229 | } else { |
| 3230 | // There are exactly two pointers. |
| 3231 | BitSet32 idBits(mCurrentCookedState.fingerIdBits); |
| 3232 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 3233 | uint32_t id2 = idBits.firstMarkedBit(); |
| 3234 | const RawPointerData::Pointer& p1 = |
| 3235 | mCurrentRawState.rawPointerData.pointerForId(id1); |
| 3236 | const RawPointerData::Pointer& p2 = |
| 3237 | mCurrentRawState.rawPointerData.pointerForId(id2); |
| 3238 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 3239 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 3240 | // There are two pointers but they are too far apart for a SWIPE, |
| 3241 | // switch to FREEFORM. |
| 3242 | ALOGD_IF(DEBUG_GESTURES, |
| 3243 | "Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
| 3244 | mutualDistance, mPointerGestureMaxSwipeWidth); |
| 3245 | *cancelPreviousGesture = true; |
| 3246 | mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM; |
| 3247 | } else { |
| 3248 | // There are two pointers. Wait for both pointers to start moving |
| 3249 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 3250 | float dist1 = dist[id1]; |
| 3251 | float dist2 = dist[id2]; |
| 3252 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance && |
| 3253 | dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 3254 | // Calculate the dot product of the displacement vectors. |
| 3255 | // When the vectors are oriented in approximately the same direction, |
| 3256 | // the angle betweeen them is near zero and the cosine of the angle |
| 3257 | // approaches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * |
| 3258 | // mag(v2). |
| 3259 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 3260 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
| 3261 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 3262 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 3263 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 3264 | float dy2 = delta2.dy * mPointerYZoomScale; |
| 3265 | float dot = dx1 * dx2 + dy1 * dy2; |
| 3266 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 3267 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 3268 | // Pointers are moving in the same direction. Switch to SWIPE. |
| 3269 | ALOGD_IF(DEBUG_GESTURES, |
| 3270 | "Gestures: PRESS transitioned to SWIPE, " |
| 3271 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 3272 | "cosine %0.3f >= %0.3f", |
| 3273 | dist1, mConfig.pointerGestureMultitouchMinDistance, dist2, |
| 3274 | mConfig.pointerGestureMultitouchMinDistance, cosine, |
| 3275 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 3276 | mPointerGesture.currentGestureMode = PointerGesture::Mode::SWIPE; |
| 3277 | } else { |
| 3278 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 3279 | ALOGD_IF(DEBUG_GESTURES, |
| 3280 | "Gestures: PRESS transitioned to FREEFORM, " |
| 3281 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 3282 | "cosine %0.3f < %0.3f", |
| 3283 | dist1, mConfig.pointerGestureMultitouchMinDistance, dist2, |
| 3284 | mConfig.pointerGestureMultitouchMinDistance, cosine, |
| 3285 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 3286 | *cancelPreviousGesture = true; |
| 3287 | mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM; |
| 3288 | } |
| 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) { |
| 3294 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 3295 | // Cancel previous gesture. |
| 3296 | if (currentFingerCount > 2) { |
| 3297 | ALOGD_IF(DEBUG_GESTURES, |
| 3298 | "Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
| 3299 | currentFingerCount); |
| 3300 | *cancelPreviousGesture = true; |
| 3301 | mPointerGesture.currentGestureMode = PointerGesture::Mode::FREEFORM; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | // Move the reference points based on the overall group motion of the fingers |
| 3306 | // except in PRESS mode while waiting for a transition to occur. |
| 3307 | if (mPointerGesture.currentGestureMode != PointerGesture::Mode::PRESS && |
| 3308 | (commonDeltaRawX || commonDeltaRawY)) { |
| 3309 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty();) { |
| 3310 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3311 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 3312 | delta.dx = 0; |
| 3313 | delta.dy = 0; |
| 3314 | } |
| 3315 | |
| 3316 | mPointerGesture.referenceTouchX += commonDeltaRawX; |
| 3317 | mPointerGesture.referenceTouchY += commonDeltaRawY; |
| 3318 | |
| 3319 | float commonDeltaX = commonDeltaRawX * mPointerXMovementScale; |
| 3320 | float commonDeltaY = commonDeltaRawY * mPointerYMovementScale; |
| 3321 | |
| 3322 | rotateDelta(mInputDeviceOrientation, &commonDeltaX, &commonDeltaY); |
| 3323 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
| 3324 | |
| 3325 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 3326 | mPointerGesture.referenceGestureY += commonDeltaY; |
| 3327 | } |
| 3328 | |
| 3329 | // Report gestures. |
| 3330 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::PRESS || |
| 3331 | mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) { |
| 3332 | // PRESS or SWIPE mode. |
| 3333 | ALOGD_IF(DEBUG_GESTURES, |
| 3334 | "Gestures: PRESS or SWIPE activeTouchId=%d, activeGestureId=%d, " |
| 3335 | "currentTouchPointerCount=%d", |
| 3336 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
| 3337 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
| 3338 | |
| 3339 | mPointerGesture.currentGestureIdBits.clear(); |
| 3340 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3341 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
| 3342 | mPointerGesture.currentGestureProperties[0].clear(); |
| 3343 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 3344 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 3345 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3346 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3347 | mPointerGesture.referenceGestureX); |
| 3348 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 3349 | mPointerGesture.referenceGestureY); |
| 3350 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 3351 | if (mPointerGesture.currentGestureMode == PointerGesture::Mode::SWIPE) { |
| 3352 | float xOffset = static_cast<float>(commonDeltaRawX) / |
| 3353 | (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue); |
| 3354 | float yOffset = static_cast<float>(commonDeltaRawY) / |
| 3355 | (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue); |
| 3356 | mPointerGesture.currentGestureCoords[0] |
| 3357 | .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET, xOffset); |
| 3358 | mPointerGesture.currentGestureCoords[0] |
| 3359 | .setAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET, yOffset); |
| 3360 | } |
| 3361 | } else if (mPointerGesture.currentGestureMode == PointerGesture::Mode::FREEFORM) { |
| 3362 | // FREEFORM mode. |
| 3363 | ALOGD_IF(DEBUG_GESTURES, |
| 3364 | "Gestures: FREEFORM activeTouchId=%d, activeGestureId=%d, " |
| 3365 | "currentTouchPointerCount=%d", |
| 3366 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
| 3367 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
| 3368 | |
| 3369 | mPointerGesture.currentGestureIdBits.clear(); |
| 3370 | |
| 3371 | BitSet32 mappedTouchIdBits; |
| 3372 | BitSet32 usedGestureIdBits; |
| 3373 | if (mPointerGesture.lastGestureMode != PointerGesture::Mode::FREEFORM) { |
| 3374 | // Initially, assign the active gesture id to the active touch point |
| 3375 | // if there is one. No other touch id bits are mapped yet. |
| 3376 | if (!*cancelPreviousGesture) { |
| 3377 | mappedTouchIdBits.markBit(activeTouchId); |
| 3378 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3379 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 3380 | mPointerGesture.activeGestureId; |
| 3381 | } else { |
| 3382 | mPointerGesture.activeGestureId = -1; |
| 3383 | } |
| 3384 | } else { |
| 3385 | // Otherwise, assume we mapped all touches from the previous frame. |
| 3386 | // Reuse all mappings that are still applicable. |
| 3387 | mappedTouchIdBits.value = |
| 3388 | mLastCookedState.fingerIdBits.value & mCurrentCookedState.fingerIdBits.value; |
| 3389 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 3390 | |
| 3391 | // Check whether we need to choose a new active gesture id because the |
| 3392 | // current went went up. |
| 3393 | for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value & |
| 3394 | ~mCurrentCookedState.fingerIdBits.value); |
| 3395 | !upTouchIdBits.isEmpty();) { |
| 3396 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
| 3397 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 3398 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 3399 | mPointerGesture.activeGestureId = -1; |
| 3400 | break; |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | ALOGD_IF(DEBUG_GESTURES, |
| 3406 | "Gestures: FREEFORM follow up mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 3407 | "activeGestureId=%d", |
| 3408 | mappedTouchIdBits.value, usedGestureIdBits.value, mPointerGesture.activeGestureId); |
| 3409 | |
| 3410 | BitSet32 idBits(mCurrentCookedState.fingerIdBits); |
| 3411 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
| 3412 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
| 3413 | uint32_t gestureId; |
| 3414 | if (!mappedTouchIdBits.hasBit(touchId)) { |
| 3415 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
| 3416 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 3417 | ALOGD_IF(DEBUG_GESTURES, |
| 3418 | "Gestures: FREEFORM new mapping for touch id %d -> gesture id %d", touchId, |
| 3419 | gestureId); |
| 3420 | } else { |
| 3421 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 3422 | ALOGD_IF(DEBUG_GESTURES, |
| 3423 | "Gestures: FREEFORM existing mapping for touch id %d -> gesture id %d", |
| 3424 | touchId, gestureId); |
| 3425 | } |
| 3426 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 3427 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 3428 | |
| 3429 | const RawPointerData::Pointer& pointer = |
| 3430 | mCurrentRawState.rawPointerData.pointerForId(touchId); |
| 3431 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) * mPointerXZoomScale; |
| 3432 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) * mPointerYZoomScale; |
| 3433 | rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY); |
| 3434 | |
| 3435 | mPointerGesture.currentGestureProperties[i].clear(); |
| 3436 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 3437 | mPointerGesture.currentGestureProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 3438 | mPointerGesture.currentGestureCoords[i].clear(); |
| 3439 | mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3440 | mPointerGesture.referenceGestureX + |
| 3441 | deltaX); |
| 3442 | mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 3443 | mPointerGesture.referenceGestureY + |
| 3444 | deltaY); |
| 3445 | mPointerGesture.currentGestureCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 3446 | } |
| 3447 | |
| 3448 | if (mPointerGesture.activeGestureId < 0) { |
| 3449 | mPointerGesture.activeGestureId = mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 3450 | ALOGD_IF(DEBUG_GESTURES, "Gestures: FREEFORM new activeGestureId=%d", |
| 3451 | mPointerGesture.activeGestureId); |
| 3452 | } |
| 3453 | } |
| 3454 | } |
| 3455 | |
Harry Cutts | 714d1ad | 2022-08-24 16:36:43 +0000 | [diff] [blame] | 3456 | void TouchInputMapper::moveMousePointerFromPointerDelta(nsecs_t when, uint32_t pointerId) { |
| 3457 | const RawPointerData::Pointer& currentPointer = |
| 3458 | mCurrentRawState.rawPointerData.pointerForId(pointerId); |
| 3459 | const RawPointerData::Pointer& lastPointer = |
| 3460 | mLastRawState.rawPointerData.pointerForId(pointerId); |
| 3461 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 3462 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
| 3463 | |
| 3464 | rotateDelta(mInputDeviceOrientation, &deltaX, &deltaY); |
| 3465 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 3466 | |
| 3467 | mPointerController->move(deltaX, deltaY); |
| 3468 | } |
| 3469 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3470 | std::list<NotifyArgs> TouchInputMapper::dispatchPointerStylus(nsecs_t when, nsecs_t readTime, |
| 3471 | uint32_t policyFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3472 | mPointerSimple.currentCoords.clear(); |
| 3473 | mPointerSimple.currentProperties.clear(); |
| 3474 | |
| 3475 | bool down, hovering; |
| 3476 | if (!mCurrentCookedState.stylusIdBits.isEmpty()) { |
| 3477 | uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit(); |
| 3478 | uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id]; |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3479 | mPointerController |
| 3480 | ->setPosition(mCurrentCookedState.cookedPointerData.pointerCoords[index].getX(), |
| 3481 | mCurrentCookedState.cookedPointerData.pointerCoords[index].getY()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3482 | |
| 3483 | hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id); |
| 3484 | down = !hovering; |
| 3485 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3486 | float x, y; |
| 3487 | mPointerController->getPosition(&x, &y); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3488 | mPointerSimple.currentCoords.copyFrom( |
| 3489 | mCurrentCookedState.cookedPointerData.pointerCoords[index]); |
| 3490 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3491 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3492 | mPointerSimple.currentProperties.id = 0; |
| 3493 | mPointerSimple.currentProperties.toolType = |
| 3494 | mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType; |
| 3495 | } else { |
| 3496 | down = false; |
| 3497 | hovering = false; |
| 3498 | } |
| 3499 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3500 | return dispatchPointerSimple(when, readTime, policyFlags, down, hovering); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3503 | std::list<NotifyArgs> TouchInputMapper::abortPointerStylus(nsecs_t when, nsecs_t readTime, |
| 3504 | uint32_t policyFlags) { |
| 3505 | return abortPointerSimple(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3508 | std::list<NotifyArgs> TouchInputMapper::dispatchPointerMouse(nsecs_t when, nsecs_t readTime, |
| 3509 | uint32_t policyFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3510 | mPointerSimple.currentCoords.clear(); |
| 3511 | mPointerSimple.currentProperties.clear(); |
| 3512 | |
| 3513 | bool down, hovering; |
| 3514 | if (!mCurrentCookedState.mouseIdBits.isEmpty()) { |
| 3515 | uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3516 | if (mLastCookedState.mouseIdBits.hasBit(id)) { |
Harry Cutts | 714d1ad | 2022-08-24 16:36:43 +0000 | [diff] [blame] | 3517 | moveMousePointerFromPointerDelta(when, id); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3518 | } else { |
| 3519 | mPointerVelocityControl.reset(); |
| 3520 | } |
| 3521 | |
| 3522 | down = isPointerDown(mCurrentRawState.buttonState); |
| 3523 | hovering = !down; |
| 3524 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3525 | float x, y; |
| 3526 | mPointerController->getPosition(&x, &y); |
Harry Cutts | 714d1ad | 2022-08-24 16:36:43 +0000 | [diff] [blame] | 3527 | uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3528 | mPointerSimple.currentCoords.copyFrom( |
| 3529 | mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]); |
| 3530 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3531 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3532 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 3533 | hovering ? 0.0f : 1.0f); |
| 3534 | mPointerSimple.currentProperties.id = 0; |
| 3535 | mPointerSimple.currentProperties.toolType = |
| 3536 | mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType; |
| 3537 | } else { |
| 3538 | mPointerVelocityControl.reset(); |
| 3539 | |
| 3540 | down = false; |
| 3541 | hovering = false; |
| 3542 | } |
| 3543 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3544 | return dispatchPointerSimple(when, readTime, policyFlags, down, hovering); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3545 | } |
| 3546 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3547 | std::list<NotifyArgs> TouchInputMapper::abortPointerMouse(nsecs_t when, nsecs_t readTime, |
| 3548 | uint32_t policyFlags) { |
| 3549 | std::list<NotifyArgs> out = abortPointerSimple(when, readTime, policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3550 | |
| 3551 | mPointerVelocityControl.reset(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3552 | |
| 3553 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3556 | std::list<NotifyArgs> TouchInputMapper::dispatchPointerSimple(nsecs_t when, nsecs_t readTime, |
| 3557 | uint32_t policyFlags, bool down, |
| 3558 | bool hovering) { |
| 3559 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3560 | int32_t metaState = getContext()->getGlobalMetaState(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3561 | |
| 3562 | if (down || hovering) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 3563 | mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3564 | mPointerController->clearSpots(); |
| 3565 | mPointerController->setButtonState(mCurrentRawState.buttonState); |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 3566 | mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3567 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame] | 3568 | mPointerController->fade(PointerControllerInterface::Transition::GRADUAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3569 | } |
Garfield Tan | 9514d78 | 2020-11-10 16:37:23 -0800 | [diff] [blame] | 3570 | int32_t displayId = mPointerController->getDisplayId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3571 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3572 | float xCursorPosition, yCursorPosition; |
| 3573 | mPointerController->getPosition(&xCursorPosition, &yCursorPosition); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3574 | |
| 3575 | if (mPointerSimple.down && !down) { |
| 3576 | mPointerSimple.down = false; |
| 3577 | |
| 3578 | // Send up. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3579 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3580 | mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_UP, 0, |
| 3581 | 0, metaState, mLastRawState.buttonState, |
| 3582 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3583 | &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 3584 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, |
| 3585 | yCursorPosition, mPointerSimple.downTime, |
| 3586 | /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | if (mPointerSimple.hovering && !hovering) { |
| 3590 | mPointerSimple.hovering = false; |
| 3591 | |
| 3592 | // Send hover exit. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3593 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3594 | mSource, displayId, policyFlags, |
| 3595 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState, |
| 3596 | mLastRawState.buttonState, MotionClassification::NONE, |
| 3597 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3598 | &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 3599 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, |
| 3600 | yCursorPosition, mPointerSimple.downTime, |
| 3601 | /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | if (down) { |
| 3605 | if (!mPointerSimple.down) { |
| 3606 | mPointerSimple.down = true; |
| 3607 | mPointerSimple.downTime = when; |
| 3608 | |
| 3609 | // Send down. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3610 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3611 | mSource, displayId, policyFlags, |
| 3612 | AMOTION_EVENT_ACTION_DOWN, 0, 0, metaState, |
| 3613 | mCurrentRawState.buttonState, MotionClassification::NONE, |
| 3614 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3615 | &mPointerSimple.currentProperties, |
| 3616 | &mPointerSimple.currentCoords, mOrientedXPrecision, |
| 3617 | mOrientedYPrecision, xCursorPosition, yCursorPosition, |
| 3618 | mPointerSimple.downTime, /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | // Send move. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3622 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3623 | mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_MOVE, |
| 3624 | 0, 0, metaState, mCurrentRawState.buttonState, |
| 3625 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3626 | &mPointerSimple.currentProperties, |
| 3627 | &mPointerSimple.currentCoords, mOrientedXPrecision, |
| 3628 | mOrientedYPrecision, xCursorPosition, yCursorPosition, |
| 3629 | mPointerSimple.downTime, /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | if (hovering) { |
| 3633 | if (!mPointerSimple.hovering) { |
| 3634 | mPointerSimple.hovering = true; |
| 3635 | |
| 3636 | // Send hover enter. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3637 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3638 | mSource, displayId, policyFlags, |
| 3639 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState, |
| 3640 | mCurrentRawState.buttonState, MotionClassification::NONE, |
| 3641 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3642 | &mPointerSimple.currentProperties, |
| 3643 | &mPointerSimple.currentCoords, mOrientedXPrecision, |
| 3644 | mOrientedYPrecision, xCursorPosition, yCursorPosition, |
| 3645 | mPointerSimple.downTime, /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | // Send hover move. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3649 | out.push_back( |
| 3650 | NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource, |
| 3651 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, |
| 3652 | metaState, mCurrentRawState.buttonState, |
| 3653 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3654 | &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 3655 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, |
| 3656 | yCursorPosition, mPointerSimple.downTime, /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) { |
| 3660 | float vscroll = mCurrentRawState.rawVScroll; |
| 3661 | float hscroll = mCurrentRawState.rawHScroll; |
| 3662 | mWheelYVelocityControl.move(when, nullptr, &vscroll); |
| 3663 | mWheelXVelocityControl.move(when, &hscroll, nullptr); |
| 3664 | |
| 3665 | // Send scroll. |
| 3666 | PointerCoords pointerCoords; |
| 3667 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 3668 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 3669 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 3670 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3671 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 3672 | mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, |
| 3673 | 0, 0, metaState, mCurrentRawState.buttonState, |
| 3674 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 3675 | &mPointerSimple.currentProperties, &pointerCoords, |
| 3676 | mOrientedXPrecision, mOrientedYPrecision, xCursorPosition, |
| 3677 | yCursorPosition, mPointerSimple.downTime, |
| 3678 | /* videoFrames */ {})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | // Save state. |
| 3682 | if (down || hovering) { |
| 3683 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 3684 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 3685 | } else { |
| 3686 | mPointerSimple.reset(); |
| 3687 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3688 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3689 | } |
| 3690 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3691 | std::list<NotifyArgs> TouchInputMapper::abortPointerSimple(nsecs_t when, nsecs_t readTime, |
| 3692 | uint32_t policyFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3693 | mPointerSimple.currentCoords.clear(); |
| 3694 | mPointerSimple.currentProperties.clear(); |
| 3695 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3696 | return dispatchPointerSimple(when, readTime, policyFlags, false, false); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3699 | NotifyMotionArgs TouchInputMapper::dispatchMotion( |
| 3700 | nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, int32_t action, |
| 3701 | int32_t actionButton, int32_t flags, int32_t metaState, int32_t buttonState, |
Prabir Pradhan | d6ccedb | 2022-09-27 21:04:06 +0000 | [diff] [blame] | 3702 | int32_t edgeFlags, const PropertiesArray& properties, const CoordsArray& coords, |
| 3703 | const IdToIndexArray& idToIndex, BitSet32 idBits, int32_t changedId, float xPrecision, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3704 | float yPrecision, nsecs_t downTime, MotionClassification classification) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3705 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 3706 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 3707 | uint32_t pointerCount = 0; |
| 3708 | while (!idBits.isEmpty()) { |
| 3709 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3710 | uint32_t index = idToIndex[id]; |
| 3711 | pointerProperties[pointerCount].copyFrom(properties[index]); |
| 3712 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 3713 | |
| 3714 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 3715 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 3716 | } |
| 3717 | |
| 3718 | pointerCount += 1; |
| 3719 | } |
| 3720 | |
| 3721 | ALOG_ASSERT(pointerCount != 0); |
| 3722 | |
| 3723 | if (changedId >= 0 && pointerCount == 1) { |
| 3724 | // Replace initial down and final up action. |
| 3725 | // We can compare the action without masking off the changed pointer index |
| 3726 | // because we know the index is 0. |
| 3727 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 3728 | action = AMOTION_EVENT_ACTION_DOWN; |
| 3729 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 3730 | if ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) { |
| 3731 | action = AMOTION_EVENT_ACTION_CANCEL; |
| 3732 | } else { |
| 3733 | action = AMOTION_EVENT_ACTION_UP; |
| 3734 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3735 | } else { |
| 3736 | // Can't happen. |
| 3737 | ALOG_ASSERT(false); |
| 3738 | } |
| 3739 | } |
| 3740 | float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; |
| 3741 | float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 3742 | if (mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 3743 | mPointerController->getPosition(&xCursorPosition, &yCursorPosition); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3744 | } |
| 3745 | const int32_t displayId = getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE); |
| 3746 | const int32_t deviceId = getDeviceId(); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 3747 | std::vector<TouchVideoFrame> frames = getDeviceContext().getVideoFrames(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3748 | std::for_each(frames.begin(), frames.end(), |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3749 | [this](TouchVideoFrame& frame) { frame.rotate(this->mInputDeviceOrientation); }); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3750 | return NotifyMotionArgs(getContext()->getNextId(), when, readTime, deviceId, source, displayId, |
| 3751 | policyFlags, action, actionButton, flags, metaState, buttonState, |
| 3752 | classification, edgeFlags, pointerCount, pointerProperties, |
| 3753 | pointerCoords, xPrecision, yPrecision, xCursorPosition, yCursorPosition, |
| 3754 | downTime, std::move(frames)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 3757 | std::list<NotifyArgs> TouchInputMapper::cancelTouch(nsecs_t when, nsecs_t readTime) { |
| 3758 | std::list<NotifyArgs> out; |
| 3759 | out += abortPointerUsage(when, readTime, 0 /*policyFlags*/); |
| 3760 | out += abortTouches(when, readTime, 0 /* policyFlags*/); |
| 3761 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3764 | // Transform input device coordinates to display panel coordinates. |
| 3765 | void TouchInputMapper::rotateAndScale(float& x, float& y) const { |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3766 | const float xScaled = float(x - mRawPointerAxes.x.minValue) * mXScale; |
| 3767 | const float yScaled = float(y - mRawPointerAxes.y.minValue) * mYScale; |
| 3768 | |
arthurhung | a36b28e | 2020-12-29 20:28:15 +0800 | [diff] [blame] | 3769 | const float xScaledMax = float(mRawPointerAxes.x.maxValue - x) * mXScale; |
| 3770 | const float yScaledMax = float(mRawPointerAxes.y.maxValue - y) * mYScale; |
| 3771 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3772 | // Rotate to display coordinate. |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3773 | // 0 - no swap and reverse. |
| 3774 | // 90 - swap x/y and reverse y. |
| 3775 | // 180 - reverse x, y. |
| 3776 | // 270 - swap x/y and reverse x. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3777 | switch (mInputDeviceOrientation) { |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3778 | case DISPLAY_ORIENTATION_0: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3779 | x = xScaled; |
| 3780 | y = yScaled; |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3781 | break; |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 3782 | case DISPLAY_ORIENTATION_90: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3783 | y = xScaledMax; |
| 3784 | x = yScaled; |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 3785 | break; |
| 3786 | case DISPLAY_ORIENTATION_180: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3787 | x = xScaledMax; |
| 3788 | y = yScaledMax; |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 3789 | break; |
| 3790 | case DISPLAY_ORIENTATION_270: |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3791 | y = xScaled; |
| 3792 | x = yScaledMax; |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 3793 | break; |
| 3794 | default: |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3795 | assert(false); |
Arthur Hung | 05de577 | 2019-09-26 18:31:26 +0800 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame] | 3799 | bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const { |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 3800 | const float xScaled = (x - mRawPointerAxes.x.minValue) * mXScale; |
| 3801 | const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale; |
| 3802 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3803 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue && |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 3804 | xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) && |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3805 | y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue && |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 3806 | yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) { |
| 3810 | for (const VirtualKey& virtualKey : mVirtualKeys) { |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 3811 | ALOGD_IF(DEBUG_VIRTUAL_KEYS, |
| 3812 | "VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 3813 | "left=%d, top=%d, right=%d, bottom=%d", |
| 3814 | x, y, virtualKey.keyCode, virtualKey.scanCode, virtualKey.hitLeft, |
| 3815 | virtualKey.hitTop, virtualKey.hitRight, virtualKey.hitBottom); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3816 | |
| 3817 | if (virtualKey.isHit(x, y)) { |
| 3818 | return &virtualKey; |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | return nullptr; |
| 3823 | } |
| 3824 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3825 | void TouchInputMapper::assignPointerIds(const RawState& last, RawState& current) { |
| 3826 | uint32_t currentPointerCount = current.rawPointerData.pointerCount; |
| 3827 | uint32_t lastPointerCount = last.rawPointerData.pointerCount; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3828 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3829 | current.rawPointerData.clearIdBits(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3830 | |
| 3831 | if (currentPointerCount == 0) { |
| 3832 | // No pointers to assign. |
| 3833 | return; |
| 3834 | } |
| 3835 | |
| 3836 | if (lastPointerCount == 0) { |
| 3837 | // All pointers are new. |
| 3838 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 3839 | uint32_t id = i; |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3840 | current.rawPointerData.pointers[i].id = id; |
| 3841 | current.rawPointerData.idToIndex[id] = i; |
| 3842 | current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(i)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3843 | } |
| 3844 | return; |
| 3845 | } |
| 3846 | |
| 3847 | if (currentPointerCount == 1 && lastPointerCount == 1 && |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3848 | current.rawPointerData.pointers[0].toolType == last.rawPointerData.pointers[0].toolType) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3849 | // Only one pointer and no change in count so it must have the same id as before. |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3850 | uint32_t id = last.rawPointerData.pointers[0].id; |
| 3851 | current.rawPointerData.pointers[0].id = id; |
| 3852 | current.rawPointerData.idToIndex[id] = 0; |
| 3853 | current.rawPointerData.markIdBit(id, current.rawPointerData.isHovering(0)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3854 | return; |
| 3855 | } |
| 3856 | |
| 3857 | // General case. |
| 3858 | // We build a heap of squared euclidean distances between current and last pointers |
| 3859 | // associated with the current and last pointer indices. Then, we find the best |
| 3860 | // match (by distance) for each current pointer. |
| 3861 | // The pointers must have the same tool type but it is possible for them to |
| 3862 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 3863 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 3864 | |
| 3865 | uint32_t heapSize = 0; |
| 3866 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 3867 | currentPointerIndex++) { |
| 3868 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 3869 | lastPointerIndex++) { |
| 3870 | const RawPointerData::Pointer& currentPointer = |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3871 | current.rawPointerData.pointers[currentPointerIndex]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3872 | const RawPointerData::Pointer& lastPointer = |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3873 | last.rawPointerData.pointers[lastPointerIndex]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3874 | if (currentPointer.toolType == lastPointer.toolType) { |
| 3875 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 3876 | int64_t deltaY = currentPointer.y - lastPointer.y; |
| 3877 | |
| 3878 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 3879 | |
| 3880 | // Insert new element into the heap (sift up). |
| 3881 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 3882 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 3883 | heap[heapSize].distance = distance; |
| 3884 | heapSize += 1; |
| 3885 | } |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | // Heapify |
| 3890 | for (uint32_t startIndex = heapSize / 2; startIndex != 0;) { |
| 3891 | startIndex -= 1; |
| 3892 | for (uint32_t parentIndex = startIndex;;) { |
| 3893 | uint32_t childIndex = parentIndex * 2 + 1; |
| 3894 | if (childIndex >= heapSize) { |
| 3895 | break; |
| 3896 | } |
| 3897 | |
| 3898 | if (childIndex + 1 < heapSize && |
| 3899 | heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 3900 | childIndex += 1; |
| 3901 | } |
| 3902 | |
| 3903 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 3904 | break; |
| 3905 | } |
| 3906 | |
| 3907 | swap(heap[parentIndex], heap[childIndex]); |
| 3908 | parentIndex = childIndex; |
| 3909 | } |
| 3910 | } |
| 3911 | |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 3912 | if (DEBUG_POINTER_ASSIGNMENT) { |
| 3913 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
| 3914 | for (size_t i = 0; i < heapSize; i++) { |
| 3915 | ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, i, |
| 3916 | heap[i].currentPointerIndex, heap[i].lastPointerIndex, heap[i].distance); |
| 3917 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3918 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3919 | |
| 3920 | // Pull matches out by increasing order of distance. |
| 3921 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 3922 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 3923 | // It also tracks the used pointer id bits. |
| 3924 | BitSet32 matchedLastBits(0); |
| 3925 | BitSet32 matchedCurrentBits(0); |
| 3926 | BitSet32 usedIdBits(0); |
| 3927 | bool first = true; |
| 3928 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 3929 | while (heapSize > 0) { |
| 3930 | if (first) { |
| 3931 | // The first time through the loop, we just consume the root element of |
| 3932 | // the heap (the one with smallest distance). |
| 3933 | first = false; |
| 3934 | } else { |
| 3935 | // Previous iterations consumed the root element of the heap. |
| 3936 | // Pop root element off of the heap (sift down). |
| 3937 | heap[0] = heap[heapSize]; |
| 3938 | for (uint32_t parentIndex = 0;;) { |
| 3939 | uint32_t childIndex = parentIndex * 2 + 1; |
| 3940 | if (childIndex >= heapSize) { |
| 3941 | break; |
| 3942 | } |
| 3943 | |
| 3944 | if (childIndex + 1 < heapSize && |
| 3945 | heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 3946 | childIndex += 1; |
| 3947 | } |
| 3948 | |
| 3949 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 3950 | break; |
| 3951 | } |
| 3952 | |
| 3953 | swap(heap[parentIndex], heap[childIndex]); |
| 3954 | parentIndex = childIndex; |
| 3955 | } |
| 3956 | |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 3957 | if (DEBUG_POINTER_ASSIGNMENT) { |
| 3958 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
| 3959 | for (size_t j = 0; j < heapSize; j++) { |
| 3960 | ALOGD(" heap[%zu]: cur=%" PRIu32 ", last=%" PRIu32 ", distance=%" PRIu64, |
| 3961 | j, heap[j].currentPointerIndex, heap[j].lastPointerIndex, |
| 3962 | heap[j].distance); |
| 3963 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3964 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | heapSize -= 1; |
| 3968 | |
| 3969 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 3970 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 3971 | |
| 3972 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 3973 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 3974 | |
| 3975 | matchedCurrentBits.markBit(currentPointerIndex); |
| 3976 | matchedLastBits.markBit(lastPointerIndex); |
| 3977 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3978 | uint32_t id = last.rawPointerData.pointers[lastPointerIndex].id; |
| 3979 | current.rawPointerData.pointers[currentPointerIndex].id = id; |
| 3980 | current.rawPointerData.idToIndex[id] = currentPointerIndex; |
| 3981 | current.rawPointerData.markIdBit(id, |
| 3982 | current.rawPointerData.isHovering( |
| 3983 | currentPointerIndex)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3984 | usedIdBits.markBit(id); |
| 3985 | |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 3986 | ALOGD_IF(DEBUG_POINTER_ASSIGNMENT, |
| 3987 | "assignPointerIds - matched: cur=%" PRIu32 ", last=%" PRIu32 ", id=%" PRIu32 |
| 3988 | ", distance=%" PRIu64, |
| 3989 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 3990 | break; |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | // Assign fresh ids to pointers that were not matched in the process. |
| 3995 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 3996 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 3997 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 3998 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 3999 | current.rawPointerData.pointers[currentPointerIndex].id = id; |
| 4000 | current.rawPointerData.idToIndex[id] = currentPointerIndex; |
| 4001 | current.rawPointerData.markIdBit(id, |
| 4002 | current.rawPointerData.isHovering(currentPointerIndex)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4003 | |
Harry Cutts | 4548360 | 2022-08-24 14:36:48 +0000 | [diff] [blame] | 4004 | ALOGD_IF(DEBUG_POINTER_ASSIGNMENT, |
| 4005 | "assignPointerIds - assigned: cur=%" PRIu32 ", id=%" PRIu32, currentPointerIndex, |
| 4006 | id); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 4011 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 4012 | return AKEY_STATE_VIRTUAL; |
| 4013 | } |
| 4014 | |
| 4015 | for (const VirtualKey& virtualKey : mVirtualKeys) { |
| 4016 | if (virtualKey.keyCode == keyCode) { |
| 4017 | return AKEY_STATE_UP; |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | return AKEY_STATE_UNKNOWN; |
| 4022 | } |
| 4023 | |
| 4024 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 4025 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 4026 | return AKEY_STATE_VIRTUAL; |
| 4027 | } |
| 4028 | |
| 4029 | for (const VirtualKey& virtualKey : mVirtualKeys) { |
| 4030 | if (virtualKey.scanCode == scanCode) { |
| 4031 | return AKEY_STATE_UP; |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | return AKEY_STATE_UNKNOWN; |
| 4036 | } |
| 4037 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 4038 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, |
| 4039 | const std::vector<int32_t>& keyCodes, |
| 4040 | uint8_t* outFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4041 | for (const VirtualKey& virtualKey : mVirtualKeys) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 4042 | for (size_t i = 0; i < keyCodes.size(); i++) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4043 | if (virtualKey.keyCode == keyCodes[i]) { |
| 4044 | outFlags[i] = 1; |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | return true; |
| 4050 | } |
| 4051 | |
| 4052 | std::optional<int32_t> TouchInputMapper::getAssociatedDisplayId() { |
| 4053 | if (mParameters.hasAssociatedDisplay) { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 4054 | if (mDeviceMode == DeviceMode::POINTER) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4055 | return std::make_optional(mPointerController->getDisplayId()); |
| 4056 | } else { |
| 4057 | return std::make_optional(mViewport.displayId); |
| 4058 | } |
| 4059 | } |
| 4060 | return std::nullopt; |
| 4061 | } |
| 4062 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 4063 | } // namespace android |