blob: 4c56b055e4e3ee30f8bf0cff3f5af89b0f70128b [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 Cutts2b67ff12023-03-13 11:32:06 +000023#include <ftl/enum.h>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000024#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000025#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000026#include <log/log_main.h>
27#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000028#include "TouchpadInputMapper.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000029#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000030
31namespace android {
32
Harry Cutts1f48a442022-11-15 17:38:36 +000033namespace {
34
Harry Cuttsc5025372023-02-21 16:04:45 +000035/**
36 * Log details of each gesture output by the gestures library.
37 * Enable this via "adb shell setprop log.tag.TouchpadInputMapperGestures DEBUG" (requires
38 * restarting the shell)
39 */
40const bool DEBUG_TOUCHPAD_GESTURES =
41 __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures",
42 ANDROID_LOG_INFO);
43
Harry Cuttsd35a24b2023-01-30 15:09:30 +000044// Describes a segment of the acceleration curve.
45struct CurveSegment {
46 // The maximum pointer speed which this segment should apply. The last segment in a curve should
47 // always set this to infinity.
48 double maxPointerSpeedMmPerS;
49 double slope;
50 double intercept;
51};
52
53const std::vector<CurveSegment> segments = {
54 {10.922, 3.19, 0},
55 {31.750, 4.79, -17.526},
56 {98.044, 7.28, -96.52},
57 {std::numeric_limits<double>::infinity(), 15.04, -857.758},
58};
59
60const std::vector<double> sensitivityFactors = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20};
61
62std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
63 size_t propertySize) {
64 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 //
73 // We are trying to implement the following function, where slope and intercept are the
74 // parameters specified in the `segments` array above:
75 // gain(input_speed_mm) =
76 // 0.64 * (sensitivityFactor / 10) * (slope + intercept / input_speed_mm)
77 // Where "gain" is a multiplier applied to the input speed to produce the output speed:
78 // output_speed(input_speed_mm) = input_speed_mm * gain(input_speed_mm)
79 //
80 // To put our function in the library's form, we substitute it into the function above:
81 // output_speed(input_speed_mm) =
82 // input_speed_mm * (0.64 * (sensitivityFactor / 10) *
83 // (slope + 25.4 * intercept / input_speed_mm))
84 // then expand the brackets so that input_speed_mm cancels out for the intercept term:
85 // gain(input_speed_mm) =
86 // 0.64 * (sensitivityFactor / 10) * slope * input_speed_mm +
87 // 0.64 * (sensitivityFactor / 10) * intercept
88 //
89 // This gives us the following parameters for the Gestures library function form:
90 // a = 0
91 // b = 0.64 * (sensitivityFactor / 10) * slope
92 // c = 0.64 * (sensitivityFactor / 10) * intercept
93
94 double commonFactor = 0.64 * sensitivityFactors[sensitivity + 7] / 10;
95
96 size_t i = 0;
97 for (CurveSegment seg : segments) {
98 // The library's curve format consists of four doubles per segment:
99 // * maximum pointer speed for the segment (mm/s)
100 // * multiplier for the x² term (a.k.a. "a" or "sqr")
101 // * multiplier for the x term (a.k.a. "b" or "mul")
102 // * the intercept (a.k.a. "c" or "int")
103 // (see struct CurveSegment in the library's AccelFilterInterpreter)
104 output[i + 0] = seg.maxPointerSpeedMmPerS;
105 output[i + 1] = 0;
106 output[i + 2] = commonFactor * seg.slope;
107 output[i + 3] = commonFactor * seg.intercept;
108 i += 4;
109 }
110
111 return output;
112}
113
Harry Cutts1f48a442022-11-15 17:38:36 +0000114short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +0000115 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
116 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
117 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
118 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
119 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +0000120 return 0;
121}
122
123HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
124 HardwareProperties props;
125 RawAbsoluteAxisInfo absMtPositionX;
126 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
127 props.left = absMtPositionX.minValue;
128 props.right = absMtPositionX.maxValue;
129 props.res_x = absMtPositionX.resolution;
130
131 RawAbsoluteAxisInfo absMtPositionY;
132 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
133 props.top = absMtPositionY.minValue;
134 props.bottom = absMtPositionY.maxValue;
135 props.res_y = absMtPositionY.resolution;
136
137 RawAbsoluteAxisInfo absMtOrientation;
138 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
139 props.orientation_minimum = absMtOrientation.minValue;
140 props.orientation_maximum = absMtOrientation.maxValue;
141
142 RawAbsoluteAxisInfo absMtSlot;
143 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
144 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
145 props.max_touch_cnt = getMaxTouchCount(context);
146
147 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
148 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
149 // that did this, so assume false.
150 props.supports_t5r2 = false;
151
152 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
153 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
154
155 // Mouse-only properties, which will always be false.
156 props.has_wheel = false;
157 props.wheel_is_hi_res = false;
158
159 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
160 // are haptic.
161 props.is_haptic_pad = false;
162 return props;
163}
164
Harry Cutts74235542022-11-24 15:52:53 +0000165void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
166 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
167 mapper->consumeGesture(gesture);
168}
169
Harry Cutts1f48a442022-11-15 17:38:36 +0000170} // namespace
171
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000172TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +0000173 : InputMapper(deviceContext),
174 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +0000175 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts4fb941a2022-12-14 19:14:04 +0000176 mStateConverter(deviceContext),
Harry Cuttsc5748d12022-12-02 17:30:18 +0000177 mGestureConverter(*getContext(), deviceContext, getDeviceId()) {
Harry Cutts1f48a442022-11-15 17:38:36 +0000178 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
179 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +0000180 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +0000181 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000182 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
183 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000184 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
185 &mPropertyProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000186 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000187 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000188}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000189
Harry Cutts74235542022-11-24 15:52:53 +0000190TouchpadInputMapper::~TouchpadInputMapper() {
191 if (mPointerController != nullptr) {
192 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
193 }
Harry Cutts1b217912023-01-03 17:13:19 +0000194
195 // The gesture interpreter's destructor will call its property provider's free function for all
196 // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer
197 // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may
198 // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on
199 // declaration order to avoid crashes seems rather fragile, so explicitly clear the property
200 // provider here to ensure all the freeProperty calls happen before mPropertyProvider is
201 // destructed.
202 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000203}
204
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000205uint32_t TouchpadInputMapper::getSources() const {
206 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
207}
208
Harry Cutts8cd2abd2023-03-15 16:35:56 +0000209void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
210 InputMapper::populateDeviceInfo(info);
211 mGestureConverter.populateMotionRanges(*info);
212}
213
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000214void TouchpadInputMapper::dump(std::string& dump) {
215 dump += INDENT2 "Touchpad Input Mapper:\n";
216 dump += INDENT3 "Gesture converter:\n";
217 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
218 dump += INDENT3 "Gesture properties:\n";
219 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
220}
221
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000222std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
223 const InputReaderConfiguration* config,
224 uint32_t changes) {
Harry Cutts2b67ff12023-03-13 11:32:06 +0000225 if (!changes) {
226 // First time configuration
227 mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration());
228 }
229
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000230 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
231 std::optional<int32_t> displayId = mPointerController->getDisplayId();
232 ui::Rotation orientation = ui::ROTATION_0;
233 if (displayId.has_value()) {
234 if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
235 orientation = getInverseRotation(viewport->orientation);
236 }
237 }
238 mGestureConverter.setOrientation(orientation);
239 }
Harry Cuttsa546ba82023-01-13 17:21:00 +0000240 if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCHPAD_SETTINGS)) {
Harry Cuttsd35a24b2023-01-30 15:09:30 +0000241 mPropertyProvider.getProperty("Use Custom Touchpad Pointer Accel Curve")
242 .setBoolValues({true});
243 GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
244 accelCurveProp.setRealValues(
245 createAccelerationCurveForSensitivity(config->touchpadPointerSpeed,
246 accelCurveProp.getCount()));
Harry Cuttsa546ba82023-01-13 17:21:00 +0000247 mPropertyProvider.getProperty("Invert Scrolling")
248 .setBoolValues({config->touchpadNaturalScrollingEnabled});
249 mPropertyProvider.getProperty("Tap Enable")
250 .setBoolValues({config->touchpadTapToClickEnabled});
251 mPropertyProvider.getProperty("Button Right Click Zone Enable")
252 .setBoolValues({config->touchpadRightClickZoneEnabled});
253 }
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000254 return {};
255}
256
Harry Cutts1f48a442022-11-15 17:38:36 +0000257std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000258 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000259 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000260 return InputMapper::reset(when);
261}
262
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000263std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000264 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
265 if (state) {
266 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
267 } else {
268 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000269 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000270}
271
Harry Cutts47db1c72022-12-13 19:20:47 +0000272std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
273 SelfContainedHardwareState schs) {
Harry Cutts287e19f2023-02-27 17:09:24 +0000274 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str());
Harry Cutts74235542022-11-24 15:52:53 +0000275 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000276 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000277 mProcessing = false;
278
Harry Cutts47db1c72022-12-13 19:20:47 +0000279 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000280}
281
282void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
Harry Cuttsc5025372023-02-21 16:04:45 +0000283 ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str());
Harry Cutts74235542022-11-24 15:52:53 +0000284 if (!mProcessing) {
285 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
286 return;
287 }
288 mGesturesToProcess.push_back(*gesture);
289}
290
291std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
292 std::list<NotifyArgs> out = {};
293 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000294 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000295 }
296 mGesturesToProcess.clear();
297 return out;
298}
299
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000300} // namespace android