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