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