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