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