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