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 @@ -45,6 +45,8 @@ void testTapFingerCount(); void testSize_data(); void testSize(); + void testLeftHandedEnabledByDefault_data(); + void testLeftHandedEnabledByDefault(); void testTapEnabledByDefault_data(); void testTapEnabledByDefault(); void testMiddleEmulationEnabledByDefault_data(); @@ -133,6 +135,8 @@ void testLoadScrollOnButton(); void testLoadScrollButton_data(); void testLoadScrollButton(); + void testLoadLeftHanded_data(); + void testLoadLeftHanded(); }; void TestLibinputDevice::testStaticGetter() @@ -327,6 +331,25 @@ 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::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.h b/autotests/libinput/mock_libinput.h --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -59,6 +59,7 @@ 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/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -194,6 +194,11 @@ 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/libinput/device.h b/libinput/device.h --- a/libinput/device.h +++ b/libinput/device.h @@ -74,6 +74,7 @@ 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 @@ 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 @@ 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; diff --git a/libinput/device.cpp b/libinput/device.cpp --- a/libinput/device.cpp +++ b/libinput/device.cpp @@ -74,6 +74,7 @@ enum class ConfigKey { Enabled, + LeftHanded, TapToClick, TapAndDrag, TapDragLock, @@ -99,6 +100,7 @@ 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 @@ , 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 @@ 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::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 @@ 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 @@ } } +#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) \ { \