blob: 588dc0c5201ba56af93872fbb830c98f23837646 [file] [log] [blame]
Harry Cutts79cc9fa2022-10-28 15:32:39 +00001/*
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 Cuttsa34de522023-06-06 15:52:54 +000019#include <algorithm>
Harry Cuttsbb24e272023-03-21 10:49:47 +000020#include <chrono>
Harry Cuttsa34de522023-06-06 15:52:54 +000021#include <iterator>
Harry Cuttsd35a24b2023-01-30 15:09:30 +000022#include <limits>
Harry Cuttsa34de522023-06-06 15:52:54 +000023#include <map>
Harry Cutts938c65d2023-12-13 19:04:02 +000024#include <mutex>
Harry Cuttsedf6ce72023-01-04 12:15:53 +000025#include <optional>
26
Siarhei Vishniakou9c933d02024-05-29 10:21:19 -070027#include <android-base/logging.h>
Harry Cuttsbb24e272023-03-21 10:49:47 +000028#include <android-base/stringprintf.h>
Harry Cutts938c65d2023-12-13 19:04:02 +000029#include <android-base/thread_annotations.h>
Harry Cutts74235542022-11-24 15:52:53 +000030#include <android/input.h>
Harry Cutts8c7cb592023-08-23 17:20:13 +000031#include <com_android_input_flags.h>
Harry Cutts2b67ff12023-03-13 11:32:06 +000032#include <ftl/enum.h>
Harry Cuttse78184b2024-01-08 15:54:58 +000033#include <input/AccelerationCurve.h>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000034#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000035#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000036#include <log/log_main.h>
Harry Cuttsa34de522023-06-06 15:52:54 +000037#include <stats_pull_atom_callback.h>
38#include <statslog.h>
Harry Cutts74235542022-11-24 15:52:53 +000039#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000040#include "TouchpadInputMapper.h"
Harry Cutts3952c832023-08-22 15:26:56 +000041#include "gestures/HardwareProperties.h"
Harry Cutts8c7cb592023-08-23 17:20:13 +000042#include "gestures/TimerProvider.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000043#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000044
Harry Cutts8c7cb592023-08-23 17:20:13 +000045namespace input_flags = com::android::input::flags;
46
Harry Cutts79cc9fa2022-10-28 15:32:39 +000047namespace android {
48
Harry Cutts1f48a442022-11-15 17:38:36 +000049namespace {
50
Harry Cuttsc5025372023-02-21 16:04:45 +000051/**
52 * Log details of each gesture output by the gestures library.
53 * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires
54 * restarting the shell)
55 */
56const bool DEBUG_TOUCHPAD_GESTURES =
57 __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures",
58 ANDROID_LOG_INFO);
59
Harry Cuttsd35a24b2023-01-30 15:09:30 +000060std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
61 size_t propertySize) {
Harry Cuttse78184b2024-01-08 15:54:58 +000062 std::vector<AccelerationCurveSegment> segments =
63 createAccelerationCurveForPointerSensitivity(sensitivity);
Harry Cuttsd35a24b2023-01-30 15:09:30 +000064 LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size());
65 std::vector<double> output(propertySize, 0);
66
67 // The Gestures library uses functions of the following form to define curve segments, where a,
68 // b, and c can be specified by us:
69 // output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c
70 //
71 // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.)
72 //
Harry Cuttse78184b2024-01-08 15:54:58 +000073 // createAccelerationCurveForPointerSensitivity gives us parameters for a function of the form:
74 // gain(input_speed_mm) = baseGain + reciprocal / input_speed_mm
Harry Cuttsd35a24b2023-01-30 15:09:30 +000075 // Where "gain" is a multiplier applied to the input speed to produce the output speed:
76 // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm)
77 //
78 // To put our function in the library's form, we substitute it into the function above:
Harry Cuttse78184b2024-01-08 15:54:58 +000079 // output_speed(input_speed_mm) = input_speed_mm * (baseGain + reciprocal / input_speed_mm)
80 // then expand the brackets so that input_speed_mm cancels out for the reciprocal term:
81 // gain(input_speed_mm) = baseGain * input_speed_mm + reciprocal
Harry Cuttsd35a24b2023-01-30 15:09:30 +000082 //
83 // This gives us the following parameters for the Gestures library function form:
84 // a = 0
Harry Cuttse78184b2024-01-08 15:54:58 +000085 // b = baseGain
86 // c = reciprocal
Harry Cuttsd35a24b2023-01-30 15:09:30 +000087
88 size_t i = 0;
Harry Cuttse78184b2024-01-08 15:54:58 +000089 for (AccelerationCurveSegment seg : segments) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +000090 // The library's curve format consists of four doubles per segment:
91 // * maximum pointer speed for the segment (mm/s)
92 // * multiplier for the x² term (a.k.a. "a" or "sqr")
93 // * multiplier for the x term (a.k.a. "b" or "mul")
94 // * the intercept (a.k.a. "c" or "int")
95 // (see struct CurveSegment in the library's AccelFilterInterpreter)
96 output[i + 0] = seg.maxPointerSpeedMmPerS;
97 output[i + 1] = 0;
Harry Cuttse78184b2024-01-08 15:54:58 +000098 output[i + 2] = seg.baseGain;
99 output[i + 3] = seg.reciprocal;
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000100 i += 4;
101 }
102
103 return output;
104}
105
Harry Cutts74235542022-11-24 15:52:53 +0000106void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
107 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
108 mapper->consumeGesture(gesture);
109}
110
Prabir Pradhan67d09ca2023-09-08 20:28:55 +0000111int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus, bool isUsiStylus) {
112 if (isUsiStylus) {
113 // This is a stylus connected over the Universal Stylus Initiative (USI) protocol.
114 // For metrics purposes, we treat this protocol as a separate bus.
115 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USI;
116 }
117
Harry Cuttsa34de522023-06-06 15:52:54 +0000118 // When adding cases to this switch, also add them to the copy of this method in
119 // InputDeviceMetricsCollector.cpp.
120 // TODO(b/286394420): deduplicate this method with the one in InputDeviceMetricsCollector.cpp.
121 switch (linuxBus) {
122 case BUS_USB:
123 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB;
124 case BUS_BLUETOOTH:
125 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH;
126 default:
127 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER;
128 }
129}
130
131class MetricsAccumulator {
132public:
133 static MetricsAccumulator& getInstance() {
134 static MetricsAccumulator sAccumulator;
135 return sAccumulator;
136 }
137
Harry Cutts938c65d2023-12-13 19:04:02 +0000138 void recordFinger(const TouchpadInputMapper::MetricsIdentifier& id) {
139 std::scoped_lock lock(mLock);
140 mCounters[id].fingers++;
141 }
Harry Cuttsa34de522023-06-06 15:52:54 +0000142
Harry Cutts938c65d2023-12-13 19:04:02 +0000143 void recordPalm(const TouchpadInputMapper::MetricsIdentifier& id) {
144 std::scoped_lock lock(mLock);
145 mCounters[id].palms++;
146 }
Harry Cuttsa34de522023-06-06 15:52:54 +0000147
148 // Checks whether a Gesture struct is for the end of a gesture that we log metrics for, and
149 // records it if so.
150 void processGesture(const TouchpadInputMapper::MetricsIdentifier& id, const Gesture& gesture) {
Harry Cutts938c65d2023-12-13 19:04:02 +0000151 std::scoped_lock lock(mLock);
Harry Cuttsa34de522023-06-06 15:52:54 +0000152 switch (gesture.type) {
153 case kGestureTypeFling:
154 if (gesture.details.fling.fling_state == GESTURES_FLING_START) {
155 // Indicates the end of a two-finger scroll gesture.
156 mCounters[id].twoFingerSwipeGestures++;
157 }
158 break;
159 case kGestureTypeSwipeLift:
160 mCounters[id].threeFingerSwipeGestures++;
161 break;
162 case kGestureTypeFourFingerSwipeLift:
163 mCounters[id].fourFingerSwipeGestures++;
164 break;
165 case kGestureTypePinch:
166 if (gesture.details.pinch.zoom_state == GESTURES_ZOOM_END) {
167 mCounters[id].pinchGestures++;
168 }
169 break;
170 default:
171 // We're not interested in any other gestures.
172 break;
173 }
174 }
175
176private:
177 MetricsAccumulator() {
178 AStatsManager_setPullAtomCallback(android::util::TOUCHPAD_USAGE, /*metadata=*/nullptr,
179 MetricsAccumulator::pullAtomCallback, /*cookie=*/nullptr);
180 }
181
182 ~MetricsAccumulator() { AStatsManager_clearPullAtomCallback(android::util::TOUCHPAD_USAGE); }
183
184 static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag,
185 AStatsEventList* outEventList,
186 void* cookie) {
187 LOG_ALWAYS_FATAL_IF(atomTag != android::util::TOUCHPAD_USAGE);
188 MetricsAccumulator& accumulator = MetricsAccumulator::getInstance();
Harry Cutts938c65d2023-12-13 19:04:02 +0000189 accumulator.produceAtomsAndReset(*outEventList);
Harry Cuttsa34de522023-06-06 15:52:54 +0000190 return AStatsManager_PULL_SUCCESS;
191 }
192
Harry Cutts938c65d2023-12-13 19:04:02 +0000193 void produceAtomsAndReset(AStatsEventList& outEventList) {
194 std::scoped_lock lock(mLock);
195 produceAtomsLocked(outEventList);
196 resetCountersLocked();
197 }
198
199 void produceAtomsLocked(AStatsEventList& outEventList) const REQUIRES(mLock) {
Harry Cuttsa34de522023-06-06 15:52:54 +0000200 for (auto& [id, counters] : mCounters) {
201 auto [busId, vendorId, productId, versionId] = id;
Harry Cutts938c65d2023-12-13 19:04:02 +0000202 addAStatsEvent(&outEventList, android::util::TOUCHPAD_USAGE, vendorId, productId,
Prabir Pradhan67d09ca2023-09-08 20:28:55 +0000203 versionId, linuxBusToInputDeviceBusEnum(busId, /*isUsi=*/false),
204 counters.fingers, counters.palms, counters.twoFingerSwipeGestures,
Harry Cuttsa34de522023-06-06 15:52:54 +0000205 counters.threeFingerSwipeGestures, counters.fourFingerSwipeGestures,
206 counters.pinchGestures);
207 }
208 }
209
Harry Cutts938c65d2023-12-13 19:04:02 +0000210 void resetCountersLocked() REQUIRES(mLock) { mCounters.clear(); }
Harry Cuttsa34de522023-06-06 15:52:54 +0000211
212 // Stores the counters for a specific touchpad model. Fields have the same meanings as those of
213 // the TouchpadUsage atom; see that definition for detailed documentation.
214 struct Counters {
215 int32_t fingers = 0;
216 int32_t palms = 0;
217
218 int32_t twoFingerSwipeGestures = 0;
219 int32_t threeFingerSwipeGestures = 0;
220 int32_t fourFingerSwipeGestures = 0;
221 int32_t pinchGestures = 0;
222 };
223
224 // Metrics are aggregated by device model and version, so if two devices of the same model and
225 // version are connected at once, they will have the same counters.
Harry Cutts938c65d2023-12-13 19:04:02 +0000226 std::map<TouchpadInputMapper::MetricsIdentifier, Counters> mCounters GUARDED_BY(mLock);
227
228 // Metrics are pulled by a binder thread, so we need to guard them with a mutex.
229 mutable std::mutex mLock;
Harry Cuttsa34de522023-06-06 15:52:54 +0000230};
231
Harry Cutts1f48a442022-11-15 17:38:36 +0000232} // namespace
233
Arpit Singh8e6fb252023-04-06 11:49:17 +0000234TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext,
235 const InputReaderConfiguration& readerConfig)
236 : InputMapper(deviceContext, readerConfig),
Harry Cutts1f48a442022-11-15 17:38:36 +0000237 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts8c7cb592023-08-23 17:20:13 +0000238 mTimerProvider(*getContext()),
Harry Cuttsbb24e272023-03-21 10:49:47 +0000239 mStateConverter(deviceContext, mMotionAccumulator),
240 mGestureConverter(*getContext(), deviceContext, getDeviceId()),
Harry Cuttsa34de522023-06-06 15:52:54 +0000241 mCapturedEventConverter(*getContext(), deviceContext, mMotionAccumulator, getDeviceId()),
Prabir Pradhan8b053512024-05-03 23:15:39 +0000242 mMetricsId(metricsIdFromInputDeviceIdentifier(deviceContext.getDeviceIdentifier())) {
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000243 if (std::optional<RawAbsoluteAxisInfo> slotAxis =
244 deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT);
245 slotAxis && slotAxis->maxValue >= 0) {
246 mMotionAccumulator.configure(deviceContext, slotAxis->maxValue + 1, true);
247 } else {
Siarhei Vishniakou9c933d02024-05-29 10:21:19 -0700248 LOG(WARNING) << "Touchpad " << deviceContext.getName()
249 << " doesn't have a valid ABS_MT_SLOT axis, and probably won't work properly.";
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000250 mMotionAccumulator.configure(deviceContext, 1, true);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000251 }
Harry Cuttsbb24e272023-03-21 10:49:47 +0000252
Harry Cutts1f48a442022-11-15 17:38:36 +0000253 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
254 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +0000255 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +0000256 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000257 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
258 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000259 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
260 &mPropertyProvider);
Omar Abdelmonemfd878632024-07-09 17:02:27 +0000261 mGestureInterpreter->SetTimerProvider(const_cast<GesturesTimerProvider*>(
262 &kGestureTimerProvider),
263 &mTimerProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000264 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000265}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000266
Harry Cutts74235542022-11-24 15:52:53 +0000267TouchpadInputMapper::~TouchpadInputMapper() {
Harry Cutts8c7cb592023-08-23 17:20:13 +0000268 // The gesture interpreter's destructor will try to free its property and timer providers,
269 // calling PropertyProvider::freeProperty and TimerProvider::freeTimer using a raw pointers.
270 // Depending on the declaration order in TouchpadInputMapper.h, those providers may have already
271 // been freed, causing allocation errors or use-after-free bugs. Depending on declaration order
272 // to avoid this seems rather fragile, so explicitly clear the providers here to ensure all the
273 // freeProperty and freeTimer calls happen before the providers are destructed.
Harry Cutts1b217912023-01-03 17:13:19 +0000274 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts8c7cb592023-08-23 17:20:13 +0000275 mGestureInterpreter->SetTimerProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000276}
277
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000278uint32_t TouchpadInputMapper::getSources() const {
279 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
280}
281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000283 InputMapper::populateDeviceInfo(info);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000284 if (mPointerCaptured) {
285 mCapturedEventConverter.populateMotionRanges(info);
286 } else {
287 mGestureConverter.populateMotionRanges(info);
288 }
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000289}
290
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000291void TouchpadInputMapper::dump(std::string& dump) {
292 dump += INDENT2 "Touchpad Input Mapper:\n";
Harry Cuttsbb24e272023-03-21 10:49:47 +0000293 if (mResettingInterpreter) {
294 dump += INDENT3 "Currently resetting gesture interpreter\n";
295 }
296 dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured));
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000297 dump += INDENT3 "Gesture converter:\n";
298 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
299 dump += INDENT3 "Gesture properties:\n";
300 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
Omar Abdelmonemfd878632024-07-09 17:02:27 +0000301 dump += INDENT3 "Timer provider:\n";
302 dump += addLinePrefix(mTimerProvider.dump(), INDENT4);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000303 dump += INDENT3 "Captured event converter:\n";
304 dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4);
Linnan Li13bf76a2024-05-05 19:18:02 +0800305 dump += StringPrintf(INDENT3 "DisplayId: %s\n",
306 toString(mDisplayId, streamableToString).c_str());
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000307}
308
Arpit Singh4be4eef2023-03-28 14:26:01 +0000309std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000310 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000311 ConfigurationChanges changes) {
312 if (!changes.any()) {
Harry Cutts2b67ff12023-03-13 11:32:06 +0000313 // First time configuration
314 mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration());
315 }
316
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000317 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700318 mDisplayId = ui::LogicalDisplayId::INVALID;
Byoungho Jungee6268f2023-10-30 17:27:26 +0900319 std::optional<DisplayViewport> resolvedViewport;
320 std::optional<FloatRect> boundsInLogicalDisplay;
321 if (auto assocViewport = mDeviceContext.getAssociatedViewport(); assocViewport) {
Josep del Riod0746382023-07-29 13:18:25 +0000322 // This InputDevice is associated with a viewport.
323 // Only generate events for the associated display.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900324 mDisplayId = assocViewport->displayId;
325 resolvedViewport = *assocViewport;
Josep del Riod0746382023-07-29 13:18:25 +0000326 } else {
327 // The InputDevice is not associated with a viewport, but it controls the mouse pointer.
Prabir Pradhan8b053512024-05-03 23:15:39 +0000328 // Always use DISPLAY_ID_NONE for touchpad events.
329 // PointerChoreographer will make it target the correct the displayId later.
330 resolvedViewport = getContext()->getPolicy()->getPointerViewportForAssociatedDisplay();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700331 mDisplayId = resolvedViewport ? std::make_optional(ui::LogicalDisplayId::INVALID)
332 : std::nullopt;
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000333 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900334
Josep del Riod0746382023-07-29 13:18:25 +0000335 mGestureConverter.setDisplayId(mDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900336 mGestureConverter.setOrientation(resolvedViewport
337 ? getInverseRotation(resolvedViewport->orientation)
338 : ui::ROTATION_0);
339
340 if (!boundsInLogicalDisplay) {
341 boundsInLogicalDisplay = resolvedViewport
342 ? FloatRect{static_cast<float>(resolvedViewport->logicalLeft),
343 static_cast<float>(resolvedViewport->logicalTop),
344 static_cast<float>(resolvedViewport->logicalRight - 1),
345 static_cast<float>(resolvedViewport->logicalBottom - 1)}
346 : FloatRect{0, 0, 0, 0};
347 }
348 mGestureConverter.setBoundsInLogicalDisplay(*boundsInLogicalDisplay);
Harry Cutts5ab90572024-01-08 14:00:48 +0000349
350 bumpGeneration();
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000351 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000352 if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000353 mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve")
354 .setBoolValues({true});
355 GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
356 accelCurveProp.setRealValues(
Arpit Singhed6c3de2023-04-05 19:24:37 +0000357 createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000358 accelCurveProp.getCount()));
Wenxin Feng1ca50662023-05-03 14:00:12 -0700359 mPropertyProvider.getProperty("Use Custom Touchpad Scroll Accel Curve")
360 .setBoolValues({true});
361 GesturesProp scrollCurveProp = mPropertyProvider.getProperty("Scroll Accel Curve");
362 scrollCurveProp.setRealValues(
363 createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
364 scrollCurveProp.getCount()));
365 mPropertyProvider.getProperty("Scroll X Out Scale").setRealValues({1.0});
366 mPropertyProvider.getProperty("Scroll Y Out Scale").setRealValues({1.0});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000367 mPropertyProvider.getProperty("Invert Scrolling")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000368 .setBoolValues({config.touchpadNaturalScrollingEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000369 mPropertyProvider.getProperty("Tap Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000370 .setBoolValues({config.touchpadTapToClickEnabled});
Harry Cuttse0e799d2024-01-24 16:27:56 +0000371 mPropertyProvider.getProperty("Tap Drag Enable")
372 .setBoolValues({config.touchpadTapDraggingEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000373 mPropertyProvider.getProperty("Button Right Click Zone Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000374 .setBoolValues({config.touchpadRightClickZoneEnabled});
Abdelrahman Awadalla61d0da32024-08-12 17:02:13 +0000375 mTouchpadHardwareStateNotificationsEnabled = config.shouldNotifyTouchpadHardwareState;
Harry Cuttsa546ba82023-01-13 17:21:00 +0000376 }
Harry Cuttsbb24e272023-03-21 10:49:47 +0000377 std::list<NotifyArgs> out;
Hiroki Sato25040232024-02-22 17:21:22 +0900378 if ((!changes.any() && config.pointerCaptureRequest.isEnable()) ||
Harry Cuttsbb24e272023-03-21 10:49:47 +0000379 changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) {
Hiroki Sato25040232024-02-22 17:21:22 +0900380 mPointerCaptured = config.pointerCaptureRequest.isEnable();
Harry Cuttsbb24e272023-03-21 10:49:47 +0000381 // The motion ranges are going to change, so bump the generation to clear the cached ones.
382 bumpGeneration();
383 if (mPointerCaptured) {
384 // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are
385 // still being reported for a gesture in progress.
386 out += reset(when);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000387 } else {
388 // We're transitioning from captured to uncaptured.
389 mCapturedEventConverter.reset();
390 }
391 if (changes.any()) {
392 out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
393 }
394 }
395 return out;
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000396}
397
Harry Cutts1f48a442022-11-15 17:38:36 +0000398std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000399 mStateConverter.reset();
Harry Cuttsbb24e272023-03-21 10:49:47 +0000400 resetGestureInterpreter(when);
Harry Cuttse9b71422023-03-14 16:54:44 +0000401 std::list<NotifyArgs> out = mGestureConverter.reset(when);
402 out += InputMapper::reset(when);
403 return out;
Harry Cutts1f48a442022-11-15 17:38:36 +0000404}
405
Harry Cuttsbb24e272023-03-21 10:49:47 +0000406void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) {
407 // The GestureInterpreter has no official reset method, but sending a HardwareState with no
408 // fingers down or buttons pressed should get it into a clean state.
409 HardwareState state;
410 state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count();
411 mResettingInterpreter = true;
412 mGestureInterpreter->PushHardwareState(&state);
413 mResettingInterpreter = false;
414}
415
Harry Cuttsa32a1192024-06-04 15:10:31 +0000416std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent& rawEvent) {
Harry Cuttsbb24e272023-03-21 10:49:47 +0000417 if (mPointerCaptured) {
Harry Cuttsa32a1192024-06-04 15:10:31 +0000418 return mCapturedEventConverter.process(rawEvent);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000419 }
Arpit Singh33a10a62023-10-12 13:06:54 +0000420 if (mMotionAccumulator.getActiveSlotsCount() == 0) {
Harry Cuttsa32a1192024-06-04 15:10:31 +0000421 mGestureStartTime = rawEvent.when;
Arpit Singh33a10a62023-10-12 13:06:54 +0000422 }
Harry Cuttsa32a1192024-06-04 15:10:31 +0000423 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
Harry Cutts47db1c72022-12-13 19:20:47 +0000424 if (state) {
Abdelrahman Awadalla61d0da32024-08-12 17:02:13 +0000425 if (mTouchpadHardwareStateNotificationsEnabled) {
426 // TODO(b/286551975): Notify policy of the touchpad hardware state.
427 LOG(DEBUG) << "Notify touchpad hardware state here!";
428 }
429
Harry Cuttsa34de522023-06-06 15:52:54 +0000430 updatePalmDetectionMetrics();
Harry Cuttsa32a1192024-06-04 15:10:31 +0000431 return sendHardwareState(rawEvent.when, rawEvent.readTime, *state);
Harry Cutts47db1c72022-12-13 19:20:47 +0000432 } else {
433 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000434 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000435}
436
Harry Cuttsa34de522023-06-06 15:52:54 +0000437void 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 Cutts47db1c72022-12-13 19:20:47 +0000463std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
464 SelfContainedHardwareState schs) {
Harry Cutts287e19f2023-02-27 17:09:24 +0000465 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str());
Harry Cutts47db1c72022-12-13 19:20:47 +0000466 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts47db1c72022-12-13 19:20:47 +0000467 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000468}
469
Harry Cutts8c7cb592023-08-23 17:20:13 +0000470std::list<NotifyArgs> TouchpadInputMapper::timeoutExpired(nsecs_t when) {
Harry Cutts8c7cb592023-08-23 17:20:13 +0000471 mTimerProvider.triggerCallbacks(when);
472 return processGestures(when, when);
473}
474
Harry Cutts74235542022-11-24 15:52:53 +0000475void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
Harry Cuttsc5025372023-02-21 16:04:45 +0000476 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str());
Harry Cuttsbb24e272023-03-21 10:49:47 +0000477 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 Cutts74235542022-11-24 15:52:53 +0000482 mGesturesToProcess.push_back(*gesture);
483}
484
485std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
486 std::list<NotifyArgs> out = {};
Josep del Riod0746382023-07-29 13:18:25 +0000487 if (mDisplayId) {
488 MetricsAccumulator& metricsAccumulator = MetricsAccumulator::getInstance();
489 for (Gesture& gesture : mGesturesToProcess) {
Arpit Singh33a10a62023-10-12 13:06:54 +0000490 out += mGestureConverter.handleGesture(when, readTime, mGestureStartTime, gesture);
Josep del Riod0746382023-07-29 13:18:25 +0000491 metricsAccumulator.processGesture(mMetricsId, gesture);
492 }
Harry Cutts74235542022-11-24 15:52:53 +0000493 }
494 mGesturesToProcess.clear();
495 return out;
496}
497
Linnan Li13bf76a2024-05-05 19:18:02 +0800498std::optional<ui::LogicalDisplayId> TouchpadInputMapper::getAssociatedDisplayId() {
Josep del Riod0746382023-07-29 13:18:25 +0000499 return mDisplayId;
500}
501
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000502} // namespace android