Skip to content

Commit cfdf4be

Browse files
Michael ChecoAndroid (Google) Code Review
authored andcommitted
Merge "Add support to disable touchpad acceleration" into main
2 parents c3aee78 + cac8d99 commit cfdf4be

4 files changed

Lines changed: 88 additions & 2 deletions

File tree

services/inputflinger/include/InputReaderBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ struct InputReaderConfiguration {
150150
// speed setting still affects the scaling factor.
151151
bool mousePointerAccelerationEnabled;
152152

153+
// True if the touchpad should exhibit pointer acceleration. If false,
154+
// a flat acceleration curve (linear scaling) is used, but the user's pointer
155+
// speed setting still affects the scaling factor.
156+
bool touchpadAccelerationEnabled;
157+
153158
// Velocity control parameters for touchpad pointer movements on the old touchpad stack (based
154159
// on TouchInputMapper).
155160
//
@@ -284,6 +289,7 @@ struct InputReaderConfiguration {
284289
mousePointerSpeed(0),
285290
displaysWithMouseScalingDisabled(),
286291
mousePointerAccelerationEnabled(true),
292+
touchpadAccelerationEnabled(true),
287293
pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f,
288294
static_cast<float>(
289295
android::os::IInputConstants::

services/inputflinger/reader/mapper/TouchpadInputMapper.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ const bool DEBUG_TOUCHPAD_GESTURES =
5959
ANDROID_LOG_INFO);
6060

6161
std::vector<double> createAccelerationCurveForSensitivity(int32_t sensitivity,
62+
bool accelerationEnabled,
6263
size_t propertySize) {
63-
std::vector<AccelerationCurveSegment> segments =
64-
createAccelerationCurveForPointerSensitivity(sensitivity);
64+
std::vector<AccelerationCurveSegment> segments = accelerationEnabled
65+
? createAccelerationCurveForPointerSensitivity(sensitivity)
66+
: createFlatAccelerationCurve(sensitivity);
6567
LOG_ALWAYS_FATAL_IF(propertySize < 4 * segments.size());
6668
std::vector<double> output(propertySize, 0);
6769

@@ -358,12 +360,14 @@ std::list<NotifyArgs> TouchpadInputMapper::reconfigure(nsecs_t when,
358360
GesturesProp accelCurveProp = mPropertyProvider.getProperty("Pointer Accel Curve");
359361
accelCurveProp.setRealValues(
360362
createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
363+
config.touchpadAccelerationEnabled,
361364
accelCurveProp.getCount()));
362365
mPropertyProvider.getProperty("Use Custom Touchpad Scroll Accel Curve")
363366
.setBoolValues({true});
364367
GesturesProp scrollCurveProp = mPropertyProvider.getProperty("Scroll Accel Curve");
365368
scrollCurveProp.setRealValues(
366369
createAccelerationCurveForSensitivity(config.touchpadPointerSpeed,
370+
config.touchpadAccelerationEnabled,
367371
scrollCurveProp.getCount()));
368372
mPropertyProvider.getProperty("Scroll X Out Scale").setRealValues({1.0});
369373
mPropertyProvider.getProperty("Scroll Y Out Scale").setRealValues({1.0});
@@ -510,4 +514,12 @@ std::optional<HardwareProperties> TouchpadInputMapper::getTouchpadHardwareProper
510514
return mHardwareProperties;
511515
}
512516

517+
std::optional<GesturesProp> TouchpadInputMapper::getGesturePropertyForTesting(
518+
const std::string& name) {
519+
if (!mPropertyProvider.hasProperty(name)) {
520+
return std::nullopt;
521+
}
522+
return mPropertyProvider.getProperty(name);
523+
}
524+
513525
} // namespace android

services/inputflinger/reader/mapper/TouchpadInputMapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class TouchpadInputMapper : public InputMapper {
7070

7171
std::optional<HardwareProperties> getTouchpadHardwareProperties() override;
7272

73+
std::optional<GesturesProp> getGesturePropertyForTesting(const std::string& name);
74+
7375
private:
7476
void resetGestureInterpreter(nsecs_t when);
7577
explicit TouchpadInputMapper(InputDeviceContext& deviceContext,

services/inputflinger/tests/TouchpadInputMapper_test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
#include <android-base/logging.h>
2020
#include <gtest/gtest.h>
21+
#include <input/AccelerationCurve.h>
2122

23+
#include <log/log.h>
2224
#include <thread>
2325
#include "InputMapperTest.h"
2426
#include "InterfaceMocks.h"
27+
#include "TestConstants.h"
2528
#include "TestEventMatchers.h"
2629

2730
#define TAG "TouchpadInputMapper_test"
@@ -190,4 +193,67 @@ TEST_F(TouchpadInputMapperTest, TouchpadHardwareState) {
190193
mFakePolicy->assertTouchpadHardwareStateNotified();
191194
}
192195

196+
TEST_F(TouchpadInputMapperTest, TouchpadAccelerationDisabled) {
197+
mReaderConfiguration.touchpadAccelerationEnabled = false;
198+
mReaderConfiguration.touchpadPointerSpeed = 3;
199+
200+
std::list<NotifyArgs> args =
201+
mMapper->reconfigure(ARBITRARY_TIME, mReaderConfiguration,
202+
InputReaderConfiguration::Change::TOUCHPAD_SETTINGS);
203+
auto* touchpadMapper = static_cast<TouchpadInputMapper*>(mMapper.get());
204+
205+
const auto accelCurvePropsDisabled =
206+
touchpadMapper->getGesturePropertyForTesting("Pointer Accel Curve");
207+
ASSERT_TRUE(accelCurvePropsDisabled.has_value());
208+
std::vector<double> curveValuesDisabled = accelCurvePropsDisabled.value().getRealValues();
209+
std::vector<AccelerationCurveSegment> curve =
210+
createFlatAccelerationCurve(mReaderConfiguration.touchpadPointerSpeed);
211+
double expectedBaseGain = curve[0].baseGain;
212+
ASSERT_EQ(curveValuesDisabled[0], std::numeric_limits<double>::infinity());
213+
ASSERT_EQ(curveValuesDisabled[1], 0);
214+
ASSERT_NEAR(curveValuesDisabled[2], expectedBaseGain, EPSILON);
215+
ASSERT_EQ(curveValuesDisabled[3], 0);
216+
}
217+
218+
TEST_F(TouchpadInputMapperTest, TouchpadAccelerationEnabled) {
219+
// Enable touchpad acceleration.
220+
mReaderConfiguration.touchpadAccelerationEnabled = true;
221+
mReaderConfiguration.touchpadPointerSpeed = 3;
222+
223+
std::list<NotifyArgs> args =
224+
mMapper->reconfigure(ARBITRARY_TIME, mReaderConfiguration,
225+
InputReaderConfiguration::Change::TOUCHPAD_SETTINGS);
226+
ASSERT_THAT(args, testing::IsEmpty());
227+
228+
auto* touchpadMapper = static_cast<TouchpadInputMapper*>(mMapper.get());
229+
230+
// Get the acceleration curve properties when acceleration is enabled.
231+
const auto accelCurvePropsEnabled =
232+
touchpadMapper->getGesturePropertyForTesting("Pointer Accel Curve");
233+
ASSERT_TRUE(accelCurvePropsEnabled.has_value());
234+
235+
// Get the curve values.
236+
std::vector<double> curveValuesEnabled = accelCurvePropsEnabled.value().getRealValues();
237+
238+
// Use createAccelerationCurveForPointerSensitivity to get expected curve segments.
239+
std::vector<AccelerationCurveSegment> expectedCurveSegments =
240+
createAccelerationCurveForPointerSensitivity(mReaderConfiguration.touchpadPointerSpeed);
241+
242+
// Iterate through the segments and compare the values.
243+
for (size_t i = 0; i < expectedCurveSegments.size(); ++i) {
244+
// Check max speed.
245+
if (std::isinf(expectedCurveSegments[i].maxPointerSpeedMmPerS)) {
246+
ASSERT_TRUE(std::isinf(curveValuesEnabled[i * 4 + 0]));
247+
} else {
248+
ASSERT_NEAR(curveValuesEnabled[i * 4 + 0],
249+
expectedCurveSegments[i].maxPointerSpeedMmPerS, EPSILON);
250+
}
251+
252+
// Check that the x^2 term is zero.
253+
ASSERT_NEAR(curveValuesEnabled[i * 4 + 1], 0, EPSILON);
254+
ASSERT_NEAR(curveValuesEnabled[i * 4 + 2], expectedCurveSegments[i].baseGain, EPSILON);
255+
ASSERT_NEAR(curveValuesEnabled[i * 4 + 3], expectedCurveSegments[i].reciprocal, EPSILON);
256+
}
257+
}
258+
193259
} // namespace android

0 commit comments

Comments
 (0)