diff --git a/autotests/libinput/device_test.cpp b/autotests/libinput/device_test.cpp --- a/autotests/libinput/device_test.cpp +++ b/autotests/libinput/device_test.cpp @@ -127,12 +127,8 @@ void testLoadMiddleButtonEmulation(); void testLoadNaturalScroll_data(); void testLoadNaturalScroll(); - void testLoadScrollTwoFinger_data(); - void testLoadScrollTwoFinger(); - void testLoadScrollEdge_data(); - void testLoadScrollEdge(); - void testLoadScrollOnButton_data(); - void testLoadScrollOnButton(); + void testLoadScrollMethod_data(); + void testLoadScrollMethod(); void testLoadScrollButton_data(); void testLoadScrollButton(); void testLoadLeftHanded_data(); @@ -1552,129 +1548,55 @@ } } -void TestLibinputDevice::testLoadScrollTwoFinger_data() +void TestLibinputDevice::testLoadScrollMethod_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::testLoadScrollTwoFinger() -{ - auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); - KConfigGroup inputConfig(config, QStringLiteral("Test")); - QFETCH(bool, configValue); - QFETCH(bool, initValue); - inputConfig.writeEntry("ScrollTwoFinger", configValue); - - libinput_device device; - device.supportedScrollMethods = LIBINPUT_CONFIG_SCROLL_2FG; - device.scrollMethod = initValue ? LIBINPUT_CONFIG_SCROLL_2FG : LIBINPUT_CONFIG_SCROLL_NO_SCROLL; - device.setScrollMethodReturnValue = false; - - Device d(&device); - QCOMPARE(d.isScrollTwoFinger(), initValue); - // no config group set, should not change - d.loadConfiguration(); - QCOMPARE(d.isScrollTwoFinger(), initValue); - - // set the group - d.setConfig(inputConfig); - d.loadConfiguration(); - QCOMPARE(d.isScrollTwoFinger(), configValue); - - // and try to store - if (configValue != initValue) { - d.setScrollTwoFinger(initValue); - QCOMPARE(inputConfig.readEntry("ScrollTwoFinger", configValue), initValue); - } -} - -void TestLibinputDevice::testLoadScrollEdge_data() -{ - QTest::addColumn("initValue"); - QTest::addColumn("configValue"); + QTest::addColumn("initValue"); + QTest::addColumn("initValuePropNameString"); + QTest::addColumn("configValue"); + QTest::addColumn("configValuePropNameString"); - QTest::newRow("false -> true") << false << true; - QTest::newRow("true -> false") << true << false; - QTest::newRow("true -> true") << true << true; - QTest::newRow("false -> false") << false << false; + QTest::newRow("scrollTwoFinger -> scrollEdge") << (quint32) LIBINPUT_CONFIG_SCROLL_2FG << "scrollTwoFinger" << (quint32) LIBINPUT_CONFIG_SCROLL_EDGE << "scrollEdge"; + QTest::newRow("scrollOnButtonDown -> scrollTwoFinger") << (quint32) LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN << "scrollOnButtonDown" << (quint32) LIBINPUT_CONFIG_SCROLL_2FG << "scrollTwoFinger"; + QTest::newRow("scrollEdge -> scrollEdge") << (quint32) LIBINPUT_CONFIG_SCROLL_EDGE << "scrollEdge" << (quint32) LIBINPUT_CONFIG_SCROLL_EDGE << "scrollEdge"; } -void TestLibinputDevice::testLoadScrollEdge() +void TestLibinputDevice::testLoadScrollMethod() { auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); KConfigGroup inputConfig(config, QStringLiteral("Test")); - QFETCH(bool, configValue); - QFETCH(bool, initValue); - inputConfig.writeEntry("ScrollEdge", configValue); - - libinput_device device; - device.supportedScrollMethods = LIBINPUT_CONFIG_SCROLL_EDGE; - device.scrollMethod = initValue ? LIBINPUT_CONFIG_SCROLL_EDGE : LIBINPUT_CONFIG_SCROLL_NO_SCROLL; - device.setScrollMethodReturnValue = false; - - Device d(&device); - QCOMPARE(d.isScrollEdge(), initValue); - // no config group set, should not change - d.loadConfiguration(); - QCOMPARE(d.isScrollEdge(), initValue); - - // set the group - d.setConfig(inputConfig); - d.loadConfiguration(); - QCOMPARE(d.isScrollEdge(), configValue); - - // and try to store - if (configValue != initValue) { - d.setScrollEdge(initValue); - QCOMPARE(inputConfig.readEntry("ScrollEdge", configValue), initValue); - } -} + QFETCH(quint32, initValue); + QFETCH(quint32, configValue); + QFETCH(QString, initValuePropNameString); + QFETCH(QString, configValuePropNameString); -void TestLibinputDevice::testLoadScrollOnButton_data() -{ - QTest::addColumn("initValue"); - QTest::addColumn("configValue"); + QByteArray initValuePropName = initValuePropNameString.toLatin1(); + QByteArray configValuePropName = configValuePropNameString.toLatin1(); - 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::testLoadScrollOnButton() -{ - auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); - KConfigGroup inputConfig(config, QStringLiteral("Test")); - QFETCH(bool, configValue); - QFETCH(bool, initValue); - inputConfig.writeEntry("ScrollOnButton", configValue); + inputConfig.writeEntry("ScrollMethod", configValue); libinput_device device; - device.supportedScrollMethods = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; - device.scrollMethod = initValue ? LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN : LIBINPUT_CONFIG_SCROLL_NO_SCROLL; + device.supportedScrollMethods = LIBINPUT_CONFIG_SCROLL_2FG | LIBINPUT_CONFIG_SCROLL_EDGE | LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; + device.scrollMethod = (libinput_config_scroll_method) initValue; device.setScrollMethodReturnValue = false; Device d(&device); - QCOMPARE(d.isScrollOnButtonDown(), initValue); + QCOMPARE(d.property(initValuePropName).toBool(), true); + QCOMPARE(d.property(configValuePropName).toBool(), initValue == configValue); // no config group set, should not change d.loadConfiguration(); - QCOMPARE(d.isScrollOnButtonDown(), initValue); + QCOMPARE(d.property(initValuePropName).toBool(), true); + QCOMPARE(d.property(configValuePropName).toBool(), initValue == configValue); // set the group d.setConfig(inputConfig); d.loadConfiguration(); - QCOMPARE(d.isScrollOnButtonDown(), configValue); + QCOMPARE(d.property(initValuePropName).toBool(), initValue == configValue); + QCOMPARE(d.property(configValuePropName).toBool(), true); // and try to store if (configValue != initValue) { - d.setScrollOnButtonDown(initValue); - QCOMPARE(inputConfig.readEntry("ScrollOnButton", configValue), initValue); + d.setProperty(initValuePropName, true); + QCOMPARE(inputConfig.readEntry("ScrollMethod", configValue), initValue); } } diff --git a/libinput/device.h b/libinput/device.h --- a/libinput/device.h +++ b/libinput/device.h @@ -203,6 +203,12 @@ bool naturalScrollEnabledByDefault() const { return m_naturalScrollEnabledByDefault; } + enum libinput_config_scroll_method defaultScrollMethod() const { + return m_defaultScrollMethod; + } + quint32 defaultScrollMethodToInt() const { + return (quint32) m_defaultScrollMethod; + } bool scrollTwoFingerEnabledByDefault() const { return m_defaultScrollMethod == LIBINPUT_CONFIG_SCROLL_2FG; } @@ -223,19 +229,28 @@ return m_naturalScroll; } void setNaturalScroll(bool set); - bool setScrollMethod(bool set, enum libinput_config_scroll_method method); + void setScrollMethod(bool set, enum libinput_config_scroll_method method); bool isScrollTwoFinger() const { return m_scrollMethod & LIBINPUT_CONFIG_SCROLL_2FG; } - void setScrollTwoFinger(bool set); + void setScrollTwoFinger(bool set) { + setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_2FG); + } bool isScrollEdge() const { return m_scrollMethod & LIBINPUT_CONFIG_SCROLL_EDGE; } - void setScrollEdge(bool set); + void setScrollEdge(bool set) { + setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_EDGE); + } bool isScrollOnButtonDown() const { return m_scrollMethod & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; } - void setScrollOnButtonDown(bool set); + void setScrollOnButtonDown(bool set) { + setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN); + } + void activateScrollMethodFromInt(quint32 method) { + setScrollMethod(true, (libinput_config_scroll_method) method); + } quint32 scrollButton() const { return m_scrollButton; } diff --git a/libinput/device.cpp b/libinput/device.cpp --- a/libinput/device.cpp +++ b/libinput/device.cpp @@ -25,8 +25,6 @@ #include -#include - namespace KWin { namespace LibInput @@ -80,36 +78,42 @@ TapDragLock, MiddleButtonEmulation, NaturalScroll, - ScrollTwoFinger, - ScrollEdge, - ScrollOnButton, + ScrollMethod, ScrollButton }; struct ConfigData { + explicit ConfigData(QByteArray _key, void (Device::*_setter)(bool), bool (Device::*_defaultValue)() const = nullptr) + : key(_key) + { booleanSetter.setter = _setter; booleanSetter.defaultValue = _defaultValue; } + + explicit ConfigData(QByteArray _key, void (Device::*_setter)(quint32), quint32 (Device::*_defaultValue)() const = nullptr) + : key(_key) + { quint32Setter.setter = _setter; quint32Setter.defaultValue = _defaultValue; } + QByteArray key; - struct BooleanSetter { - std::function setter; - std::function defaultValue; + + struct { + void (Device::*setter)(bool) = nullptr; + bool (Device::*defaultValue)() const; } booleanSetter; - struct UintSetter { - std::function setter; - std::function defaultValue; + + struct { + void (Device::*setter)(quint32) = nullptr; + quint32 (Device::*defaultValue)() const; } quint32Setter; }; 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}, {}}}, - {ConfigKey::MiddleButtonEmulation, {QByteArrayLiteral("MiddleButtonEmulation"), {&Device::setMiddleEmulation, &Device::middleEmulationEnabledByDefault}, {}}}, - {ConfigKey::NaturalScroll, {QByteArrayLiteral("NaturalScroll"), {&Device::setNaturalScroll, &Device::naturalScrollEnabledByDefault}, {}}}, - {ConfigKey::ScrollTwoFinger, {QByteArrayLiteral("ScrollTwoFinger"), {&Device::setScrollTwoFinger, &Device::scrollTwoFingerEnabledByDefault}, {}}}, - {ConfigKey::ScrollEdge, {QByteArrayLiteral("ScrollEdge"), {&Device::setScrollEdge, &Device::scrollEdgeEnabledByDefault}, {}}}, - {ConfigKey::ScrollOnButton, {QByteArrayLiteral("ScrollOnButton"), {&Device::setScrollOnButtonDown, &Device::scrollOnButtonDownEnabledByDefault}, {}}}, - {ConfigKey::ScrollButton, {QByteArrayLiteral("ScrollButton"), {}, {&Device::setScrollButton, &Device::defaultScrollButton}}} + {ConfigKey::Enabled, ConfigData(QByteArrayLiteral("Enabled"), &Device::setEnabled)}, + {ConfigKey::LeftHanded, ConfigData(QByteArrayLiteral("LeftHanded"), &Device::setLeftHanded, &Device::leftHandedEnabledByDefault)}, + {ConfigKey::TapToClick, ConfigData(QByteArrayLiteral("TapToClick"), &Device::setTapToClick, &Device::tapToClickEnabledByDefault)}, + {ConfigKey::TapAndDrag, ConfigData(QByteArrayLiteral("TapAndDrag"), &Device::setTapAndDrag, &Device::tapAndDragEnabledByDefault)}, + {ConfigKey::TapDragLock, ConfigData(QByteArrayLiteral("TapDragLock"), &Device::setTapDragLock, &Device::tapDragLockEnabledByDefault)}, + {ConfigKey::MiddleButtonEmulation, ConfigData(QByteArrayLiteral("MiddleButtonEmulation"), &Device::setMiddleEmulation, &Device::middleEmulationEnabledByDefault)}, + {ConfigKey::NaturalScroll, ConfigData(QByteArrayLiteral("NaturalScroll"), &Device::setNaturalScroll, &Device::naturalScrollEnabledByDefault)}, + {ConfigKey::ScrollMethod, ConfigData(QByteArrayLiteral("ScrollMethod"), &Device::activateScrollMethodFromInt, &Device::defaultScrollMethodToInt)}, + {ConfigKey::ScrollButton, ConfigData(QByteArrayLiteral("ScrollButton"), &Device::setScrollButton, &Device::defaultScrollButton)} }; Device::Device(libinput_device *device, QObject *parent) @@ -234,7 +238,8 @@ if (!s.setter) { return; } - s.setter(this, m_config.readEntry(key.constData(), s.defaultValue ? s.defaultValue(this) : defaultValue)); + + (this->*(s.setter))(m_config.readEntry(key.constData(), s.defaultValue ? (this->*(s.defaultValue))() : defaultValue)); } void Device::loadConfiguration() @@ -269,51 +274,21 @@ } } -bool Device::setScrollMethod(bool set, enum libinput_config_scroll_method method) +void Device::setScrollMethod(bool set, enum libinput_config_scroll_method method) { - if (!(m_supportedScrollMethods & method)) { - return false; + bool stays_the_same = (m_scrollMethod == method) == set; + if (!(m_supportedScrollMethods & method) || stays_the_same) { + return; } - if (set) { - if (m_scrollMethod == method) { - return false; - } - } else { - if (m_scrollMethod != method) { - return false; - } + + if (!set) { method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; } if (libinput_device_config_scroll_set_method(m_device, method) == LIBINPUT_CONFIG_STATUS_SUCCESS) { m_scrollMethod = method; emit scrollMethodChanged(); - return true; - } - return false; -} - -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); + writeEntry(ConfigKey::ScrollMethod, (quint32) method); } }