blob: a5da3cdccc17496a4f183f21d97cc6619c894f62 [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 Cuttsbb24e272023-03-21 10:49:47 +000019#include <chrono>
Harry Cuttsd35a24b2023-01-30 15:09:30 +000020#include <limits>
Harry Cuttsedf6ce72023-01-04 12:15:53 +000021#include <optional>
22
Harry Cuttsbb24e272023-03-21 10:49:47 +000023#include <android-base/stringprintf.h>
Harry Cutts74235542022-11-24 15:52:53 +000024#include <android/input.h>
Harry Cutts2b67ff12023-03-13 11:32:06 +000025#include <ftl/enum.h>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000026#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000027#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000028#include <log/log_main.h>
29#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000030#include "TouchpadInputMapper.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000031#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000032
33namespace android {
34
Harry Cutts1f48a442022-11-15 17:38:36 +000035namespace {
36
Harry Cuttsc5025372023-02-21 16:04:45 +000037/**
38 * Log details of each gesture output by the gestures library.
39 * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires
40 * restarting the shell)
41 */
42const bool DEBUG_TOUCHPAD_GESTURES =
43 __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures",
44 ANDROID_LOG_INFO);
45
Harry Cuttsd35a24b2023-01-30 15:09:30 +000046// Describes a segment of the acceleration curve.
47struct CurveSegment {
48 // The maximum pointer speed which this segment should apply. The last segment in a curve should
49 // always set this to infinity.
50 double maxPointerSpeedMmPerS;
51 double slope;
52 double intercept;
53};
54
55const std::vector<CurveSegment> segments = {
56 {10.922, 3.19, 0},
57 {31.750, 4.79, -17.526},
58 {98.044, 7.28, -96.52},
59 {std::numeric_limits<double>::infinity(), 15.04, -857.758},
60};
61
Wenxin Fenge0320e72023-04-08 19:26:02 -070062const std::vector<double> sensitivityFactors = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18};
Harry Cuttsd35a24b2023-01-30 15:09:30 +000063
64std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
65 size_t propertySize) {
66 LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size());
67 std::vector<double> output(propertySize, 0);
68
69 // The Gestures library uses functions of the following form to define curve segments, where a,
70 // b, and c can be specified by us:
71 // output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c
72 //
73 // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.)
74 //
75 // We are trying to implement the following function, where slope and intercept are the
76 // parameters specified in the `segments` array above:
77 // gain(input_speed_mm) =
78 // 0.64 * (sensitivityFactor / 10) * (slope + intercept / input_speed_mm)
79 // Where "gain" is a multiplier applied to the input speed to produce the output speed:
80 // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm)
81 //
82 // To put our function in the library's form, we substitute it into the function above:
83 // output_speed(input_speed_mm) =
84 // input_speed_mm * (0.64 * (sensitivityFactor / 10) *
85 // (slope + 25.4 * intercept / input_speed_mm))
86 // then expand the brackets so that input_speed_mm cancels out for the intercept term:
87 // gain(input_speed_mm) =
88 // 0.64 * (sensitivityFactor / 10) * slope * input_speed_mm +
89 // 0.64 * (sensitivityFactor / 10) * intercept
90 //
91 // This gives us the following parameters for the Gestures library function form:
92 // a = 0
93 // b = 0.64 * (sensitivityFactor / 10) * slope
94 // c = 0.64 * (sensitivityFactor / 10) * intercept
95
96 double commonFactor = 0.64 * sensitivityFactors[sensitivity + 7] / 10;
97
98 size_t i = 0;
99 for (CurveSegment seg : segments) {
100 // The library's curve format consists of four doubles per segment:
101 // * maximum pointer speed for the segment (mm/s)
102 // * multiplier for the x² term (a.k.a. "a" or "sqr")
103 // * multiplier for the x term (a.k.a. "b" or "mul")
104 // * the intercept (a.k.a. "c" or "int")
105 // (see struct CurveSegment in the library's AccelFilterInterpreter)
106 output[i + 0] = seg.maxPointerSpeedMmPerS;
107 output[i + 1] = 0;
108 output[i + 2] = commonFactor * seg.slope;
109 output[i + 3] = commonFactor * seg.intercept;
110 i += 4;
111 }
112
113 return output;
114}
115
Harry Cutts1f48a442022-11-15 17:38:36 +0000116short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +0000117 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
118 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
119 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
120 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
121 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +0000122 return 0;
123}
124
125HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
126 HardwareProperties props;
127 RawAbsoluteAxisInfo absMtPositionX;
128 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
129 props.left = absMtPositionX.minValue;
130 props.right = absMtPositionX.maxValue;
131 props.res_x = absMtPositionX.resolution;
132
133 RawAbsoluteAxisInfo absMtPositionY;
134 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
135 props.top = absMtPositionY.minValue;
136 props.bottom = absMtPositionY.maxValue;
137 props.res_y = absMtPositionY.resolution;
138
139 RawAbsoluteAxisInfo absMtOrientation;
140 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
141 props.orientation_minimum = absMtOrientation.minValue;
142 props.orientation_maximum = absMtOrientation.maxValue;
143
144 RawAbsoluteAxisInfo absMtSlot;
145 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
146 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
147 props.max_touch_cnt = getMaxTouchCount(context);
148
149 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
150 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
151 // that did this, so assume false.
152 props.supports_t5r2 = false;
153
154 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
155 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
156
157 // Mouse-only properties, which will always be false.
158 props.has_wheel = false;
159 props.wheel_is_hi_res = false;
160
161 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
162 // are haptic.
163 props.is_haptic_pad = false;
164 return props;
165}
166
Harry Cutts74235542022-11-24 15:52:53 +0000167void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
168 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
169 mapper->consumeGesture(gesture);
170}
171
Harry Cutts1f48a442022-11-15 17:38:36 +0000172} // namespace
173
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext,
175 const InputReaderConfiguration& readerConfig)
176 : InputMapper(deviceContext, readerConfig),
Harry Cutts1f48a442022-11-15 17:38:36 +0000177 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +0000178 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cuttsbb24e272023-03-21 10:49:47 +0000179 mStateConverter(deviceContext, mMotionAccumulator),
180 mGestureConverter(*getContext(), deviceContext, getDeviceId()),
181 mCapturedEventConverter(*getContext(), deviceContext, mMotionAccumulator, getDeviceId()) {
182 RawAbsoluteAxisInfo slotAxisInfo;
183 deviceContext.getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo);
184 if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) {
185 ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work "
186 "properly.",
187 deviceContext.getName().c_str());
188 }
189 mMotionAccumulator.configure(deviceContext, slotAxisInfo.maxValue + 1, true);
190
Harry Cutts1f48a442022-11-15 17:38:36 +0000191 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
192 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +0000193 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +0000194 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000195 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
196 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000197 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
198 &mPropertyProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000199 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000200 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000201}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000202
Harry Cutts74235542022-11-24 15:52:53 +0000203TouchpadInputMapper::~TouchpadInputMapper() {
204 if (mPointerController != nullptr) {
205 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
206 }
Harry Cutts1b217912023-01-03 17:13:19 +0000207
208 // The gesture interpreter's destructor will call its property provider's free function for all
209 // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer
210 // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may
211 // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on
212 // declaration order to avoid crashes seems rather fragile, so explicitly clear the property
213 // provider here to ensure all the freeProperty calls happen before mPropertyProvider is
214 // destructed.
215 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000216}
217
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000218uint32_t TouchpadInputMapper::getSources() const {
219 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
220}
221
Harry Cuttsd02ea102023-03-17 18:21:30 +0000222void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000223 InputMapper::populateDeviceInfo(info);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000224 if (mPointerCaptured) {
225 mCapturedEventConverter.populateMotionRanges(info);
226 } else {
227 mGestureConverter.populateMotionRanges(info);
228 }
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000229}
230
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000231void TouchpadInputMapper::dump(std::string& dump) {
232 dump += INDENT2 "Touchpad Input Mapper:\n";
Harry Cuttsbb24e272023-03-21 10:49:47 +0000233 if (mProcessing) {
234 dump += INDENT3 "Currently processing a hardware state\n";
235 }
236 if (mResettingInterpreter) {
237 dump += INDENT3 "Currently resetting gesture interpreter\n";
238 }
239 dump += StringPrintf(INDENT3 "Pointer captured: %s\n", toString(mPointerCaptured));
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000240 dump += INDENT3 "Gesture converter:\n";
241 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
242 dump += INDENT3 "Gesture properties:\n";
243 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
Harry Cuttsbb24e272023-03-21 10:49:47 +0000244 dump += INDENT3 "Captured event converter:\n";
245 dump += addLinePrefix(mCapturedEventConverter.dump(), INDENT4);
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000246}
247
Arpit Singh4be4eef2023-03-28 14:26:01 +0000248std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000249 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000250 ConfigurationChanges changes) {
251 if (!changes.any()) {
Harry Cutts2b67ff12023-03-13 11:32:06 +0000252 // First time configuration
253 mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration());
254 }
255
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000256 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000257 std::optional<int32_t> displayId = mPointerController->getDisplayId();
258 ui::Rotation orientation = ui::ROTATION_0;
259 if (displayId.has_value()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000260 if (auto viewport = config.getDisplayViewportById(*displayId); viewport) {
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000261 orientation = getInverseRotation(viewport->orientation);
262 }
263 }
264 mGestureConverter.setOrientation(orientation);
265 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000266 if (!changes.any() || changes.test(InputReaderConfiguration::Change::TOUCHPAD_SETTINGS)) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000267 mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve")
268 .setBoolValues({true});
269 GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
270 accelCurveProp.setRealValues(
Arpit Singhed6c3de2023-04-05 19:24:37 +0000271 createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000272 accelCurveProp.getCount()));
Harry Cuttsa546ba82023-01-13 17:21:00 +0000273 mPropertyProvider.getProperty("Invert Scrolling")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000274 .setBoolValues({config.touchpadNaturalScrollingEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000275 mPropertyProvider.getProperty("Tap Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000276 .setBoolValues({config.touchpadTapToClickEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000277 mPropertyProvider.getProperty("Button Right Click Zone Enable")
Arpit Singhed6c3de2023-04-05 19:24:37 +0000278 .setBoolValues({config.touchpadRightClickZoneEnabled});
Harry Cuttsa546ba82023-01-13 17:21:00 +0000279 }
Harry Cuttsbb24e272023-03-21 10:49:47 +0000280 std::list<NotifyArgs> out;
281 if ((!changes.any() && config.pointerCaptureRequest.enable) ||
282 changes.test(InputReaderConfiguration::Change::POINTER_CAPTURE)) {
283 mPointerCaptured = config.pointerCaptureRequest.enable;
284 // The motion ranges are going to change, so bump the generation to clear the cached ones.
285 bumpGeneration();
286 if (mPointerCaptured) {
287 // The touchpad is being captured, so we need to tidy up any fake fingers etc. that are
288 // still being reported for a gesture in progress.
289 out += reset(when);
290 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
291 } else {
292 // We're transitioning from captured to uncaptured.
293 mCapturedEventConverter.reset();
294 }
295 if (changes.any()) {
296 out.push_back(NotifyDeviceResetArgs(getContext()->getNextId(), when, getDeviceId()));
297 }
298 }
299 return out;
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000300}
301
Harry Cutts1f48a442022-11-15 17:38:36 +0000302std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000303 mStateConverter.reset();
Harry Cuttsbb24e272023-03-21 10:49:47 +0000304 resetGestureInterpreter(when);
Harry Cuttse9b71422023-03-14 16:54:44 +0000305 std::list<NotifyArgs> out = mGestureConverter.reset(when);
306 out += InputMapper::reset(when);
307 return out;
Harry Cutts1f48a442022-11-15 17:38:36 +0000308}
309
Harry Cuttsbb24e272023-03-21 10:49:47 +0000310void TouchpadInputMapper::resetGestureInterpreter(nsecs_t when) {
311 // The GestureInterpreter has no official reset method, but sending a HardwareState with no
312 // fingers down or buttons pressed should get it into a clean state.
313 HardwareState state;
314 state.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count();
315 mResettingInterpreter = true;
316 mGestureInterpreter->PushHardwareState(&state);
317 mResettingInterpreter = false;
318}
319
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000320std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cuttsbb24e272023-03-21 10:49:47 +0000321 if (mPointerCaptured) {
322 return mCapturedEventConverter.process(*rawEvent);
323 }
Harry Cutts47db1c72022-12-13 19:20:47 +0000324 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
325 if (state) {
326 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
327 } else {
328 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000329 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000330}
331
Harry Cutts47db1c72022-12-13 19:20:47 +0000332std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
333 SelfContainedHardwareState schs) {
Harry Cutts287e19f2023-02-27 17:09:24 +0000334 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str());
Harry Cutts74235542022-11-24 15:52:53 +0000335 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000336 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000337 mProcessing = false;
338
Harry Cutts47db1c72022-12-13 19:20:47 +0000339 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000340}
341
342void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
Harry Cuttsc5025372023-02-21 16:04:45 +0000343 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str());
Harry Cuttsbb24e272023-03-21 10:49:47 +0000344 if (mResettingInterpreter) {
345 // We already handle tidying up fake fingers etc. in GestureConverter::reset, so we should
346 // ignore any gestures produced from the interpreter while we're resetting it.
347 return;
348 }
Harry Cutts74235542022-11-24 15:52:53 +0000349 if (!mProcessing) {
350 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
351 return;
352 }
353 mGesturesToProcess.push_back(*gesture);
354}
355
356std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
357 std::list<NotifyArgs> out = {};
358 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000359 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000360 }
361 mGesturesToProcess.clear();
362 return out;
363}
364
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000365} // namespace android