| 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 | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 19 | #include <algorithm> | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 20 | #include <chrono> | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 21 | #include <iterator> | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 22 | #include <limits> | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 23 | #include <map> | 
| Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 24 | #include <optional> | 
|  | 25 |  | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 27 | #include <android/input.h> | 
| Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 28 | #include <ftl/enum.h> | 
| Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 29 | #include <input/PrintTools.h> | 
| Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 30 | #include <linux/input-event-codes.h> | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 31 | #include <log/log_main.h> | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 32 | #include <stats_pull_atom_callback.h> | 
|  | 33 | #include <statslog.h> | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 34 | #include "TouchCursorInputMapperCommon.h" | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 35 | #include "TouchpadInputMapper.h" | 
| Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 36 | #include "ui/Rotation.h" | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 37 |  | 
|  | 38 | namespace android { | 
|  | 39 |  | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 40 | namespace { | 
|  | 41 |  | 
| Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 42 | /** | 
|  | 43 | * Log details of each gesture output by the gestures library. | 
|  | 44 | * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires | 
|  | 45 | * restarting the shell) | 
|  | 46 | */ | 
|  | 47 | const bool DEBUG_TOUCHPAD_GESTURES = | 
|  | 48 | __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures", | 
|  | 49 | ANDROID_LOG_INFO); | 
|  | 50 |  | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 51 | // Describes a segment of the acceleration curve. | 
|  | 52 | struct CurveSegment { | 
|  | 53 | // The maximum pointer speed which this segment should apply. The last segment in a curve should | 
|  | 54 | // always set this to infinity. | 
|  | 55 | double maxPointerSpeedMmPerS; | 
|  | 56 | double slope; | 
|  | 57 | double intercept; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | const std::vector<CurveSegment> segments = { | 
| Wenxin Feng | e5d6d04 | 2023-07-06 17:40:45 -0700 | [diff] [blame] | 61 | {32.002, 3.19, 0}, | 
|  | 62 | {52.83, 4.79, -51.254}, | 
|  | 63 | {119.124, 7.28, -182.737}, | 
|  | 64 | {std::numeric_limits<double>::infinity(), 15.04, -1107.556}, | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 65 | }; | 
|  | 66 |  | 
| Wenxin Feng | e5d6d04 | 2023-07-06 17:40:45 -0700 | [diff] [blame] | 67 | const std::vector<double> sensitivityFactors = {1,  2,  4,  6,  7,  8,  9, 10, | 
|  | 68 | 11, 12, 13, 14, 16, 18, 20}; | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 69 |  | 
|  | 70 | std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity, | 
|  | 71 | size_t propertySize) { | 
|  | 72 | LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size()); | 
|  | 73 | std::vector<double> output(propertySize, 0); | 
|  | 74 |  | 
|  | 75 | // The Gestures library uses functions of the following form to define curve segments, where a, | 
|  | 76 | // b, and c can be specified by us: | 
|  | 77 | //     output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c | 
|  | 78 | // | 
|  | 79 | // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.) | 
|  | 80 | // | 
|  | 81 | // We are trying to implement the following function, where slope and intercept are the | 
|  | 82 | // parameters specified in the `segments` array above: | 
|  | 83 | //     gain(input_speed_mm) = | 
|  | 84 | //             0.64 * (sensitivityFactor / 10) * (slope + intercept / input_speed_mm) | 
|  | 85 | // Where "gain" is a multiplier applied to the input speed to produce the output speed: | 
|  | 86 | //     output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm) | 
|  | 87 | // | 
|  | 88 | // To put our function in the library's form, we substitute it into the function above: | 
|  | 89 | //     output_speed(input_speed_mm) = | 
|  | 90 | //             input_speed_mm * (0.64 * (sensitivityFactor / 10) * | 
|  | 91 | //             (slope + 25.4 * intercept / input_speed_mm)) | 
|  | 92 | // then expand the brackets so that input_speed_mm cancels out for the intercept term: | 
|  | 93 | //     gain(input_speed_mm) = | 
|  | 94 | //             0.64 * (sensitivityFactor / 10) * slope * input_speed_mm + | 
|  | 95 | //             0.64 * (sensitivityFactor / 10) * intercept | 
|  | 96 | // | 
|  | 97 | // This gives us the following parameters for the Gestures library function form: | 
|  | 98 | //     a = 0 | 
|  | 99 | //     b = 0.64 * (sensitivityFactor / 10) * slope | 
|  | 100 | //     c = 0.64 * (sensitivityFactor / 10) * intercept | 
|  | 101 |  | 
|  | 102 | double commonFactor = 0.64 * sensitivityFactors[sensitivity + 7] / 10; | 
|  | 103 |  | 
|  | 104 | size_t i = 0; | 
|  | 105 | for (CurveSegment seg : segments) { | 
|  | 106 | // The library's curve format consists of four doubles per segment: | 
|  | 107 | // * maximum pointer speed for the segment (mm/s) | 
|  | 108 | // * multiplier for the x² term (a.k.a. "a" or "sqr") | 
|  | 109 | // * multiplier for the x term (a.k.a. "b" or "mul") | 
|  | 110 | // * the intercept (a.k.a. "c" or "int") | 
|  | 111 | // (see struct CurveSegment in the library's AccelFilterInterpreter) | 
|  | 112 | output[i + 0] = seg.maxPointerSpeedMmPerS; | 
|  | 113 | output[i + 1] = 0; | 
|  | 114 | output[i + 2] = commonFactor * seg.slope; | 
|  | 115 | output[i + 3] = commonFactor * seg.intercept; | 
|  | 116 | i += 4; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | return output; | 
|  | 120 | } | 
|  | 121 |  | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 122 | short getMaxTouchCount(const InputDeviceContext& context) { | 
| Harry Cutts | b255215 | 2022-12-13 17:18:09 +0000 | [diff] [blame] | 123 | if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5; | 
|  | 124 | if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4; | 
|  | 125 | if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3; | 
|  | 126 | if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2; | 
|  | 127 | if (context.hasScanCode(BTN_TOOL_FINGER)) return 1; | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 128 | return 0; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | HardwareProperties createHardwareProperties(const InputDeviceContext& context) { | 
|  | 132 | HardwareProperties props; | 
|  | 133 | RawAbsoluteAxisInfo absMtPositionX; | 
|  | 134 | context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX); | 
|  | 135 | props.left = absMtPositionX.minValue; | 
|  | 136 | props.right = absMtPositionX.maxValue; | 
|  | 137 | props.res_x = absMtPositionX.resolution; | 
|  | 138 |  | 
|  | 139 | RawAbsoluteAxisInfo absMtPositionY; | 
|  | 140 | context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY); | 
|  | 141 | props.top = absMtPositionY.minValue; | 
|  | 142 | props.bottom = absMtPositionY.maxValue; | 
|  | 143 | props.res_y = absMtPositionY.resolution; | 
|  | 144 |  | 
|  | 145 | RawAbsoluteAxisInfo absMtOrientation; | 
|  | 146 | context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation); | 
|  | 147 | props.orientation_minimum = absMtOrientation.minValue; | 
|  | 148 | props.orientation_maximum = absMtOrientation.maxValue; | 
|  | 149 |  | 
|  | 150 | RawAbsoluteAxisInfo absMtSlot; | 
|  | 151 | context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot); | 
|  | 152 | props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1; | 
|  | 153 | props.max_touch_cnt = getMaxTouchCount(context); | 
|  | 154 |  | 
|  | 155 | // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5 | 
|  | 156 | // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads | 
|  | 157 | // that did this, so assume false. | 
|  | 158 | props.supports_t5r2 = false; | 
|  | 159 |  | 
|  | 160 | props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT); | 
|  | 161 | props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD); | 
|  | 162 |  | 
|  | 163 | // Mouse-only properties, which will always be false. | 
|  | 164 | props.has_wheel = false; | 
|  | 165 | props.wheel_is_hi_res = false; | 
|  | 166 |  | 
|  | 167 | // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads | 
|  | 168 | // are haptic. | 
|  | 169 | props.is_haptic_pad = false; | 
|  | 170 | return props; | 
|  | 171 | } | 
|  | 172 |  | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 173 | void gestureInterpreterCallback(void* clientData, const Gesture* gesture) { | 
|  | 174 | TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData); | 
|  | 175 | mapper->consumeGesture(gesture); | 
|  | 176 | } | 
|  | 177 |  | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 178 | int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus) { | 
|  | 179 | // When adding cases to this switch, also add them to the copy of this method in | 
|  | 180 | // InputDeviceMetricsCollector.cpp. | 
|  | 181 | // TODO(b/286394420): deduplicate this method with the one in InputDeviceMetricsCollector.cpp. | 
|  | 182 | switch (linuxBus) { | 
|  | 183 | case BUS_USB: | 
|  | 184 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB; | 
|  | 185 | case BUS_BLUETOOTH: | 
|  | 186 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH; | 
|  | 187 | default: | 
|  | 188 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER; | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | class MetricsAccumulator { | 
|  | 193 | public: | 
|  | 194 | static MetricsAccumulator& getInstance() { | 
|  | 195 | static MetricsAccumulator sAccumulator; | 
|  | 196 | return sAccumulator; | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | void recordFinger(const TouchpadInputMapper::MetricsIdentifier& id) { mCounters[id].fingers++; } | 
|  | 200 |  | 
|  | 201 | void recordPalm(const TouchpadInputMapper::MetricsIdentifier& id) { mCounters[id].palms++; } | 
|  | 202 |  | 
|  | 203 | // Checks whether a Gesture struct is for the end of a gesture that we log metrics for, and | 
|  | 204 | // records it if so. | 
|  | 205 | void processGesture(const TouchpadInputMapper::MetricsIdentifier& id, const Gesture& gesture) { | 
|  | 206 | switch (gesture.type) { | 
|  | 207 | case kGestureTypeFling: | 
|  | 208 | if (gesture.details.fling.fling_state == GESTURES_FLING_START) { | 
|  | 209 | // Indicates the end of a two-finger scroll gesture. | 
|  | 210 | mCounters[id].twoFingerSwipeGestures++; | 
|  | 211 | } | 
|  | 212 | break; | 
|  | 213 | case kGestureTypeSwipeLift: | 
|  | 214 | mCounters[id].threeFingerSwipeGestures++; | 
|  | 215 | break; | 
|  | 216 | case kGestureTypeFourFingerSwipeLift: | 
|  | 217 | mCounters[id].fourFingerSwipeGestures++; | 
|  | 218 | break; | 
|  | 219 | case kGestureTypePinch: | 
|  | 220 | if (gesture.details.pinch.zoom_state == GESTURES_ZOOM_END) { | 
|  | 221 | mCounters[id].pinchGestures++; | 
|  | 222 | } | 
|  | 223 | break; | 
|  | 224 | default: | 
|  | 225 | // We're not interested in any other gestures. | 
|  | 226 | break; | 
|  | 227 | } | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | private: | 
|  | 231 | MetricsAccumulator() { | 
|  | 232 | AStatsManager_setPullAtomCallback(android::util::TOUCHPAD_USAGE, /*metadata=*/nullptr, | 
|  | 233 | MetricsAccumulator::pullAtomCallback, /*cookie=*/nullptr); | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | ~MetricsAccumulator() { AStatsManager_clearPullAtomCallback(android::util::TOUCHPAD_USAGE); } | 
|  | 237 |  | 
|  | 238 | static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag, | 
|  | 239 | AStatsEventList* outEventList, | 
|  | 240 | void* cookie) { | 
|  | 241 | LOG_ALWAYS_FATAL_IF(atomTag != android::util::TOUCHPAD_USAGE); | 
|  | 242 | MetricsAccumulator& accumulator = MetricsAccumulator::getInstance(); | 
|  | 243 | accumulator.produceAtoms(outEventList); | 
|  | 244 | accumulator.resetCounters(); | 
|  | 245 | return AStatsManager_PULL_SUCCESS; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | void produceAtoms(AStatsEventList* outEventList) const { | 
|  | 249 | for (auto& [id, counters] : mCounters) { | 
|  | 250 | auto [busId, vendorId, productId, versionId] = id; | 
|  | 251 | addAStatsEvent(outEventList, android::util::TOUCHPAD_USAGE, vendorId, productId, | 
|  | 252 | versionId, linuxBusToInputDeviceBusEnum(busId), counters.fingers, | 
|  | 253 | counters.palms, counters.twoFingerSwipeGestures, | 
|  | 254 | counters.threeFingerSwipeGestures, counters.fourFingerSwipeGestures, | 
|  | 255 | counters.pinchGestures); | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | void resetCounters() { mCounters.clear(); } | 
|  | 260 |  | 
|  | 261 | // Stores the counters for a specific touchpad model. Fields have the same meanings as those of | 
|  | 262 | // the TouchpadUsage atom; see that definition for detailed documentation. | 
|  | 263 | struct Counters { | 
|  | 264 | int32_t fingers = 0; | 
|  | 265 | int32_t palms = 0; | 
|  | 266 |  | 
|  | 267 | int32_t twoFingerSwipeGestures = 0; | 
|  | 268 | int32_t threeFingerSwipeGestures = 0; | 
|  | 269 | int32_t fourFingerSwipeGestures = 0; | 
|  | 270 | int32_t pinchGestures = 0; | 
|  | 271 | }; | 
|  | 272 |  | 
|  | 273 | // Metrics are aggregated by device model and version, so if two devices of the same model and | 
|  | 274 | // version are connected at once, they will have the same counters. | 
|  | 275 | std::map<TouchpadInputMapper::MetricsIdentifier, Counters> mCounters; | 
|  | 276 | }; | 
|  | 277 |  | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 278 | } // namespace | 
|  | 279 |  | 
| Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 280 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext, | 
|  | 281 | const InputReaderConfiguration& readerConfig) | 
|  | 282 | : InputMapper(deviceContext, readerConfig), | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 283 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 284 | mPointerController(getContext()->getPointerController(getDeviceId())), | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 285 | mStateConverter(deviceContext, mMotionAccumulator), | 
|  | 286 | mGestureConverter(*getContext(), deviceContext, getDeviceId()), | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 287 | mCapturedEventConverter(*getContext(), deviceContext, mMotionAccumulator, getDeviceId()), | 
|  | 288 | mMetricsId(metricsIdFromInputDeviceIdentifier(deviceContext.getDeviceIdentifier())) { | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 289 | RawAbsoluteAxisInfo slotAxisInfo; | 
|  | 290 | deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo); | 
|  | 291 | if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) { | 
|  | 292 | ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work " | 
|  | 293 | "properly.", | 
|  | 294 | deviceContext.getName().c_str()); | 
|  | 295 | } | 
|  | 296 | mMotionAccumulator.configure(deviceContext, slotAxisInfo.maxValue + 1, true); | 
|  | 297 |  | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 298 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); | 
|  | 299 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 300 | // 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] | 301 | // give away pointers to TouchpadInputMapper and its members here because | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 302 | // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and | 
|  | 303 | // 2) TouchpadInputMapper is stored as a unique_ptr and not moved. | 
| Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 304 | mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider), | 
|  | 305 | &mPropertyProvider); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 306 | mGestureInterpreter->SetCallback(gestureInterpreterCallback, this); | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 307 | // 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] | 308 | } | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 309 |  | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 310 | TouchpadInputMapper::~TouchpadInputMapper() { | 
|  | 311 | if (mPointerController != nullptr) { | 
|  | 312 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); | 
|  | 313 | } | 
| Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 314 |  | 
|  | 315 | // The gesture interpreter's destructor will call its property provider's free function for all | 
|  | 316 | // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer | 
|  | 317 | // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may | 
|  | 318 | // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on | 
|  | 319 | // declaration order to avoid crashes seems rather fragile, so explicitly clear the property | 
|  | 320 | // provider here to ensure all the freeProperty calls happen before mPropertyProvider is | 
|  | 321 | // destructed. | 
|  | 322 | mGestureInterpreter->SetPropProvider(nullptr, nullptr); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 325 | uint32_t TouchpadInputMapper::getSources() const { | 
|  | 326 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; | 
|  | 327 | } | 
|  | 328 |  | 
| Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 329 | void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) { | 
| Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 330 | InputMapper::populateDeviceInfo(info); | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 331 | if (mPointerCaptured) { | 
|  | 332 | mCapturedEventConverter.populateMotionRanges(info); | 
|  | 333 | } else { | 
|  | 334 | mGestureConverter.populateMotionRanges(info); | 
|  | 335 | } | 
| Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
| Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 338 | void TouchpadInputMapper::dump(std::string& dump) { | 
|  | 339 | dump += INDENT2 "Touchpad Input Mapper:\n"; | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 340 | if (mProcessing) { | 
|  | 341 | dump += INDENT3 "Currently processing a hardware state\n"; | 
|  | 342 | } | 
|  | 343 | if (mResettingInterpreter) { | 
|  | 344 | dump += INDENT3 "Currently resetting gesture interpreter\n"; | 
|  | 345 | } | 
|  | 346 | dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured)); | 
| Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 347 | dump += INDENT3 "Gesture converter:\n"; | 
|  | 348 | dump += addLinePrefix(mGestureConverter.dump(), INDENT4); | 
|  | 349 | dump += INDENT3 "Gesture properties:\n"; | 
|  | 350 | dump += addLinePrefix(mPropertyProvider.dump(), INDENT4); | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 351 | dump += INDENT3 "Captured event converter:\n"; | 
|  | 352 | dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4); | 
| Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
| Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 355 | std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when, | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 356 | const InputReaderConfiguration& config, | 
| Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 357 | ConfigurationChanges changes) { | 
|  | 358 | if (!changes.any()) { | 
| Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 359 | // First time configuration | 
|  | 360 | mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration()); | 
|  | 361 | } | 
|  | 362 |  | 
| Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 363 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) { | 
| Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 364 | std::optional<int32_t> displayId = mPointerController->getDisplayId(); | 
|  | 365 | ui::Rotation orientation = ui::ROTATION_0; | 
|  | 366 | if (displayId.has_value()) { | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 367 | if (auto viewport = config.getDisplayViewportById(*displayId); viewport) { | 
| Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 368 | orientation = getInverseRotation(viewport->orientation); | 
|  | 369 | } | 
|  | 370 | } | 
|  | 371 | mGestureConverter.setOrientation(orientation); | 
|  | 372 | } | 
| Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 373 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) { | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 374 | mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve") | 
|  | 375 | .setBoolValues({true}); | 
|  | 376 | GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve"); | 
|  | 377 | accelCurveProp.setRealValues( | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 378 | createAccelerationCurveForSensitivity(config.touchpadPointerSpeed, | 
| Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 379 | accelCurveProp.getCount())); | 
| Wenxin Feng | 1ca5066 | 2023-05-03 14:00:12 -0700 | [diff] [blame] | 380 | mPropertyProvider.getProperty("Use Custom Touchpad Scroll Accel Curve") | 
|  | 381 | .setBoolValues({true}); | 
|  | 382 | GesturesProp scrollCurveProp = mPropertyProvider.getProperty("Scroll Accel Curve"); | 
|  | 383 | scrollCurveProp.setRealValues( | 
|  | 384 | createAccelerationCurveForSensitivity(config.touchpadPointerSpeed, | 
|  | 385 | scrollCurveProp.getCount())); | 
|  | 386 | mPropertyProvider.getProperty("Scroll X Out Scale").setRealValues({1.0}); | 
|  | 387 | mPropertyProvider.getProperty("Scroll Y Out Scale").setRealValues({1.0}); | 
| Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 388 | mPropertyProvider.getProperty("Invert Scrolling") | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 389 | .setBoolValues({config.touchpadNaturalScrollingEnabled}); | 
| Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 390 | mPropertyProvider.getProperty("Tap Enable") | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 391 | .setBoolValues({config.touchpadTapToClickEnabled}); | 
| Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 392 | mPropertyProvider.getProperty("Button Right Click Zone Enable") | 
| Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 393 | .setBoolValues({config.touchpadRightClickZoneEnabled}); | 
| Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 394 | } | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 395 | std::list<NotifyArgs> out; | 
|  | 396 | if ((!changes.any() && config.pointerCaptureRequest.enable) || | 
|  | 397 | changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) { | 
|  | 398 | mPointerCaptured = config.pointerCaptureRequest.enable; | 
|  | 399 | // The motion ranges are going to change, so bump the generation to clear the cached ones. | 
|  | 400 | bumpGeneration(); | 
|  | 401 | if (mPointerCaptured) { | 
|  | 402 | // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are | 
|  | 403 | // still being reported for a gesture in progress. | 
|  | 404 | out += reset(when); | 
|  | 405 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); | 
|  | 406 | } else { | 
|  | 407 | // We're transitioning from captured to uncaptured. | 
|  | 408 | mCapturedEventConverter.reset(); | 
|  | 409 | } | 
|  | 410 | if (changes.any()) { | 
|  | 411 | out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId())); | 
|  | 412 | } | 
|  | 413 | } | 
|  | 414 | return out; | 
| Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 417 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 418 | mStateConverter.reset(); | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 419 | resetGestureInterpreter(when); | 
| Harry Cutts | e9b7142 | 2023-03-14 16:54:44 +0000 | [diff] [blame] | 420 | std::list<NotifyArgs> out = mGestureConverter.reset(when); | 
|  | 421 | out += InputMapper::reset(when); | 
|  | 422 | return out; | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 425 | void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) { | 
|  | 426 | // The GestureInterpreter has no official reset method, but sending a HardwareState with no | 
|  | 427 | // fingers down or buttons pressed should get it into a clean state. | 
|  | 428 | HardwareState state; | 
|  | 429 | state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count(); | 
|  | 430 | mResettingInterpreter = true; | 
|  | 431 | mGestureInterpreter->PushHardwareState(&state); | 
|  | 432 | mResettingInterpreter = false; | 
|  | 433 | } | 
|  | 434 |  | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 435 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 436 | if (mPointerCaptured) { | 
|  | 437 | return mCapturedEventConverter.process(*rawEvent); | 
|  | 438 | } | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 439 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); | 
|  | 440 | if (state) { | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 441 | updatePalmDetectionMetrics(); | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 442 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); | 
|  | 443 | } else { | 
|  | 444 | return {}; | 
| Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 445 | } | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 448 | void TouchpadInputMapper::updatePalmDetectionMetrics() { | 
|  | 449 | std::set<int32_t> currentTrackingIds; | 
|  | 450 | for (size_t i = 0; i < mMotionAccumulator.getSlotCount(); i++) { | 
|  | 451 | const MultiTouchMotionAccumulator::Slot& slot = mMotionAccumulator.getSlot(i); | 
|  | 452 | if (!slot.isInUse()) { | 
|  | 453 | continue; | 
|  | 454 | } | 
|  | 455 | currentTrackingIds.insert(slot.getTrackingId()); | 
|  | 456 | if (slot.getToolType() == ToolType::PALM) { | 
|  | 457 | mPalmTrackingIds.insert(slot.getTrackingId()); | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 | std::vector<int32_t> liftedTouches; | 
|  | 461 | std::set_difference(mLastFrameTrackingIds.begin(), mLastFrameTrackingIds.end(), | 
|  | 462 | currentTrackingIds.begin(), currentTrackingIds.end(), | 
|  | 463 | std::inserter(liftedTouches, liftedTouches.begin())); | 
|  | 464 | for (int32_t trackingId : liftedTouches) { | 
|  | 465 | if (mPalmTrackingIds.erase(trackingId) > 0) { | 
|  | 466 | MetricsAccumulator::getInstance().recordPalm(mMetricsId); | 
|  | 467 | } else { | 
|  | 468 | MetricsAccumulator::getInstance().recordFinger(mMetricsId); | 
|  | 469 | } | 
|  | 470 | } | 
|  | 471 | mLastFrameTrackingIds = currentTrackingIds; | 
|  | 472 | } | 
|  | 473 |  | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 474 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, | 
|  | 475 | SelfContainedHardwareState schs) { | 
| Harry Cutts | 287e19f | 2023-02-27 17:09:24 +0000 | [diff] [blame] | 476 | 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] | 477 | mProcessing = true; | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 478 | mGestureInterpreter->PushHardwareState(&schs.state); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 479 | mProcessing = false; | 
|  | 480 |  | 
| Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 481 | return processGestures(when, readTime); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
|  | 484 | void TouchpadInputMapper::consumeGesture(const Gesture* gesture) { | 
| Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 485 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str()); | 
| Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 486 | if (mResettingInterpreter) { | 
|  | 487 | // We already handle tidying up fake fingers etc. in GestureConverter::reset, so we should | 
|  | 488 | // ignore any gestures produced from the interpreter while we're resetting it. | 
|  | 489 | return; | 
|  | 490 | } | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 491 | if (!mProcessing) { | 
|  | 492 | ALOGE("Received gesture outside of the normal processing flow; ignoring it."); | 
|  | 493 | return; | 
|  | 494 | } | 
|  | 495 | mGesturesToProcess.push_back(*gesture); | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { | 
|  | 499 | std::list<NotifyArgs> out = {}; | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 500 | MetricsAccumulator& metricsAccumulator = MetricsAccumulator::getInstance(); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 501 | for (Gesture& gesture : mGesturesToProcess) { | 
| Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 502 | out += mGestureConverter.handleGesture(when, readTime, gesture); | 
| Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 503 | metricsAccumulator.processGesture(mMetricsId, gesture); | 
| Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 504 | } | 
|  | 505 | mGesturesToProcess.clear(); | 
|  | 506 | return out; | 
|  | 507 | } | 
|  | 508 |  | 
| Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 509 | } // namespace android |