From ad0647688afe71b7e617c1f47491a63f9aab5dcb Mon Sep 17 00:00:00 2001 From: Roman Gilg Date: Tue, 22 Nov 2016 14:49:01 +0100 Subject: [PATCH] [libinput] Query defaultLeftHanded, save leftHanded, fix ScrollMode config Some small improvements / fixes to the libinput backend: - Query libinput_device_config_left_handed_get_default - Write leftHanded property to config file - When saving the touchpad scroll mode, write false to all other ones. Otherwise it will always enable the last read entry after reboot. - Use macro for setLeftHanded(bool) and setNaturalScroll(bool) Reviewers: #kwin, graesslin Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3430 --- autotests/libinput/device_test.cpp | 65 ++++++++++++++++++++++++++++ autotests/libinput/mock_libinput.cpp | 5 +++ autotests/libinput/mock_libinput.h | 1 + libinput/device.cpp | 56 ++++++++++++------------ libinput/device.h | 5 +++ 5 files changed, 105 insertions(+), 27 deletions(-) diff --git a/autotests/libinput/device_test.cpp b/autotests/libinput/device_test.cpp index 57d5a24a4e..da0599c8ef 100644 --- a/autotests/libinput/device_test.cpp +++ b/autotests/libinput/device_test.cpp @@ -45,6 +45,8 @@ private Q_SLOTS: void testTapFingerCount(); void testSize_data(); void testSize(); + void testLeftHandedEnabledByDefault_data(); + void testLeftHandedEnabledByDefault(); void testTapEnabledByDefault_data(); void testTapEnabledByDefault(); void testMiddleEmulationEnabledByDefault_data(); @@ -133,6 +135,8 @@ private Q_SLOTS: void testLoadScrollOnButton(); void testLoadScrollButton_data(); void testLoadScrollButton(); + void testLoadLeftHanded_data(); + void testLoadLeftHanded(); }; void TestLibinputDevice::testStaticGetter() @@ -327,6 +331,25 @@ void TestLibinputDevice::testSize() QTEST(d.property("size").toSizeF(), "expectedSize"); } +void TestLibinputDevice::testLeftHandedEnabledByDefault_data() +{ + QTest::addColumn("enabled"); + + QTest::newRow("enabled") << true; + QTest::newRow("disabled") << false; +} + +void TestLibinputDevice::testLeftHandedEnabledByDefault() +{ + QFETCH(bool, enabled); + libinput_device device; + device.leftHandedEnabledByDefault = enabled; + + Device d(&device); + QCOMPARE(d.leftHandedEnabledByDefault(), enabled); + QCOMPARE(d.property("leftHandedEnabledByDefault").toBool(), enabled); +} + void TestLibinputDevice::testTapEnabledByDefault_data() { QTest::addColumn("enabled"); @@ -1699,5 +1722,47 @@ void TestLibinputDevice::testLoadScrollButton() } } +void TestLibinputDevice::testLoadLeftHanded_data() +{ + QTest::addColumn("initValue"); + QTest::addColumn("configValue"); + + QTest::newRow("false -> true") << false << true; + QTest::newRow("true -> false") << true << false; + QTest::newRow("true -> true") << true << true; + QTest::newRow("false -> false") << false << false; +} + +void TestLibinputDevice::testLoadLeftHanded() +{ + auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); + KConfigGroup inputConfig(config, QStringLiteral("Test")); + QFETCH(bool, configValue); + QFETCH(bool, initValue); + inputConfig.writeEntry("LeftHanded", configValue); + + libinput_device device; + device.supportsLeftHanded = true; + device.leftHanded = initValue; + device.setLeftHandedReturnValue = false; + + Device d(&device); + QCOMPARE(d.isLeftHanded(), initValue); + // no config group set, should not change + d.loadConfiguration(); + QCOMPARE(d.isLeftHanded(), initValue); + + // set the group + d.setConfig(inputConfig); + d.loadConfiguration(); + QCOMPARE(d.isLeftHanded(), configValue); + + // and try to store + if (configValue != initValue) { + d.setLeftHanded(initValue); + QCOMPARE(inputConfig.readEntry("LeftHanded", configValue), initValue); + } +} + QTEST_GUILESS_MAIN(TestLibinputDevice) #include "device_test.moc" diff --git a/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp index 387a63c8bb..2d7baddb55 100644 --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -194,6 +194,11 @@ int libinput_device_config_left_handed_get(struct libinput_device *device) return device->leftHanded; } +int libinput_device_config_left_handed_get_default(struct libinput_device *device) +{ + return device->leftHandedEnabledByDefault; +} + double libinput_device_config_accel_get_speed(struct libinput_device *device) { return device->pointerAcceleration; diff --git a/autotests/libinput/mock_libinput.h b/autotests/libinput/mock_libinput.h index e553884c50..3e074737f1 100644 --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -59,6 +59,7 @@ struct libinput_device { bool middleEmulation = false; qreal pointerAcceleration = 0.0; int setPointerAccelerationReturnValue = 0; + bool leftHandedEnabledByDefault = false; bool leftHanded = false; int setLeftHandedReturnValue = 0; bool naturalScrollEnabledByDefault = false; diff --git a/libinput/device.cpp b/libinput/device.cpp index ad43c02802..c55da3b83a 100644 --- a/libinput/device.cpp +++ b/libinput/device.cpp @@ -74,6 +74,7 @@ Device *Device::getDevice(libinput_device *native) enum class ConfigKey { Enabled, + LeftHanded, TapToClick, TapAndDrag, TapDragLock, @@ -99,6 +100,7 @@ struct ConfigData { static const QMap s_configData { {ConfigKey::Enabled, {QByteArrayLiteral("Enabled"), {&Device::setEnabled, std::function()}, {}}}, + {ConfigKey::LeftHanded, {QByteArrayLiteral("LeftHanded"), {&Device::setLeftHanded, &Device::leftHandedEnabledByDefault}, {}}}, {ConfigKey::TapToClick, {QByteArrayLiteral("TapToClick"), {&Device::setTapToClick, &Device::tapToClickEnabledByDefault}, {}}}, {ConfigKey::TapAndDrag, {QByteArrayLiteral("TapAndDrag"), {&Device::setTapAndDrag, &Device::tapAndDragEnabledByDefault}, {}}}, {ConfigKey::TapDragLock, {QByteArrayLiteral("TapDragLock"), {&Device::setTapDragLock, &Device::tapDragLockEnabledByDefault}, {}}}, @@ -146,6 +148,7 @@ Device::Device(libinput_device *device, QObject *parent) , m_supportsNaturalScroll(libinput_device_config_scroll_has_natural_scroll(m_device)) , m_supportedScrollMethods(libinput_device_config_scroll_get_methods(m_device)) , m_middleEmulationEnabledByDefault(libinput_device_config_middle_emulation_get_default_enabled(m_device) == LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED) + , m_leftHandedEnabledByDefault(libinput_device_config_left_handed_get_default(m_device)) , m_naturalScrollEnabledByDefault(libinput_device_config_scroll_get_default_natural_scroll_enabled(m_device)) , m_defaultScrollMethod(libinput_device_config_scroll_get_default_method(m_device)) , m_defaultScrollButton(libinput_device_config_scroll_get_default_button(m_device)) @@ -252,19 +255,6 @@ void Device::loadConfiguration() m_loading = false; } -void Device::setLeftHanded(bool set) -{ - if (!m_supportsLeftHanded) { - return; - } - if (libinput_device_config_left_handed_set(m_device, set) == LIBINPUT_CONFIG_STATUS_SUCCESS) { - if (m_leftHanded != set) { - m_leftHanded = set; - emit leftHandedChanged(); - } - } -} - void Device::setPointerAcceleration(qreal acceleration) { if (!m_supportsPointerAcceleration) { @@ -279,20 +269,6 @@ void Device::setPointerAcceleration(qreal acceleration) } } -void Device::setNaturalScroll(bool set) -{ - if (!m_supportsNaturalScroll) { - return; - } - if (libinput_device_config_scroll_set_natural_scroll_enabled(m_device, set) == LIBINPUT_CONFIG_STATUS_SUCCESS) { - if (m_naturalScroll != set) { - m_naturalScroll = set; - writeEntry(ConfigKey::NaturalScroll, m_naturalScroll); - emit naturalScrollChanged(); - } - } -} - bool Device::setScrollMethod(bool set, enum libinput_config_scroll_method method) { if (!(m_supportedScrollMethods & method)) { @@ -320,18 +296,24 @@ bool Device::setScrollMethod(bool set, enum libinput_config_scroll_method method void Device::setScrollTwoFinger(bool set) { if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_2FG)) { writeEntry(ConfigKey::ScrollTwoFinger, set); + writeEntry(ConfigKey::ScrollEdge, !set); + writeEntry(ConfigKey::ScrollOnButton, !set); } } void Device::setScrollEdge(bool set) { if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_EDGE)) { writeEntry(ConfigKey::ScrollEdge, set); + writeEntry(ConfigKey::ScrollTwoFinger, !set); + writeEntry(ConfigKey::ScrollOnButton, !set); } } void Device::setScrollOnButtonDown(bool set) { if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)) { writeEntry(ConfigKey::ScrollOnButton, set); + writeEntry(ConfigKey::ScrollTwoFinger, !set); + writeEntry(ConfigKey::ScrollEdge, !set); } } @@ -349,6 +331,26 @@ void Device::setScrollButton(quint32 button) } } +#define CONFIG(method, condition, function, variable, key) \ +void Device::method(bool set) \ +{ \ + if (condition) { \ + return; \ + } \ + if (libinput_device_config_##function(m_device, set) == LIBINPUT_CONFIG_STATUS_SUCCESS) { \ + if (m_##variable != set) { \ + m_##variable = set; \ + writeEntry(ConfigKey::key, m_##variable); \ + emit variable##Changed(); \ + }\ + } \ +} + +CONFIG(setLeftHanded, !m_supportsLeftHanded, left_handed_set, leftHanded, LeftHanded) +CONFIG(setNaturalScroll, !m_supportsNaturalScroll, scroll_set_natural_scroll_enabled, naturalScroll, NaturalScroll) + +#undef CONFIG + #define CONFIG(method, condition, function, enum, variable, key) \ void Device::method(bool set) \ { \ diff --git a/libinput/device.h b/libinput/device.h index 21955383f9..03d5edd9fd 100644 --- a/libinput/device.h +++ b/libinput/device.h @@ -74,6 +74,7 @@ class Device : public QObject Q_PROPERTY(bool scrollOnButtonDownEnabledByDefault READ scrollOnButtonDownEnabledByDefault CONSTANT) Q_PROPERTY(quint32 defaultScrollButton READ defaultScrollButton CONSTANT) Q_PROPERTY(bool middleEmulation READ isMiddleEmulation WRITE setMiddleEmulation NOTIFY middleEmulationChanged) + Q_PROPERTY(bool leftHandedEnabledByDefault READ leftHandedEnabledByDefault CONSTANT) Q_PROPERTY(bool leftHanded READ isLeftHanded WRITE setLeftHanded NOTIFY leftHandedChanged) Q_PROPERTY(bool naturalScroll READ isNaturalScroll WRITE setNaturalScroll NOTIFY naturalScrollChanged) Q_PROPERTY(bool scrollTwoFinger READ isScrollTwoFinger WRITE setScrollTwoFinger NOTIFY scrollMethodChanged) @@ -193,6 +194,9 @@ public: bool supportsScrollOnButtonDown() const { return (m_supportedScrollMethods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN); } + bool leftHandedEnabledByDefault() const { + return m_leftHandedEnabledByDefault; + } bool middleEmulationEnabledByDefault() const { return m_middleEmulationEnabledByDefault; } @@ -337,6 +341,7 @@ private: quint32 m_supportedScrollMethods; bool m_supportsScrollEdge; bool m_supportsScrollOnButtonDown; + bool m_leftHandedEnabledByDefault; bool m_middleEmulationEnabledByDefault; bool m_naturalScrollEnabledByDefault; enum libinput_config_scroll_method m_defaultScrollMethod;