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