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 | |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 50 | static const bool ENABLE_POINTER_CHOREOGRAPHER = input_flags::enable_pointer_choreographer(); |
| 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) |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 237 | : TouchpadInputMapper(deviceContext, readerConfig, ENABLE_POINTER_CHOREOGRAPHER) {} |
| 238 | |
| 239 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext, |
| 240 | const InputReaderConfiguration& readerConfig, |
| 241 | bool enablePointerChoreographer) |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 242 | : InputMapper(deviceContext, readerConfig), |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 243 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 244 | mPointerController(getContext()->getPointerController(getDeviceId())), |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 245 | mTimerProvider(*getContext()), |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 246 | mStateConverter(deviceContext, mMotionAccumulator), |
| 247 | mGestureConverter(*getContext(), deviceContext, getDeviceId()), |
Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 248 | mCapturedEventConverter(*getContext(), deviceContext, mMotionAccumulator, getDeviceId()), |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 249 | mMetricsId(metricsIdFromInputDeviceIdentifier(deviceContext.getDeviceIdentifier())), |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 250 | mEnablePointerChoreographer(enablePointerChoreographer) { |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 251 | RawAbsoluteAxisInfo slotAxisInfo; |
| 252 | deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo); |
| 253 | if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) { |
| 254 | ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work " |
| 255 | "properly.", |
| 256 | deviceContext.getName().c_str()); |
| 257 | } |
| 258 | mMotionAccumulator.configure(deviceContext, slotAxisInfo.maxValue + 1, true); |
| 259 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 260 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); |
| 261 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 262 | // 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] | 263 | // give away pointers to TouchpadInputMapper and its members here because |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 264 | // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and |
| 265 | // 2) TouchpadInputMapper is stored as a unique_ptr and not moved. |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 266 | mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider), |
| 267 | &mPropertyProvider); |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 268 | if (input_flags::enable_gestures_library_timer_provider()) { |
| 269 | mGestureInterpreter->SetTimerProvider(const_cast<GesturesTimerProvider*>( |
| 270 | &kGestureTimerProvider), |
| 271 | &mTimerProvider); |
| 272 | } |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 273 | mGestureInterpreter->SetCallback(gestureInterpreterCallback, this); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 274 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 275 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 276 | TouchpadInputMapper::~TouchpadInputMapper() { |
| 277 | if (mPointerController != nullptr) { |
| 278 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 279 | } |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 280 | |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 281 | // The gesture interpreter's destructor will try to free its property and timer providers, |
| 282 | // calling PropertyProvider::freeProperty and TimerProvider::freeTimer using a raw pointers. |
| 283 | // Depending on the declaration order in TouchpadInputMapper.h, those providers may have already |
| 284 | // been freed, causing allocation errors or use-after-free bugs. Depending on declaration order |
| 285 | // to avoid this seems rather fragile, so explicitly clear the providers here to ensure all the |
| 286 | // freeProperty and freeTimer calls happen before the providers are destructed. |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 287 | mGestureInterpreter->SetPropProvider(nullptr, nullptr); |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 288 | mGestureInterpreter->SetTimerProvider(nullptr, nullptr); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 291 | uint32_t TouchpadInputMapper::getSources() const { |
| 292 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; |
| 293 | } |
| 294 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 295 | void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 296 | InputMapper::populateDeviceInfo(info); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 297 | if (mPointerCaptured) { |
| 298 | mCapturedEventConverter.populateMotionRanges(info); |
| 299 | } else { |
| 300 | mGestureConverter.populateMotionRanges(info); |
| 301 | } |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 304 | void TouchpadInputMapper::dump(std::string& dump) { |
| 305 | dump += INDENT2 "Touchpad Input Mapper:\n"; |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 306 | if (mResettingInterpreter) { |
| 307 | dump += INDENT3 "Currently resetting gesture interpreter\n"; |
| 308 | } |
| 309 | dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured)); |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 310 | dump += INDENT3 "Gesture converter:\n"; |
| 311 | dump += addLinePrefix(mGestureConverter.dump(), INDENT4); |
| 312 | dump += INDENT3 "Gesture properties:\n"; |
| 313 | dump += addLinePrefix(mPropertyProvider.dump(), INDENT4); |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 314 | if (input_flags::enable_gestures_library_timer_provider()) { |
| 315 | dump += INDENT3 "Timer provider:\n"; |
| 316 | dump += addLinePrefix(mTimerProvider.dump(), INDENT4); |
| 317 | } else { |
| 318 | dump += INDENT3 "Timer provider: disabled by flag\n"; |
| 319 | } |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 320 | dump += INDENT3 "Captured event converter:\n"; |
| 321 | dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4); |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 322 | dump += StringPrintf(INDENT3 "DisplayId: %s\n", toString(mDisplayId).c_str()); |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 325 | std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 326 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 327 | ConfigurationChanges changes) { |
| 328 | if (!changes.any()) { |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 329 | // First time configuration |
| 330 | mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration()); |
| 331 | } |
| 332 | |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 333 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) { |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 334 | mDisplayId = ADISPLAY_ID_NONE; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 335 | std::optional<DisplayViewport> resolvedViewport; |
| 336 | std::optional<FloatRect> boundsInLogicalDisplay; |
| 337 | if (auto assocViewport = mDeviceContext.getAssociatedViewport(); assocViewport) { |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 338 | // This InputDevice is associated with a viewport. |
| 339 | // Only generate events for the associated display. |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 340 | mDisplayId = assocViewport->displayId; |
| 341 | resolvedViewport = *assocViewport; |
| 342 | if (!mEnablePointerChoreographer) { |
| 343 | const bool mismatchedPointerDisplay = |
| 344 | (assocViewport->displayId != mPointerController->getDisplayId()); |
| 345 | if (mismatchedPointerDisplay) { |
| 346 | ALOGW("Touchpad \"%s\" associated viewport display does not match pointer " |
| 347 | "controller", |
| 348 | mDeviceContext.getName().c_str()); |
| 349 | mDisplayId.reset(); |
| 350 | } |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 351 | } |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 352 | } else { |
| 353 | // The InputDevice is not associated with a viewport, but it controls the mouse pointer. |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 354 | if (mEnablePointerChoreographer) { |
| 355 | // Always use DISPLAY_ID_NONE for touchpad events. |
| 356 | // PointerChoreographer will make it target the correct the displayId later. |
| 357 | resolvedViewport = |
| 358 | getContext()->getPolicy()->getPointerViewportForAssociatedDisplay(); |
| 359 | mDisplayId = resolvedViewport ? std::make_optional(ADISPLAY_ID_NONE) : std::nullopt; |
| 360 | } else { |
| 361 | mDisplayId = mPointerController->getDisplayId(); |
| 362 | if (auto v = config.getDisplayViewportById(*mDisplayId); v) { |
| 363 | resolvedViewport = *v; |
| 364 | } |
| 365 | if (auto bounds = mPointerController->getBounds(); bounds) { |
| 366 | boundsInLogicalDisplay = *bounds; |
| 367 | } |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 370 | |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 371 | mGestureConverter.setDisplayId(mDisplayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 372 | mGestureConverter.setOrientation(resolvedViewport |
| 373 | ? getInverseRotation(resolvedViewport->orientation) |
| 374 | : ui::ROTATION_0); |
| 375 | |
| 376 | if (!boundsInLogicalDisplay) { |
| 377 | boundsInLogicalDisplay = resolvedViewport |
| 378 | ? FloatRect{static_cast<float>(resolvedViewport->logicalLeft), |
| 379 | static_cast<float>(resolvedViewport->logicalTop), |
| 380 | static_cast<float>(resolvedViewport->logicalRight - 1), |
| 381 | static_cast<float>(resolvedViewport->logicalBottom - 1)} |
| 382 | : FloatRect{0, 0, 0, 0}; |
| 383 | } |
| 384 | mGestureConverter.setBoundsInLogicalDisplay(*boundsInLogicalDisplay); |
Harry Cutts | 5ab9057 | 2024-01-08 14:00:48 +0000 | [diff] [blame] | 385 | |
| 386 | bumpGeneration(); |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 387 | } |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 388 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) { |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 389 | mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve") |
| 390 | .setBoolValues({true}); |
| 391 | GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve"); |
| 392 | accelCurveProp.setRealValues( |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 393 | createAccelerationCurveForSensitivity(config.touchpadPointerSpeed, |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 394 | accelCurveProp.getCount())); |
Wenxin Feng | 1ca5066 | 2023-05-03 14:00:12 -0700 | [diff] [blame] | 395 | mPropertyProvider.getProperty("Use Custom Touchpad Scroll Accel Curve") |
| 396 | .setBoolValues({true}); |
| 397 | GesturesProp scrollCurveProp = mPropertyProvider.getProperty("Scroll Accel Curve"); |
| 398 | scrollCurveProp.setRealValues( |
| 399 | createAccelerationCurveForSensitivity(config.touchpadPointerSpeed, |
| 400 | scrollCurveProp.getCount())); |
| 401 | mPropertyProvider.getProperty("Scroll X Out Scale").setRealValues({1.0}); |
| 402 | mPropertyProvider.getProperty("Scroll Y Out Scale").setRealValues({1.0}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 403 | mPropertyProvider.getProperty("Invert Scrolling") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 404 | .setBoolValues({config.touchpadNaturalScrollingEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 405 | mPropertyProvider.getProperty("Tap Enable") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 406 | .setBoolValues({config.touchpadTapToClickEnabled}); |
Harry Cutts | e0e799d | 2024-01-24 16:27:56 +0000 | [diff] [blame] | 407 | mPropertyProvider.getProperty("Tap Drag Enable") |
| 408 | .setBoolValues({config.touchpadTapDraggingEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 409 | mPropertyProvider.getProperty("Button Right Click Zone Enable") |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 410 | .setBoolValues({config.touchpadRightClickZoneEnabled}); |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 411 | } |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 412 | std::list<NotifyArgs> out; |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame^] | 413 | if ((!changes.any() && config.pointerCaptureRequest.isEnable()) || |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 414 | changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) { |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame^] | 415 | mPointerCaptured = config.pointerCaptureRequest.isEnable(); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 416 | // The motion ranges are going to change, so bump the generation to clear the cached ones. |
| 417 | bumpGeneration(); |
| 418 | if (mPointerCaptured) { |
| 419 | // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are |
| 420 | // still being reported for a gesture in progress. |
| 421 | out += reset(when); |
| 422 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 423 | } else { |
| 424 | // We're transitioning from captured to uncaptured. |
| 425 | mCapturedEventConverter.reset(); |
| 426 | } |
| 427 | if (changes.any()) { |
| 428 | out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId())); |
| 429 | } |
| 430 | } |
| 431 | return out; |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 434 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 435 | mStateConverter.reset(); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 436 | resetGestureInterpreter(when); |
Harry Cutts | e9b7142 | 2023-03-14 16:54:44 +0000 | [diff] [blame] | 437 | std::list<NotifyArgs> out = mGestureConverter.reset(when); |
| 438 | out += InputMapper::reset(when); |
| 439 | return out; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 442 | void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) { |
| 443 | // The GestureInterpreter has no official reset method, but sending a HardwareState with no |
| 444 | // fingers down or buttons pressed should get it into a clean state. |
| 445 | HardwareState state; |
| 446 | state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count(); |
| 447 | mResettingInterpreter = true; |
| 448 | mGestureInterpreter->PushHardwareState(&state); |
| 449 | mResettingInterpreter = false; |
| 450 | } |
| 451 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 452 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 453 | if (mPointerCaptured) { |
| 454 | return mCapturedEventConverter.process(*rawEvent); |
| 455 | } |
Arpit Singh | 33a10a6 | 2023-10-12 13:06:54 +0000 | [diff] [blame] | 456 | if (mMotionAccumulator.getActiveSlotsCount() == 0) { |
| 457 | mGestureStartTime = rawEvent->when; |
| 458 | } |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 459 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); |
| 460 | if (state) { |
Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 461 | updatePalmDetectionMetrics(); |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 462 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); |
| 463 | } else { |
| 464 | return {}; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 465 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 468 | void TouchpadInputMapper::updatePalmDetectionMetrics() { |
| 469 | std::set<int32_t> currentTrackingIds; |
| 470 | for (size_t i = 0; i < mMotionAccumulator.getSlotCount(); i++) { |
| 471 | const MultiTouchMotionAccumulator::Slot& slot = mMotionAccumulator.getSlot(i); |
| 472 | if (!slot.isInUse()) { |
| 473 | continue; |
| 474 | } |
| 475 | currentTrackingIds.insert(slot.getTrackingId()); |
| 476 | if (slot.getToolType() == ToolType::PALM) { |
| 477 | mPalmTrackingIds.insert(slot.getTrackingId()); |
| 478 | } |
| 479 | } |
| 480 | std::vector<int32_t> liftedTouches; |
| 481 | std::set_difference(mLastFrameTrackingIds.begin(), mLastFrameTrackingIds.end(), |
| 482 | currentTrackingIds.begin(), currentTrackingIds.end(), |
| 483 | std::inserter(liftedTouches, liftedTouches.begin())); |
| 484 | for (int32_t trackingId : liftedTouches) { |
| 485 | if (mPalmTrackingIds.erase(trackingId) > 0) { |
| 486 | MetricsAccumulator::getInstance().recordPalm(mMetricsId); |
| 487 | } else { |
| 488 | MetricsAccumulator::getInstance().recordFinger(mMetricsId); |
| 489 | } |
| 490 | } |
| 491 | mLastFrameTrackingIds = currentTrackingIds; |
| 492 | } |
| 493 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 494 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, |
| 495 | SelfContainedHardwareState schs) { |
Harry Cutts | 287e19f | 2023-02-27 17:09:24 +0000 | [diff] [blame] | 496 | 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] | 497 | mGestureInterpreter->PushHardwareState(&schs.state); |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 498 | return processGestures(when, readTime); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Harry Cutts | 8c7cb59 | 2023-08-23 17:20:13 +0000 | [diff] [blame] | 501 | std::list<NotifyArgs> TouchpadInputMapper::timeoutExpired(nsecs_t when) { |
| 502 | if (!input_flags::enable_gestures_library_timer_provider()) { |
| 503 | return {}; |
| 504 | } |
| 505 | mTimerProvider.triggerCallbacks(when); |
| 506 | return processGestures(when, when); |
| 507 | } |
| 508 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 509 | void TouchpadInputMapper::consumeGesture(const Gesture* gesture) { |
Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 510 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str()); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 511 | if (mResettingInterpreter) { |
| 512 | // We already handle tidying up fake fingers etc. in GestureConverter::reset, so we should |
| 513 | // ignore any gestures produced from the interpreter while we're resetting it. |
| 514 | return; |
| 515 | } |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 516 | mGesturesToProcess.push_back(*gesture); |
| 517 | } |
| 518 | |
| 519 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { |
| 520 | std::list<NotifyArgs> out = {}; |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 521 | if (mDisplayId) { |
| 522 | MetricsAccumulator& metricsAccumulator = MetricsAccumulator::getInstance(); |
| 523 | for (Gesture& gesture : mGesturesToProcess) { |
Arpit Singh | 33a10a6 | 2023-10-12 13:06:54 +0000 | [diff] [blame] | 524 | out += mGestureConverter.handleGesture(when, readTime, mGestureStartTime, gesture); |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 525 | metricsAccumulator.processGesture(mMetricsId, gesture); |
| 526 | } |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 527 | } |
| 528 | mGesturesToProcess.clear(); |
| 529 | return out; |
| 530 | } |
| 531 | |
Josep del Rio | d074638 | 2023-07-29 13:18:25 +0000 | [diff] [blame] | 532 | std::optional<int32_t> TouchpadInputMapper::getAssociatedDisplayId() { |
| 533 | return mDisplayId; |
| 534 | } |
| 535 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 536 | } // namespace android |