Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 19 | #include <limits> |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 20 | #include <optional> |
| 21 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 22 | #include <android/input.h> |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 23 | #include <ftl/enum.h> |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 24 | #include <input/PrintTools.h> |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 25 | #include <linux/input-event-codes.h> |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 26 | #include <log/log_main.h> |
| 27 | #include "TouchCursorInputMapperCommon.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 28 | #include "TouchpadInputMapper.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 29 | #include "ui/Rotation.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 35 | /** |
| 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 | */ |
| 40 | const bool DEBUG_TOUCHPAD_GESTURES = |
| 41 | __android_log_is_loggable(ANDROID_LOG_DEBUG, "TouchpadInputMapperGestures", |
| 42 | ANDROID_LOG_INFO); |
| 43 | |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 44 | // Describes a segment of the acceleration curve. |
| 45 | struct 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 | |
| 53 | const 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 | |
| 60 | const std::vector<double> sensitivityFactors = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20}; |
| 61 | |
| 62 | std::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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 114 | short getMaxTouchCount(const InputDeviceContext& context) { |
Harry Cutts | b255215 | 2022-12-13 17:18:09 +0000 | [diff] [blame] | 115 | 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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | HardwareProperties 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 Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 165 | void gestureInterpreterCallback(void* clientData, const Gesture* gesture) { |
| 166 | TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData); |
| 167 | mapper->consumeGesture(gesture); |
| 168 | } |
| 169 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 170 | } // namespace |
| 171 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 172 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext) |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 173 | : InputMapper(deviceContext), |
| 174 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 175 | mPointerController(getContext()->getPointerController(getDeviceId())), |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 176 | mStateConverter(deviceContext), |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 177 | mGestureConverter(*getContext(), deviceContext, getDeviceId()) { |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 178 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); |
| 179 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 180 | // Even though we don't explicitly delete copy/move semantics, it's safe to |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 181 | // give away pointers to TouchpadInputMapper and its members here because |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 182 | // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and |
| 183 | // 2) TouchpadInputMapper is stored as a unique_ptr and not moved. |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 184 | mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider), |
| 185 | &mPropertyProvider); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 186 | mGestureInterpreter->SetCallback(gestureInterpreterCallback, this); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 187 | // TODO(b/251196347): set a timer provider, so the library can use timers. |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 188 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 189 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 190 | TouchpadInputMapper::~TouchpadInputMapper() { |
| 191 | if (mPointerController != nullptr) { |
| 192 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 193 | } |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 194 | |
| 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 Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 205 | uint32_t TouchpadInputMapper::getSources() const { |
| 206 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; |
| 207 | } |
| 208 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 209 | void TouchpadInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 210 | InputMapper::populateDeviceInfo(info); |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 211 | mGestureConverter.populateMotionRanges(info); |
Harry Cutts | 8cd2abd | 2023-03-15 16:35:56 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame] | 214 | void 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 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame^] | 222 | std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when, |
| 223 | const InputReaderConfiguration* config, |
| 224 | uint32_t changes) { |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 225 | if (!changes) { |
| 226 | // First time configuration |
| 227 | mPropertyProvider.loadPropertiesFromIdcFile(getDeviceContext().getConfiguration()); |
| 228 | } |
| 229 | |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 230 | 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 Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 240 | if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCHPAD_SETTINGS)) { |
Harry Cutts | d35a24b | 2023-01-30 15:09:30 +0000 | [diff] [blame] | 241 | 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 Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame] | 247 | 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 Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 254 | return {}; |
| 255 | } |
| 256 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 257 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 258 | mStateConverter.reset(); |
Harry Cutts | e9b7142 | 2023-03-14 16:54:44 +0000 | [diff] [blame] | 259 | std::list<NotifyArgs> out = mGestureConverter.reset(when); |
| 260 | out += InputMapper::reset(when); |
| 261 | return out; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 264 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 265 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); |
| 266 | if (state) { |
| 267 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); |
| 268 | } else { |
| 269 | return {}; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 270 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 273 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, |
| 274 | SelfContainedHardwareState schs) { |
Harry Cutts | 287e19f | 2023-02-27 17:09:24 +0000 | [diff] [blame] | 275 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "New hardware state: %s", schs.state.String().c_str()); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 276 | mProcessing = true; |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 277 | mGestureInterpreter->PushHardwareState(&schs.state); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 278 | mProcessing = false; |
| 279 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 280 | return processGestures(when, readTime); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void TouchpadInputMapper::consumeGesture(const Gesture* gesture) { |
Harry Cutts | c502537 | 2023-02-21 16:04:45 +0000 | [diff] [blame] | 284 | ALOGD_IF(DEBUG_TOUCHPAD_GESTURES, "Gesture ready: %s", gesture->String().c_str()); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 285 | if (!mProcessing) { |
| 286 | ALOGE("Received gesture outside of the normal processing flow; ignoring it."); |
| 287 | return; |
| 288 | } |
| 289 | mGesturesToProcess.push_back(*gesture); |
| 290 | } |
| 291 | |
| 292 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { |
| 293 | std::list<NotifyArgs> out = {}; |
| 294 | for (Gesture& gesture : mGesturesToProcess) { |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 295 | out += mGestureConverter.handleGesture(when, readTime, gesture); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 296 | } |
| 297 | mGesturesToProcess.clear(); |
| 298 | return out; |
| 299 | } |
| 300 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 301 | } // namespace android |