blob: bdc164029c4d0e4ffb455589ec48adeee8fb5cb8 [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
Harry Cuttsbb24e272023-03-21 10:49:47 +000027#include <android-base/stringprintf.h>
Harry Cutts938c65d2023-12-13 19:04:02 +000028#include <android-base/thread_annotations.h>
Harry Cutts74235542022-11-24 15:52:53 +000029#include <android/input.h>
Harry Cutts8c7cb592023-08-23 17:20:13 +000030#include <com_android_input_flags.h>
Harry Cutts2b67ff12023-03-13 11:32:06 +000031#include <ftl/enum.h>
Harry Cuttse78184b2024-01-08 15:54:58 +000032#include <input/AccelerationCurve.h>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000033#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000034#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000035#include <log/log_main.h>
Harry Cuttsa34de522023-06-06 15:52:54 +000036#include <stats_pull_atom_callback.h>
37#include <statslog.h>
Harry Cutts74235542022-11-24 15:52:53 +000038#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000039#include "TouchpadInputMapper.h"
Harry Cutts3952c832023-08-22 15:26:56 +000040#include "gestures/HardwareProperties.h"
Harry Cutts8c7cb592023-08-23 17:20:13 +000041#include "gestures/TimerProvider.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000042#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000043
Harry Cutts8c7cb592023-08-23 17:20:13 +000044namespace input_flags = com::android::input::flags;
45
Harry Cutts79cc9fa2022-10-28 15:32:39 +000046namespace android {
47
Harry Cutts1f48a442022-11-15 17:38:36 +000048namespace {
49
Harry Cuttsc5025372023-02-21 16:04:45 +000050/**
51 * Log details of each gesture output by the gestures library.
52 * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires
53 * restarting the shell)
54 */
55const bool DEBUG_TOUCHPAD_GESTURES =
56 __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures",
57 ANDROID_LOG_INFO);
58
Harry Cuttsd35a24b2023-01-30 15:09:30 +000059std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
60 size_t propertySize) {
Harry Cuttse78184b2024-01-08 15:54:58 +000061 std::vector<AccelerationCurveSegment> segments =
62 createAccelerationCurveForPointerSensitivity(sensitivity);
Harry Cuttsd35a24b2023-01-30 15:09:30 +000063 LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size());
64 std::vector<double> output(propertySize, 0);
65
66 // The Gestures library uses functions of the following form to define curve segments, where a,
67 // b, and c can be specified by us:
68 // output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c
69 //
70 // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.)
71 //
Harry Cuttse78184b2024-01-08 15:54:58 +000072 // createAccelerationCurveForPointerSensitivity gives us parameters for a function of the form:
73 // gain(input_speed_mm) = baseGain + reciprocal / input_speed_mm
Harry Cuttsd35a24b2023-01-30 15:09:30 +000074 // Where "gain" is a multiplier applied to the input speed to produce the output speed:
75 // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm)
76 //
77 // To put our function in the library's form, we substitute it into the function above:
Harry Cuttse78184b2024-01-08 15:54:58 +000078 // output_speed(input_speed_mm) = input_speed_mm * (baseGain + reciprocal / input_speed_mm)
79 // then expand the brackets so that input_speed_mm cancels out for the reciprocal term:
80 // gain(input_speed_mm) = baseGain * input_speed_mm + reciprocal
Harry Cuttsd35a24b2023-01-30 15:09:30 +000081 //
82 // This gives us the following parameters for the Gestures library function form:
83 // a = 0
Harry Cuttse78184b2024-01-08 15:54:58 +000084 // b = baseGain
85 // c = reciprocal
Harry Cuttsd35a24b2023-01-30 15:09:30 +000086
87 size_t i = 0;
Harry Cuttse78184b2024-01-08 15:54:58 +000088 for (AccelerationCurveSegment seg : segments) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +000089 // The library's curve format consists of four doubles per segment:
90 // * maximum pointer speed for the segment (mm/s)
91 // * multiplier for the x² term (a.k.a. "a" or "sqr")
92 // * multiplier for the x term (a.k.a. "b" or "mul")
93 // * the intercept (a.k.a. "c" or "int")
94 // (see struct CurveSegment in the library's AccelFilterInterpreter)
95 output[i + 0] = seg.maxPointerSpeedMmPerS;
96 output[i + 1] = 0;
Harry Cuttse78184b2024-01-08 15:54:58 +000097 output[i + 2] = seg.baseGain;
98 output[i + 3] = seg.reciprocal;
Harry Cuttsd35a24b2023-01-30 15:09:30 +000099 i += 4;
100 }
101
102 return output;
103}
104
Harry Cutts74235542022-11-24 15:52:53 +0000105void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
106 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
107 mapper->consumeGesture(gesture);
108}
109
Prabir Pradhan67d09ca2023-09-08 20:28:55 +0000110int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus, bool isUsiStylus) {
111 if (isUsiStylus) {
112 // This is a stylus connected over the Universal Stylus Initiative (USI) protocol.
113 // For metrics purposes, we treat this protocol as a separate bus.
114 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USI;
115 }
116
Harry Cuttsa34de522023-06-06 15:52:54 +0000117 // When adding cases to this switch, also add them to the copy of this method in
118 // InputDeviceMetricsCollector.cpp.
119 // TODO(b/286394420): deduplicate this method with the one in InputDeviceMetricsCollector.cpp.
120 switch (linuxBus) {
121 case BUS_USB:
122 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB;
123 case BUS_BLUETOOTH:
124 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH;
125 default:
126 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER;
127 }
128}
129
130class MetricsAccumulator {
131public:
132 static MetricsAccumulator& getInstance() {
133 static MetricsAccumulator sAccumulator;
134 return sAccumulator;
135 }
136
Harry Cutts938c65d2023-12-13 19:04:02 +0000137 void recordFinger(const TouchpadInputMapper::MetricsIdentifier& id) {
138 std::scoped_lock lock(mLock);
139 mCounters[id].fingers++;
140 }
Harry Cuttsa34de522023-06-06 15:52:54 +0000141
Harry Cutts938c65d2023-12-13 19:04:02 +0000142 void recordPalm(const TouchpadInputMapper::MetricsIdentifier& id) {
143 std::scoped_lock lock(mLock);
144 mCounters[id].palms++;
145 }
Harry Cuttsa34de522023-06-06 15:52:54 +0000146
147 // Checks whether a Gesture struct is for the end of a gesture that we log metrics for, and
148 // records it if so.
149 void processGesture(const TouchpadInputMapper::MetricsIdentifier& id, const Gesture& gesture) {
Harry Cutts938c65d2023-12-13 19:04:02 +0000150 std::scoped_lock lock(mLock);
Harry Cuttsa34de522023-06-06 15:52:54 +0000151 switch (gesture.type) {
152 case kGestureTypeFling:
153 if (gesture.details.fling.fling_state == GESTURES_FLING_START) {
154 // Indicates the end of a two-finger scroll gesture.
155 mCounters[id].twoFingerSwipeGestures++;
156 }
157 break;
158 case kGestureTypeSwipeLift:
159 mCounters[id].threeFingerSwipeGestures++;
160 break;
161 case kGestureTypeFourFingerSwipeLift:
162 mCounters[id].fourFingerSwipeGestures++;
163 break;
164 case kGestureTypePinch:
165 if (gesture.details.pinch.zoom_state == GESTURES_ZOOM_END) {
166 mCounters[id].pinchGestures++;
167 }
168 break;
169 default:
170 // We're not interested in any other gestures.
171 break;
172 }
173 }
174
175private:
176 MetricsAccumulator() {
177 AStatsManager_setPullAtomCallback(android::util::TOUCHPAD_USAGE, /*metadata=*/nullptr,
178 MetricsAccumulator::pullAtomCallback, /*cookie=*/nullptr);
179 }
180
181 ~MetricsAccumulator() { AStatsManager_clearPullAtomCallback(android::util::TOUCHPAD_USAGE); }
182
183 static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag,
184 AStatsEventList* outEventList,
185 void* cookie) {
186 LOG_ALWAYS_FATAL_IF(atomTag != android::util::TOUCHPAD_USAGE);
187 MetricsAccumulator& accumulator = MetricsAccumulator::getInstance();
Harry Cutts938c65d2023-12-13 19:04:02 +0000188 accumulator.produceAtomsAndReset(*outEventList);
Harry Cuttsa34de522023-06-06 15:52:54 +0000189 return AStatsManager_PULL_SUCCESS;
190 }
191
Harry Cutts938c65d2023-12-13 19:04:02 +0000192 void produceAtomsAndReset(AStatsEventList& outEventList) {
193 std::scoped_lock lock(mLock);
194 produceAtomsLocked(outEventList);
195 resetCountersLocked();
196 }
197
198 void produceAtomsLocked(AStatsEventList& outEventList) const REQUIRES(mLock) {
Harry Cuttsa34de522023-06-06 15:52:54 +0000199 for (auto& [id, counters] : mCounters) {
200 auto [busId, vendorId, productId, versionId] = id;
Harry Cutts938c65d2023-12-13 19:04:02 +0000201 addAStatsEvent(&outEventList, android::util::TOUCHPAD_USAGE, vendorId, productId,
Prabir Pradhan67d09ca2023-09-08 20:28:55 +0000202 versionId, linuxBusToInputDeviceBusEnum(busId, /*isUsi=*/false),
203 counters.fingers, counters.palms, counters.twoFingerSwipeGestures,
Harry Cuttsa34de522023-06-06 15:52:54 +0000204 counters.threeFingerSwipeGestures, counters.fourFingerSwipeGestures,
205 counters.pinchGestures);
206 }
207 }
208
Harry Cutts938c65d2023-12-13 19:04:02 +0000209 void resetCountersLocked() REQUIRES(mLock) { mCounters.clear(); }
Harry Cuttsa34de522023-06-06 15:52:54 +0000210
211 // Stores the counters for a specific touchpad model. Fields have the same meanings as those of
212 // the TouchpadUsage atom; see that definition for detailed documentation.
213 struct Counters {
214 int32_t fingers = 0;
215 int32_t palms = 0;
216
217 int32_t twoFingerSwipeGestures = 0;
218 int32_t threeFingerSwipeGestures = 0;
219 int32_t fourFingerSwipeGestures = 0;
220 int32_t pinchGestures = 0;
221 };
222
223 // Metrics are aggregated by device model and version, so if two devices of the same model and
224 // version are connected at once, they will have the same counters.
Harry Cutts938c65d2023-12-13 19:04:02 +0000225 std::map<TouchpadInputMapper::MetricsIdentifier, Counters> mCounters GUARDED_BY(mLock);
226
227 // Metrics are pulled by a binder thread, so we need to guard them with a mutex.
228 mutable std::mutex mLock;
Harry Cuttsa34de522023-06-06 15:52:54 +0000229};
230
Harry Cutts1f48a442022-11-15 17:38:36 +0000231} // namespace
232
Arpit Singh8e6fb252023-04-06 11:49:17 +0000233TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext,
234 const InputReaderConfiguration& readerConfig)
235 : InputMapper(deviceContext, readerConfig),
Harry Cutts1f48a442022-11-15 17:38:36 +0000236 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +0000237 mPointerController(getContext()->getPointerController(getDeviceId())),
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()),
Byoungho Jungee6268f2023-10-30 17:27:26 +0900242 mMetricsId(metricsIdFromInputDeviceIdentifier(deviceContext.getDeviceIdentifier())),
243 mEnablePointerChoreographer(input_flags::enable_pointer_choreographer()) {
Harry Cuttsbb24e272023-03-21 10:49:47 +0000244 RawAbsoluteAxisInfo slotAxisInfo;
245 deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo);
246 if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) {
247 ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work "
248 "properly.",
249 deviceContext.getName().c_str());
250 }
251 mMotionAccumulator.configure(deviceContext, slotAxisInfo.maxValue + 1, true);
252
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);
Harry Cutts8c7cb592023-08-23 17:20:13 +0000261 if (input_flags::enable_gestures_library_timer_provider()) {
262 mGestureInterpreter->SetTimerProvider(const_cast<GesturesTimerProvider*>(
263 &kGestureTimerProvider),
264 &mTimerProvider);
265 }
Harry Cutts74235542022-11-24 15:52:53 +0000266 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000267}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000268
Harry Cutts74235542022-11-24 15:52:53 +0000269TouchpadInputMapper::~TouchpadInputMapper() {
270 if (mPointerController != nullptr) {
271 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
272 }
Harry Cutts1b217912023-01-03 17:13:19 +0000273
Harry Cutts8c7cb592023-08-23 17:20:13 +0000274 // The gesture interpreter's destructor will try to free its property and timer providers,
275 // calling PropertyProvider::freeProperty and TimerProvider::freeTimer using a raw pointers.
276 // Depending on the declaration order in TouchpadInputMapper.h, those providers may have already
277 // been freed, causing allocation errors or use-after-free bugs. Depending on declaration order
278 // to avoid this seems rather fragile, so explicitly clear the providers here to ensure all the
279 // freeProperty and freeTimer calls happen before the providers are destructed.
Harry Cutts1b217912023-01-03 17:13:19 +0000280 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts8c7cb592023-08-23 17:20:13 +0000281 mGestureInterpreter->SetTimerProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000282}
283
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000284uint32_t TouchpadInputMapper::getSources() const {
285 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
286}
287
Harry Cuttsd02ea102023-03-17 18:21:30 +0000288void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000289 InputMapper::populateDeviceInfo(info);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000290 if (mPointerCaptured) {
291 mCapturedEventConverter.populateMotionRanges(info);
292 } else {
293 mGestureConverter.populateMotionRanges(info);
294 }
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000295}
296
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000297void TouchpadInputMapper::dump(std::string& dump) {
298 dump += INDENT2 "Touchpad Input Mapper:\n";
Harry Cuttsbb24e272023-03-21 10:49:47 +0000299 if (mResettingInterpreter) {
300 dump += INDENT3 "Currently resetting gesture interpreter\n";
301 }
302 dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured));
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000303 dump += INDENT3 "Gesture converter:\n";
304 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
305 dump += INDENT3 "Gesture properties:\n";
306 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
Harry Cutts8c7cb592023-08-23 17:20:13 +0000307 if (input_flags::enable_gestures_library_timer_provider()) {
308 dump += INDENT3 "Timer provider:\n";
309 dump += addLinePrefix(mTimerProvider.dump(), INDENT4);
310 } else {
311 dump += INDENT3 "Timer provider: disabled by flag\n";
312 }
Harry Cuttsbb24e272023-03-21 10:49:47 +0000313 dump += INDENT3 "Captured event converter:\n";
314 dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4);
Josep del Riod0746382023-07-29 13:18:25 +0000315 dump += StringPrintf(INDENT3 "DisplayId: %s\n", toString(mDisplayId).c_str());
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000316}
317
Arpit Singh4be4eef2023-03-28 14:26:01 +0000318std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000319 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000320 ConfigurationChanges changes) {
321 if (!changes.any()) {
Harry Cutts2b67ff12023-03-13 11:32:06 +0000322 // First time configuration
323 mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration());
324 }
325
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000326 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Josep del Riod0746382023-07-29 13:18:25 +0000327 mDisplayId = ADISPLAY_ID_NONE;
Byoungho Jungee6268f2023-10-30 17:27:26 +0900328 std::optional<DisplayViewport> resolvedViewport;
329 std::optional<FloatRect> boundsInLogicalDisplay;
330 if (auto assocViewport = mDeviceContext.getAssociatedViewport(); assocViewport) {
Josep del Riod0746382023-07-29 13:18:25 +0000331 // This InputDevice is associated with a viewport.
332 // Only generate events for the associated display.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900333 mDisplayId = assocViewport->displayId;
334 resolvedViewport = *assocViewport;
335 if (!mEnablePointerChoreographer) {
336 const bool mismatchedPointerDisplay =
337 (assocViewport->displayId != mPointerController->getDisplayId());
338 if (mismatchedPointerDisplay) {
339 ALOGW("Touchpad \"%s\" associated viewport display does not match pointer "
340 "controller",
341 mDeviceContext.getName().c_str());
342 mDisplayId.reset();
343 }
Josep del Riod0746382023-07-29 13:18:25 +0000344 }
Josep del Riod0746382023-07-29 13:18:25 +0000345 } else {
346 // The InputDevice is not associated with a viewport, but it controls the mouse pointer.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900347 if (mEnablePointerChoreographer) {
348 // Always use DISPLAY_ID_NONE for touchpad events.
349 // PointerChoreographer will make it target the correct the displayId later.
350 resolvedViewport =
351 getContext()->getPolicy()->getPointerViewportForAssociatedDisplay();
352 mDisplayId = resolvedViewport ? std::make_optional(ADISPLAY_ID_NONE) : std::nullopt;
353 } else {
354 mDisplayId = mPointerController->getDisplayId();
355 if (auto v = config.getDisplayViewportById(*mDisplayId); v) {
356 resolvedViewport = *v;
357 }
358 if (auto bounds = mPointerController->getBounds(); bounds) {
359 boundsInLogicalDisplay = *bounds;
360 }
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000361 }
362 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900363
Josep del Riod0746382023-07-29 13:18:25 +0000364 mGestureConverter.setDisplayId(mDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900365 mGestureConverter.setOrientation(resolvedViewport
366 ? getInverseRotation(resolvedViewport->orientation)
367 : ui::ROTATION_0);
368
369 if (!boundsInLogicalDisplay) {
370 boundsInLogicalDisplay = resolvedViewport
371 ? FloatRect{static_cast<float>(resolvedViewport->logicalLeft),
372 static_cast<float>(resolvedViewport->logicalTop),
373 static_cast<float>(resolvedViewport->logicalRight - 1),
374 static_cast<float>(resolvedViewport->logicalBottom - 1)}
375 : FloatRect{0, 0, 0, 0};
376 }
377 mGestureConverter.setBoundsInLogicalDisplay(*boundsInLogicalDisplay);
Harry Cutts5ab90572024-01-08 14:00:48 +0000378
379 bumpGeneration();
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000380 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000381 if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000382 mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve")
383 .setBoolValues({true});
384 GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
385 accelCurveProp.setRealValues(
Arpit Singhed6c3de2023-04-05 19:24:37 +0000386 createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000387 accelCurveProp.getCount()));
Wenxin Feng1ca50662023-05-03 14:00:12 -0700388 mPropertyProvider.getProperty("Use Custom Touchpad Scroll Accel Curve")
389 .setBoolValues({true});
390 GesturesProp scrollCurveProp = mPropertyProvider.getProperty("Scroll Accel Curve");
391 scrollCurveProp.setRealValues(
392 createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
393 scrollCurveProp.getCount()));
394 mPropertyProvider.getProperty("Scroll X Out Scale").setRealValues({1.0});
395 mPropertyProvider.getProperty("Scroll Y Out Scale").setRealValues({1.0});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000396 mPropertyProvider.getProperty("Invert Scrolling")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000397 .setBoolValues({config.touchpadNaturalScrollingEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000398 mPropertyProvider.getProperty("Tap Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000399 .setBoolValues({config.touchpadTapToClickEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000400 mPropertyProvider.getProperty("Button Right Click Zone Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000401 .setBoolValues({config.touchpadRightClickZoneEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000402 }
Harry Cuttsbb24e272023-03-21 10:49:47 +0000403 std::list<NotifyArgs> out;
404 if ((!changes.any() && config.pointerCaptureRequest.enable) ||
405 changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) {
406 mPointerCaptured = config.pointerCaptureRequest.enable;
407 // The motion ranges are going to change, so bump the generation to clear the cached ones.
408 bumpGeneration();
409 if (mPointerCaptured) {
410 // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are
411 // still being reported for a gesture in progress.
412 out += reset(when);
413 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
414 } else {
415 // We're transitioning from captured to uncaptured.
416 mCapturedEventConverter.reset();
417 }
418 if (changes.any()) {
419 out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
420 }
421 }
422 return out;
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000423}
424
Harry Cutts1f48a442022-11-15 17:38:36 +0000425std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000426 mStateConverter.reset();
Harry Cuttsbb24e272023-03-21 10:49:47 +0000427 resetGestureInterpreter(when);
Harry Cuttse9b71422023-03-14 16:54:44 +0000428 std::list<NotifyArgs> out = mGestureConverter.reset(when);
429 out += InputMapper::reset(when);
430 return out;
Harry Cutts1f48a442022-11-15 17:38:36 +0000431}
432
Harry Cuttsbb24e272023-03-21 10:49:47 +0000433void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) {
434 // The GestureInterpreter has no official reset method, but sending a HardwareState with no
435 // fingers down or buttons pressed should get it into a clean state.
436 HardwareState state;
437 state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count();
438 mResettingInterpreter = true;
439 mGestureInterpreter->PushHardwareState(&state);
440 mResettingInterpreter = false;
441}
442
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000443std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cuttsbb24e272023-03-21 10:49:47 +0000444 if (mPointerCaptured) {
445 return mCapturedEventConverter.process(*rawEvent);
446 }
Arpit Singh33a10a62023-10-12 13:06:54 +0000447 if (mMotionAccumulator.getActiveSlotsCount() == 0) {
448 mGestureStartTime = rawEvent->when;
449 }
Harry Cutts47db1c72022-12-13 19:20:47 +0000450 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
451 if (state) {
Harry Cuttsa34de522023-06-06 15:52:54 +0000452 updatePalmDetectionMetrics();
Harry Cutts47db1c72022-12-13 19:20:47 +0000453 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
454 } else {
455 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000456 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000457}
458
Harry Cuttsa34de522023-06-06 15:52:54 +0000459void TouchpadInputMapper::updatePalmDetectionMetrics() {
460 std::set<int32_t> currentTrackingIds;
461 for (size_t i = 0; i < mMotionAccumulator.getSlotCount(); i++) {
462 const MultiTouchMotionAccumulator::Slot& slot = mMotionAccumulator.getSlot(i);
463 if (!slot.isInUse()) {
464 continue;
465 }
466 currentTrackingIds.insert(slot.getTrackingId());
467 if (slot.getToolType() == ToolType::PALM) {
468 mPalmTrackingIds.insert(slot.getTrackingId());
469 }
470 }
471 std::vector<int32_t> liftedTouches;
472 std::set_difference(mLastFrameTrackingIds.begin(), mLastFrameTrackingIds.end(),
473 currentTrackingIds.begin(), currentTrackingIds.end(),
474 std::inserter(liftedTouches, liftedTouches.begin()));
475 for (int32_t trackingId : liftedTouches) {
476 if (mPalmTrackingIds.erase(trackingId) > 0) {
477 MetricsAccumulator::getInstance().recordPalm(mMetricsId);
478 } else {
479 MetricsAccumulator::getInstance().recordFinger(mMetricsId);
480 }
481 }
482 mLastFrameTrackingIds = currentTrackingIds;
483}
484
Harry Cutts47db1c72022-12-13 19:20:47 +0000485std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
486 SelfContainedHardwareState schs) {
Harry Cutts287e19f2023-02-27 17:09:24 +0000487 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str());
Harry Cutts47db1c72022-12-13 19:20:47 +0000488 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts47db1c72022-12-13 19:20:47 +0000489 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000490}
491
Harry Cutts8c7cb592023-08-23 17:20:13 +0000492std::list<NotifyArgs> TouchpadInputMapper::timeoutExpired(nsecs_t when) {
493 if (!input_flags::enable_gestures_library_timer_provider()) {
494 return {};
495 }
496 mTimerProvider.triggerCallbacks(when);
497 return processGestures(when, when);
498}
499
Harry Cutts74235542022-11-24 15:52:53 +0000500void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
Harry Cuttsc5025372023-02-21 16:04:45 +0000501 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str());
Harry Cuttsbb24e272023-03-21 10:49:47 +0000502 if (mResettingInterpreter) {
503 // We already handle tidying up fake fingers etc. in GestureConverter::reset, so we should
504 // ignore any gestures produced from the interpreter while we're resetting it.
505 return;
506 }
Harry Cutts74235542022-11-24 15:52:53 +0000507 mGesturesToProcess.push_back(*gesture);
508}
509
510std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
511 std::list<NotifyArgs> out = {};
Josep del Riod0746382023-07-29 13:18:25 +0000512 if (mDisplayId) {
513 MetricsAccumulator& metricsAccumulator = MetricsAccumulator::getInstance();
514 for (Gesture& gesture : mGesturesToProcess) {
Arpit Singh33a10a62023-10-12 13:06:54 +0000515 out += mGestureConverter.handleGesture(when, readTime, mGestureStartTime, gesture);
Josep del Riod0746382023-07-29 13:18:25 +0000516 metricsAccumulator.processGesture(mMetricsId, gesture);
517 }
Harry Cutts74235542022-11-24 15:52:53 +0000518 }
519 mGesturesToProcess.clear();
520 return out;
521}
522
Josep del Riod0746382023-07-29 13:18:25 +0000523std::optional<int32_t> TouchpadInputMapper::getAssociatedDisplayId() {
524 return mDisplayId;
525}
526
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000527} // namespace android