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