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