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