Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | |
| 17 | #include "../Macros.h" |
| 18 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 19 | #include <chrono> |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 20 | #include <limits> |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 21 | #include <optional> |
| 22 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 23 | #include <android-base/stringprintf.h> |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 24 | #include <android/input.h> |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 25 | #include <ftl/enum.h> |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 26 | #include <input/PrintTools.h> |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 27 | #include <linux/input-event-codes.h> |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 28 | #include <log/log_main.h> |
| 29 | #include "TouchCursorInputMapperCommon.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 30 | #include "TouchpadInputMapper.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 31 | #include "ui/Rotation.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Log details of each gesture output by the gestures library. |
| 39 | * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires |
| 40 | * restarting the shell) |
| 41 | */ |
| 42 | const bool DEBUG_TOUCHPAD_GESTURES = |
| 43 | __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures", |
| 44 | ANDROID_LOG_INFO); |
| 45 | |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 46 | // Describes a segment of the acceleration curve. |
| 47 | struct CurveSegment { |
| 48 | // The maximum pointer speed which this segment should apply. The last segment in a curve should |
| 49 | // always set this to infinity. |
| 50 | double maxPointerSpeedMmPerS; |
| 51 | double slope; |
| 52 | double intercept; |
| 53 | }; |
| 54 | |
| 55 | const std::vector<CurveSegment> segments = { |
| 56 | {10.922, 3.19, 0}, |
| 57 | {31.750, 4.79, -17.526}, |
| 58 | {98.044, 7.28, -96.52}, |
| 59 | {std::numeric_limits<double>::infinity(), 15.04, -857.758}, |
| 60 | }; |
| 61 | |
Wenxin Feng | e0320e7 | 2023-04-08 19:26:02 -0700 | [diff] [blame] | 62 | const std::vector<double> sensitivityFactors = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18}; |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 63 | |
| 64 | std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity, |
| 65 | size_t propertySize) { |
| 66 | LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size()); |
| 67 | std::vector<double> output(propertySize, 0); |
| 68 | |
| 69 | // The Gestures library uses functions of the following form to define curve segments, where a, |
| 70 | // b, and c can be specified by us: |
| 71 | // output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c |
| 72 | // |
| 73 | // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.) |
| 74 | // |
| 75 | // We are trying to implement the following function, where slope and intercept are the |
| 76 | // parameters specified in the `segments` array above: |
| 77 | // gain(input_speed_mm) = |
| 78 | // 0.64 * (sensitivityFactor / 10) * (slope + intercept / input_speed_mm) |
| 79 | // Where "gain" is a multiplier applied to the input speed to produce the output speed: |
| 80 | // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm) |
| 81 | // |
| 82 | // To put our function in the library's form, we substitute it into the function above: |
| 83 | // output_speed(input_speed_mm) = |
| 84 | // input_speed_mm * (0.64 * (sensitivityFactor / 10) * |
| 85 | // (slope + 25.4 * intercept / input_speed_mm)) |
| 86 | // then expand the brackets so that input_speed_mm cancels out for the intercept term: |
| 87 | // gain(input_speed_mm) = |
| 88 | // 0.64 * (sensitivityFactor / 10) * slope * input_speed_mm + |
| 89 | // 0.64 * (sensitivityFactor / 10) * intercept |
| 90 | // |
| 91 | // This gives us the following parameters for the Gestures library function form: |
| 92 | // a = 0 |
| 93 | // b = 0.64 * (sensitivityFactor / 10) * slope |
| 94 | // c = 0.64 * (sensitivityFactor / 10) * intercept |
| 95 | |
| 96 | double commonFactor = 0.64 * sensitivityFactors[sensitivity + 7] / 10; |
| 97 | |
| 98 | size_t i = 0; |
| 99 | for (CurveSegment seg : segments) { |
| 100 | // The library's curve format consists of four doubles per segment: |
| 101 | // * maximum pointer speed for the segment (mm/s) |
| 102 | // * multiplier for the x² term (a.k.a. "a" or "sqr") |
| 103 | // * multiplier for the x term (a.k.a. "b" or "mul") |
| 104 | // * the intercept (a.k.a. "c" or "int") |
| 105 | // (see struct CurveSegment in the library's AccelFilterInterpreter) |
| 106 | output[i + 0] = seg.maxPointerSpeedMmPerS; |
| 107 | output[i + 1] = 0; |
| 108 | output[i + 2] = commonFactor * seg.slope; |
| 109 | output[i + 3] = commonFactor * seg.intercept; |
| 110 | i += 4; |
| 111 | } |
| 112 | |
| 113 | return output; |
| 114 | } |
| 115 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 116 | short getMaxTouchCount(const InputDeviceContext& context) { |
Harry Cutts | b255215 | 2022-12-13 17:18:09 +0000 | [diff] [blame] | 117 | if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5; |
| 118 | if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4; |
| 119 | if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3; |
| 120 | if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2; |
| 121 | if (context.hasScanCode(BTN_TOOL_FINGER)) return 1; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | HardwareProperties createHardwareProperties(const InputDeviceContext& context) { |
| 126 | HardwareProperties props; |
| 127 | RawAbsoluteAxisInfo absMtPositionX; |
| 128 | context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX); |
| 129 | props.left = absMtPositionX.minValue; |
| 130 | props.right = absMtPositionX.maxValue; |
| 131 | props.res_x = absMtPositionX.resolution; |
| 132 | |
| 133 | RawAbsoluteAxisInfo absMtPositionY; |
| 134 | context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY); |
| 135 | props.top = absMtPositionY.minValue; |
| 136 | props.bottom = absMtPositionY.maxValue; |
| 137 | props.res_y = absMtPositionY.resolution; |
| 138 | |
| 139 | RawAbsoluteAxisInfo absMtOrientation; |
| 140 | context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation); |
| 141 | props.orientation_minimum = absMtOrientation.minValue; |
| 142 | props.orientation_maximum = absMtOrientation.maxValue; |
| 143 | |
| 144 | RawAbsoluteAxisInfo absMtSlot; |
| 145 | context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot); |
| 146 | props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1; |
| 147 | props.max_touch_cnt = getMaxTouchCount(context); |
| 148 | |
| 149 | // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5 |
| 150 | // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads |
| 151 | // that did this, so assume false. |
| 152 | props.supports_t5r2 = false; |
| 153 | |
| 154 | props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT); |
| 155 | props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD); |
| 156 | |
| 157 | // Mouse-only properties, which will always be false. |
| 158 | props.has_wheel = false; |
| 159 | props.wheel_is_hi_res = false; |
| 160 | |
| 161 | // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads |
| 162 | // are haptic. |
| 163 | props.is_haptic_pad = false; |
| 164 | return props; |
| 165 | } |
| 166 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 167 | void gestureInterpreterCallback(void* clientData, const Gesture* gesture) { |
| 168 | TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData); |
| 169 | mapper->consumeGesture(gesture); |
| 170 | } |
| 171 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 172 | } // namespace |
| 173 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 174 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext, |
| 175 | const InputReaderConfiguration& readerConfig) |
| 176 | : InputMapper(deviceContext, readerConfig), |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 177 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 178 | mPointerController(getContext()->getPointerController(getDeviceId())), |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 179 | mStateConverter(deviceContext, mMotionAccumulator), |
| 180 | mGestureConverter(*getContext(), deviceContext, getDeviceId()), |
| 181 | mCapturedEventConverter(*getContext(), deviceContext, mMotionAccumulator, getDeviceId()) { |
| 182 | RawAbsoluteAxisInfo slotAxisInfo; |
| 183 | deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo); |
| 184 | if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) { |
| 185 | ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work " |
| 186 | "properly.", |
| 187 | deviceContext.getName().c_str()); |
| 188 | } |
| 189 | mMotionAccumulator.configure(deviceContext, slotAxisInfo.maxValue + 1, true); |
| 190 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 191 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); |
| 192 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 193 | // Even though we don't explicitly delete copy/move semantics, it's safe to |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 194 | // give away pointers to TouchpadInputMapper and its members here because |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 195 | // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and |
| 196 | // 2) TouchpadInputMapper is stored as a unique_ptr and not moved. |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 197 | mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider), |
| 198 | &mPropertyProvider); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 199 | mGestureInterpreter->SetCallback(gestureInterpreterCallback, this); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 200 | // TODO(b/251196347): set a timer provider, so the library can use timers. |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 201 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 202 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 203 | TouchpadInputMapper::~TouchpadInputMapper() { |
| 204 | if (mPointerController != nullptr) { |
| 205 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 206 | } |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 207 | |
| 208 | // The gesture interpreter's destructor will call its property provider's free function for all |
| 209 | // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer |
| 210 | // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may |
| 211 | // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on |
| 212 | // declaration order to avoid crashes seems rather fragile, so explicitly clear the property |
| 213 | // provider here to ensure all the freeProperty calls happen before mPropertyProvider is |
| 214 | // destructed. |
| 215 | mGestureInterpreter->SetPropProvider(nullptr, nullptr); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 218 | uint32_t TouchpadInputMapper::getSources() const { |
| 219 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; |
| 220 | } |
| 221 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 222 | void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 223 | InputMapper::populateDeviceInfo(info); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 224 | if (mPointerCaptured) { |
| 225 | mCapturedEventConverter.populateMotionRanges(info); |
| 226 | } else { |
| 227 | mGestureConverter.populateMotionRanges(info); |
| 228 | } |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 231 | void TouchpadInputMapper::dump(std::string& dump) { |
| 232 | dump += INDENT2 "Touchpad Input Mapper:\n"; |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 233 | if (mProcessing) { |
| 234 | dump += INDENT3 "Currently processing a hardware state\n"; |
| 235 | } |
| 236 | if (mResettingInterpreter) { |
| 237 | dump += INDENT3 "Currently resetting gesture interpreter\n"; |
| 238 | } |
| 239 | dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured)); |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 240 | dump += INDENT3 "Gesture converter:\n"; |
| 241 | dump += addLinePrefix(mGestureConverter.dump(), INDENT4); |
| 242 | dump += INDENT3 "Gesture properties:\n"; |
| 243 | dump += addLinePrefix(mPropertyProvider.dump(), INDENT4); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 244 | dump += INDENT3 "Captured event converter:\n"; |
| 245 | dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4); |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 248 | std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 249 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 250 | ConfigurationChanges changes) { |
| 251 | if (!changes.any()) { |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 252 | // First time configuration |
| 253 | mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration()); |
| 254 | } |
| 255 | |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 256 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) { |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 257 | std::optional<int32_t> displayId = mPointerController->getDisplayId(); |
| 258 | ui::Rotation orientation = ui::ROTATION_0; |
| 259 | if (displayId.has_value()) { |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 260 | if (auto viewport = config.getDisplayViewportById(*displayId); viewport) { |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 261 | orientation = getInverseRotation(viewport->orientation); |
| 262 | } |
| 263 | } |
| 264 | mGestureConverter.setOrientation(orientation); |
| 265 | } |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 266 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) { |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 267 | mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve") |
| 268 | .setBoolValues({true}); |
| 269 | GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve"); |
| 270 | accelCurveProp.setRealValues( |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 271 | createAccelerationCurveForSensitivity(config.touchpadPointerSpeed, |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 272 | accelCurveProp.getCount())); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 273 | mPropertyProvider.getProperty("Invert Scrolling") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 274 | .setBoolValues({config.touchpadNaturalScrollingEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 275 | mPropertyProvider.getProperty("Tap Enable") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 276 | .setBoolValues({config.touchpadTapToClickEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 277 | mPropertyProvider.getProperty("Button Right Click Zone Enable") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 278 | .setBoolValues({config.touchpadRightClickZoneEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 279 | } |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 280 | std::list<NotifyArgs> out; |
| 281 | if ((!changes.any() && config.pointerCaptureRequest.enable) || |
| 282 | changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) { |
| 283 | mPointerCaptured = config.pointerCaptureRequest.enable; |
| 284 | // The motion ranges are going to change, so bump the generation to clear the cached ones. |
| 285 | bumpGeneration(); |
| 286 | if (mPointerCaptured) { |
| 287 | // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are |
| 288 | // still being reported for a gesture in progress. |
| 289 | out += reset(when); |
| 290 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 291 | } else { |
| 292 | // We're transitioning from captured to uncaptured. |
| 293 | mCapturedEventConverter.reset(); |
| 294 | } |
| 295 | if (changes.any()) { |
| 296 | out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId())); |
| 297 | } |
| 298 | } |
| 299 | return out; |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 302 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 303 | mStateConverter.reset(); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 304 | resetGestureInterpreter(when); |
Harry Cutts | e9b7142 | 2023-03-14 16:54:44 +0000 | [diff] [blame] | 305 | std::list<NotifyArgs> out = mGestureConverter.reset(when); |
| 306 | out += InputMapper::reset(when); |
| 307 | return out; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 310 | void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) { |
| 311 | // The GestureInterpreter has no official reset method, but sending a HardwareState with no |
| 312 | // fingers down or buttons pressed should get it into a clean state. |
| 313 | HardwareState state; |
| 314 | state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count(); |
| 315 | mResettingInterpreter = true; |
| 316 | mGestureInterpreter->PushHardwareState(&state); |
| 317 | mResettingInterpreter = false; |
| 318 | } |
| 319 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 320 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 321 | if (mPointerCaptured) { |
| 322 | return mCapturedEventConverter.process(*rawEvent); |
| 323 | } |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 324 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); |
| 325 | if (state) { |
| 326 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); |
| 327 | } else { |
| 328 | return {}; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 329 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 332 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, |
| 333 | SelfContainedHardwareState schs) { |
Harry Cutts | 287e19f | 2023-02-27 17:09:24 +0000 | [diff] [blame] | 334 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str()); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 335 | mProcessing = true; |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 336 | mGestureInterpreter->PushHardwareState(&schs.state); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 337 | mProcessing = false; |
| 338 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 339 | return processGestures(when, readTime); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void TouchpadInputMapper::consumeGesture(const Gesture* gesture) { |
Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 343 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str()); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame^] | 344 | if (mResettingInterpreter) { |
| 345 | // We already handle tidying up fake fingers etc. in GestureConverter::reset, so we should |
| 346 | // ignore any gestures produced from the interpreter while we're resetting it. |
| 347 | return; |
| 348 | } |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 349 | if (!mProcessing) { |
| 350 | ALOGE("Received gesture outside of the normal processing flow; ignoring it."); |
| 351 | return; |
| 352 | } |
| 353 | mGesturesToProcess.push_back(*gesture); |
| 354 | } |
| 355 | |
| 356 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { |
| 357 | std::list<NotifyArgs> out = {}; |
| 358 | for (Gesture& gesture : mGesturesToProcess) { |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 359 | out += mGestureConverter.handleGesture(when, readTime, gesture); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 360 | } |
| 361 | mGesturesToProcess.clear(); |
| 362 | return out; |
| 363 | } |
| 364 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 365 | } // namespace android |