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