blob: 8ab6748bfe30218ce03a69018ecaff441e0b8bff [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 Cuttsd35a24b2023-01-30 15:09:30 +000019#include <limits>
Harry Cuttsedf6ce72023-01-04 12:15:53 +000020#include <optional>
21
Harry Cutts74235542022-11-24 15:52:53 +000022#include <android/input.h>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000023#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000024#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000025#include <log/log_main.h>
26#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000027#include "TouchpadInputMapper.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000028#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000029
30namespace android {
31
Harry Cutts1f48a442022-11-15 17:38:36 +000032namespace {
33
Harry Cuttsd35a24b2023-01-30 15:09:30 +000034// Describes a segment of the acceleration curve.
35struct CurveSegment {
36 // The maximum pointer speed which this segment should apply. The last segment in a curve should
37 // always set this to infinity.
38 double maxPointerSpeedMmPerS;
39 double slope;
40 double intercept;
41};
42
43const std::vector<CurveSegment> segments = {
44 {10.922, 3.19, 0},
45 {31.750, 4.79, -17.526},
46 {98.044, 7.28, -96.52},
47 {std::numeric_limits<double>::infinity(), 15.04, -857.758},
48};
49
50const std::vector<double> sensitivityFactors = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20};
51
52std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
53 size_t propertySize) {
54 LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size());
55 std::vector<double> output(propertySize, 0);
56
57 // The Gestures library uses functions of the following form to define curve segments, where a,
58 // b, and c can be specified by us:
59 // output_speed(input_speed_mm) = a * input_speed_mm ^ 2 + b * input_speed_mm + c
60 //
61 // (a, b, and c are also called sqr_, mul_, and int_ in the Gestures library code.)
62 //
63 // We are trying to implement the following function, where slope and intercept are the
64 // parameters specified in the `segments` array above:
65 // gain(input_speed_mm) =
66 // 0.64 * (sensitivityFactor / 10) * (slope + intercept / input_speed_mm)
67 // Where "gain" is a multiplier applied to the input speed to produce the output speed:
68 // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm)
69 //
70 // To put our function in the library's form, we substitute it into the function above:
71 // output_speed(input_speed_mm) =
72 // input_speed_mm * (0.64 * (sensitivityFactor / 10) *
73 // (slope + 25.4 * intercept / input_speed_mm))
74 // then expand the brackets so that input_speed_mm cancels out for the intercept term:
75 // gain(input_speed_mm) =
76 // 0.64 * (sensitivityFactor / 10) * slope * input_speed_mm +
77 // 0.64 * (sensitivityFactor / 10) * intercept
78 //
79 // This gives us the following parameters for the Gestures library function form:
80 // a = 0
81 // b = 0.64 * (sensitivityFactor / 10) * slope
82 // c = 0.64 * (sensitivityFactor / 10) * intercept
83
84 double commonFactor = 0.64 * sensitivityFactors[sensitivity + 7] / 10;
85
86 size_t i = 0;
87 for (CurveSegment seg : segments) {
88 // The library's curve format consists of four doubles per segment:
89 // * maximum pointer speed for the segment (mm/s)
90 // * multiplier for the x² term (a.k.a. "a" or "sqr")
91 // * multiplier for the x term (a.k.a. "b" or "mul")
92 // * the intercept (a.k.a. "c" or "int")
93 // (see struct CurveSegment in the library's AccelFilterInterpreter)
94 output[i + 0] = seg.maxPointerSpeedMmPerS;
95 output[i + 1] = 0;
96 output[i + 2] = commonFactor * seg.slope;
97 output[i + 3] = commonFactor * seg.intercept;
98 i += 4;
99 }
100
101 return output;
102}
103
Harry Cutts1f48a442022-11-15 17:38:36 +0000104short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +0000105 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
106 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
107 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
108 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
109 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +0000110 return 0;
111}
112
113HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
114 HardwareProperties props;
115 RawAbsoluteAxisInfo absMtPositionX;
116 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
117 props.left = absMtPositionX.minValue;
118 props.right = absMtPositionX.maxValue;
119 props.res_x = absMtPositionX.resolution;
120
121 RawAbsoluteAxisInfo absMtPositionY;
122 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
123 props.top = absMtPositionY.minValue;
124 props.bottom = absMtPositionY.maxValue;
125 props.res_y = absMtPositionY.resolution;
126
127 RawAbsoluteAxisInfo absMtOrientation;
128 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
129 props.orientation_minimum = absMtOrientation.minValue;
130 props.orientation_maximum = absMtOrientation.maxValue;
131
132 RawAbsoluteAxisInfo absMtSlot;
133 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
134 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
135 props.max_touch_cnt = getMaxTouchCount(context);
136
137 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
138 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
139 // that did this, so assume false.
140 props.supports_t5r2 = false;
141
142 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
143 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
144
145 // Mouse-only properties, which will always be false.
146 props.has_wheel = false;
147 props.wheel_is_hi_res = false;
148
149 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
150 // are haptic.
151 props.is_haptic_pad = false;
152 return props;
153}
154
Harry Cutts74235542022-11-24 15:52:53 +0000155void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
156 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
157 mapper->consumeGesture(gesture);
158}
159
Harry Cutts1f48a442022-11-15 17:38:36 +0000160} // namespace
161
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000162TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +0000163 : InputMapper(deviceContext),
164 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +0000165 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts4fb941a2022-12-14 19:14:04 +0000166 mStateConverter(deviceContext),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000167 mGestureConverter(*getContext(), deviceContext, getDeviceId()) {
Harry Cutts1f48a442022-11-15 17:38:36 +0000168 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
169 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +0000170 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +0000171 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000172 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
173 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000174 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
175 &mPropertyProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000176 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000177 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000178}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000179
Harry Cutts74235542022-11-24 15:52:53 +0000180TouchpadInputMapper::~TouchpadInputMapper() {
181 if (mPointerController != nullptr) {
182 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
183 }
Harry Cutts1b217912023-01-03 17:13:19 +0000184
185 // The gesture interpreter's destructor will call its property provider's free function for all
186 // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer
187 // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may
188 // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on
189 // declaration order to avoid crashes seems rather fragile, so explicitly clear the property
190 // provider here to ensure all the freeProperty calls happen before mPropertyProvider is
191 // destructed.
192 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000193}
194
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000195uint32_t TouchpadInputMapper::getSources() const {
196 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
197}
198
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000199void TouchpadInputMapper::dump(std::string& dump) {
200 dump += INDENT2 "Touchpad Input Mapper:\n";
201 dump += INDENT3 "Gesture converter:\n";
202 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
203 dump += INDENT3 "Gesture properties:\n";
204 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
205}
206
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000207std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
208 const InputReaderConfiguration* config,
209 uint32_t changes) {
210 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
211 std::optional<int32_t> displayId = mPointerController->getDisplayId();
212 ui::Rotation orientation = ui::ROTATION_0;
213 if (displayId.has_value()) {
214 if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
215 orientation = getInverseRotation(viewport->orientation);
216 }
217 }
218 mGestureConverter.setOrientation(orientation);
219 }
Harry Cuttsa546ba82023-01-13 17:21:00 +0000220 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCHPAD_SETTINGS)) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000221 mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve")
222 .setBoolValues({true});
223 GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
224 accelCurveProp.setRealValues(
225 createAccelerationCurveForSensitivity(config->touchpadPointerSpeed,
226 accelCurveProp.getCount()));
Harry Cuttsa546ba82023-01-13 17:21:00 +0000227 mPropertyProvider.getProperty("Invert Scrolling")
228 .setBoolValues({config->touchpadNaturalScrollingEnabled});
229 mPropertyProvider.getProperty("Tap Enable")
230 .setBoolValues({config->touchpadTapToClickEnabled});
231 mPropertyProvider.getProperty("Button Right Click Zone Enable")
232 .setBoolValues({config->touchpadRightClickZoneEnabled});
233 }
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000234 return {};
235}
236
Harry Cutts1f48a442022-11-15 17:38:36 +0000237std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000238 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000239 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000240 return InputMapper::reset(when);
241}
242
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000243std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000244 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
245 if (state) {
246 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
247 } else {
248 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000249 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000250}
251
Harry Cutts47db1c72022-12-13 19:20:47 +0000252std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
253 SelfContainedHardwareState schs) {
Harry Cutts74235542022-11-24 15:52:53 +0000254 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000255 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000256 mProcessing = false;
257
Harry Cutts47db1c72022-12-13 19:20:47 +0000258 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000259}
260
261void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
262 ALOGD("Gesture ready: %s", gesture->String().c_str());
263 if (!mProcessing) {
264 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
265 return;
266 }
267 mGesturesToProcess.push_back(*gesture);
268}
269
270std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
271 std::list<NotifyArgs> out = {};
272 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000273 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000274 }
275 mGesturesToProcess.clear();
276 return out;
277}
278
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000279} // namespace android