Changeset View
Changeset View
Standalone View
Standalone View
libinput/device.cpp
Show All 19 Lines | |||||
20 | #include "device.h" | 20 | #include "device.h" | ||
21 | 21 | | |||
22 | #include <QDBusConnection> | 22 | #include <QDBusConnection> | ||
23 | 23 | | |||
24 | #include <linux/input.h> | 24 | #include <linux/input.h> | ||
25 | 25 | | |||
26 | #include <config-kwin.h> | 26 | #include <config-kwin.h> | ||
27 | 27 | | |||
28 | #include <functional> | | |||
29 | | ||||
30 | namespace KWin | 28 | namespace KWin | ||
31 | { | 29 | { | ||
32 | namespace LibInput | 30 | namespace LibInput | ||
33 | { | 31 | { | ||
34 | 32 | | |||
35 | static bool checkAlphaNumericKeyboard(libinput_device *device) | 33 | static bool checkAlphaNumericKeyboard(libinput_device *device) | ||
36 | { | 34 | { | ||
37 | for (uint i = KEY_1; i <= KEY_0; i++) { | 35 | for (uint i = KEY_1; i <= KEY_0; i++) { | ||
Show All 37 Lines | |||||
75 | enum class ConfigKey { | 73 | enum class ConfigKey { | ||
76 | Enabled, | 74 | Enabled, | ||
77 | LeftHanded, | 75 | LeftHanded, | ||
78 | TapToClick, | 76 | TapToClick, | ||
79 | TapAndDrag, | 77 | TapAndDrag, | ||
80 | TapDragLock, | 78 | TapDragLock, | ||
81 | MiddleButtonEmulation, | 79 | MiddleButtonEmulation, | ||
82 | NaturalScroll, | 80 | NaturalScroll, | ||
83 | ScrollTwoFinger, | 81 | ScrollMethod, | ||
84 | ScrollEdge, | | |||
85 | ScrollOnButton, | | |||
86 | ScrollButton | 82 | ScrollButton | ||
87 | }; | 83 | }; | ||
88 | 84 | | |||
89 | struct ConfigData { | 85 | struct ConfigData { | ||
86 | explicit ConfigData(QByteArray _key, void (Device::*_setter)(bool), bool (Device::*_defaultValue)() const = nullptr) | ||||
87 | : key(_key) | ||||
88 | { booleanSetter.setter = _setter; booleanSetter.defaultValue = _defaultValue; } | ||||
89 | | ||||
90 | explicit ConfigData(QByteArray _key, void (Device::*_setter)(quint32), quint32 (Device::*_defaultValue)() const = nullptr) | ||||
91 | : key(_key) | ||||
92 | { quint32Setter.setter = _setter; quint32Setter.defaultValue = _defaultValue; } | ||||
93 | | ||||
90 | QByteArray key; | 94 | QByteArray key; | ||
91 | struct BooleanSetter { | 95 | | ||
92 | std::function<void (Device*, bool)> setter; | 96 | struct { | ||
93 | std::function<bool (Device*)> defaultValue; | 97 | void (Device::*setter)(bool) = nullptr; | ||
98 | bool (Device::*defaultValue)() const; | ||||
94 | } booleanSetter; | 99 | } booleanSetter; | ||
95 | struct UintSetter { | 100 | | ||
96 | std::function<void (Device*, quint32)> setter; | 101 | struct { | ||
97 | std::function<quint32 (Device*)> defaultValue; | 102 | void (Device::*setter)(quint32) = nullptr; | ||
103 | quint32 (Device::*defaultValue)() const; | ||||
98 | } quint32Setter; | 104 | } quint32Setter; | ||
99 | }; | 105 | }; | ||
100 | 106 | | |||
101 | static const QMap<ConfigKey, ConfigData> s_configData { | 107 | static const QMap<ConfigKey, ConfigData> s_configData { | ||
102 | {ConfigKey::Enabled, {QByteArrayLiteral("Enabled"), {&Device::setEnabled, std::function<bool (Device*)>()}, {}}}, | 108 | {ConfigKey::Enabled, ConfigData(QByteArrayLiteral("Enabled"), &Device::setEnabled)}, | ||
103 | {ConfigKey::LeftHanded, {QByteArrayLiteral("LeftHanded"), {&Device::setLeftHanded, &Device::leftHandedEnabledByDefault}, {}}}, | 109 | {ConfigKey::LeftHanded, ConfigData(QByteArrayLiteral("LeftHanded"), &Device::setLeftHanded, &Device::leftHandedEnabledByDefault)}, | ||
104 | {ConfigKey::TapToClick, {QByteArrayLiteral("TapToClick"), {&Device::setTapToClick, &Device::tapToClickEnabledByDefault}, {}}}, | 110 | {ConfigKey::TapToClick, ConfigData(QByteArrayLiteral("TapToClick"), &Device::setTapToClick, &Device::tapToClickEnabledByDefault)}, | ||
105 | {ConfigKey::TapAndDrag, {QByteArrayLiteral("TapAndDrag"), {&Device::setTapAndDrag, &Device::tapAndDragEnabledByDefault}, {}}}, | 111 | {ConfigKey::TapAndDrag, ConfigData(QByteArrayLiteral("TapAndDrag"), &Device::setTapAndDrag, &Device::tapAndDragEnabledByDefault)}, | ||
106 | {ConfigKey::TapDragLock, {QByteArrayLiteral("TapDragLock"), {&Device::setTapDragLock, &Device::tapDragLockEnabledByDefault}, {}}}, | 112 | {ConfigKey::TapDragLock, ConfigData(QByteArrayLiteral("TapDragLock"), &Device::setTapDragLock, &Device::tapDragLockEnabledByDefault)}, | ||
107 | {ConfigKey::MiddleButtonEmulation, {QByteArrayLiteral("MiddleButtonEmulation"), {&Device::setMiddleEmulation, &Device::middleEmulationEnabledByDefault}, {}}}, | 113 | {ConfigKey::MiddleButtonEmulation, ConfigData(QByteArrayLiteral("MiddleButtonEmulation"), &Device::setMiddleEmulation, &Device::middleEmulationEnabledByDefault)}, | ||
108 | {ConfigKey::NaturalScroll, {QByteArrayLiteral("NaturalScroll"), {&Device::setNaturalScroll, &Device::naturalScrollEnabledByDefault}, {}}}, | 114 | {ConfigKey::NaturalScroll, ConfigData(QByteArrayLiteral("NaturalScroll"), &Device::setNaturalScroll, &Device::naturalScrollEnabledByDefault)}, | ||
109 | {ConfigKey::ScrollTwoFinger, {QByteArrayLiteral("ScrollTwoFinger"), {&Device::setScrollTwoFinger, &Device::scrollTwoFingerEnabledByDefault}, {}}}, | 115 | {ConfigKey::ScrollMethod, ConfigData(QByteArrayLiteral("ScrollMethod"), &Device::activateScrollMethodFromInt, &Device::defaultScrollMethodToInt)}, | ||
110 | {ConfigKey::ScrollEdge, {QByteArrayLiteral("ScrollEdge"), {&Device::setScrollEdge, &Device::scrollEdgeEnabledByDefault}, {}}}, | 116 | {ConfigKey::ScrollButton, ConfigData(QByteArrayLiteral("ScrollButton"), &Device::setScrollButton, &Device::defaultScrollButton)} | ||
111 | {ConfigKey::ScrollOnButton, {QByteArrayLiteral("ScrollOnButton"), {&Device::setScrollOnButtonDown, &Device::scrollOnButtonDownEnabledByDefault}, {}}}, | | |||
112 | {ConfigKey::ScrollButton, {QByteArrayLiteral("ScrollButton"), {}, {&Device::setScrollButton, &Device::defaultScrollButton}}} | | |||
113 | }; | 117 | }; | ||
114 | 118 | | |||
115 | Device::Device(libinput_device *device, QObject *parent) | 119 | Device::Device(libinput_device *device, QObject *parent) | ||
116 | : QObject(parent) | 120 | : QObject(parent) | ||
117 | , m_device(device) | 121 | , m_device(device) | ||
118 | , m_keyboard(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_KEYBOARD)) | 122 | , m_keyboard(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_KEYBOARD)) | ||
119 | , m_pointer(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_POINTER)) | 123 | , m_pointer(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_POINTER)) | ||
120 | , m_touch(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_TOUCH)) | 124 | , m_touch(libinput_device_has_capability(m_device, LIBINPUT_DEVICE_CAP_TOUCH)) | ||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | |||||
229 | } | 233 | } | ||
230 | 234 | | |||
231 | template <typename T, typename Setter> | 235 | template <typename T, typename Setter> | ||
232 | void Device::readEntry(const QByteArray &key, const Setter &s, const T &defaultValue) | 236 | void Device::readEntry(const QByteArray &key, const Setter &s, const T &defaultValue) | ||
233 | { | 237 | { | ||
234 | if (!s.setter) { | 238 | if (!s.setter) { | ||
235 | return; | 239 | return; | ||
236 | } | 240 | } | ||
237 | s.setter(this, m_config.readEntry(key.constData(), s.defaultValue ? s.defaultValue(this) : defaultValue)); | 241 | | ||
242 | (this->*(s.setter))(m_config.readEntry(key.constData(), s.defaultValue ? (this->*(s.defaultValue))() : defaultValue)); | ||||
238 | } | 243 | } | ||
239 | 244 | | |||
240 | void Device::loadConfiguration() | 245 | void Device::loadConfiguration() | ||
241 | { | 246 | { | ||
242 | if (!m_config.isValid()) { | 247 | if (!m_config.isValid()) { | ||
243 | return; | 248 | return; | ||
244 | } | 249 | } | ||
245 | m_loading = true; | 250 | m_loading = true; | ||
Show All 18 Lines | 264 | { | |||
264 | if (libinput_device_config_accel_set_speed(m_device, acceleration) == LIBINPUT_CONFIG_STATUS_SUCCESS) { | 269 | if (libinput_device_config_accel_set_speed(m_device, acceleration) == LIBINPUT_CONFIG_STATUS_SUCCESS) { | ||
265 | if (m_pointerAcceleration != acceleration) { | 270 | if (m_pointerAcceleration != acceleration) { | ||
266 | m_pointerAcceleration = acceleration; | 271 | m_pointerAcceleration = acceleration; | ||
267 | emit pointerAccelerationChanged(); | 272 | emit pointerAccelerationChanged(); | ||
268 | } | 273 | } | ||
269 | } | 274 | } | ||
270 | } | 275 | } | ||
271 | 276 | | |||
272 | bool Device::setScrollMethod(bool set, enum libinput_config_scroll_method method) | 277 | void Device::setScrollMethod(bool set, enum libinput_config_scroll_method method) | ||
273 | { | 278 | { | ||
274 | if (!(m_supportedScrollMethods & method)) { | 279 | bool stays_the_same = (m_scrollMethod == method) == set; | ||
275 | return false; | 280 | if (!(m_supportedScrollMethods & method) || stays_the_same) { | ||
276 | } | 281 | return; | ||
277 | if (set) { | | |||
278 | if (m_scrollMethod == method) { | | |||
279 | return false; | | |||
280 | } | | |||
281 | } else { | | |||
282 | if (m_scrollMethod != method) { | | |||
283 | return false; | | |||
284 | } | 282 | } | ||
283 | | ||||
284 | if (!set) { | ||||
285 | method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; | 285 | method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; | ||
286 | } | 286 | } | ||
287 | 287 | | |||
288 | if (libinput_device_config_scroll_set_method(m_device, method) == LIBINPUT_CONFIG_STATUS_SUCCESS) { | 288 | if (libinput_device_config_scroll_set_method(m_device, method) == LIBINPUT_CONFIG_STATUS_SUCCESS) { | ||
289 | m_scrollMethod = method; | 289 | m_scrollMethod = method; | ||
290 | emit scrollMethodChanged(); | 290 | emit scrollMethodChanged(); | ||
291 | return true; | 291 | writeEntry(ConfigKey::ScrollMethod, (quint32) method); | ||
292 | } | | |||
293 | return false; | | |||
294 | } | | |||
295 | | ||||
296 | void Device::setScrollTwoFinger(bool set) { | | |||
297 | if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_2FG)) { | | |||
298 | writeEntry(ConfigKey::ScrollTwoFinger, set); | | |||
299 | writeEntry(ConfigKey::ScrollEdge, !set); | | |||
300 | writeEntry(ConfigKey::ScrollOnButton, !set); | | |||
301 | } | | |||
302 | } | | |||
303 | | ||||
304 | void Device::setScrollEdge(bool set) { | | |||
305 | if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_EDGE)) { | | |||
306 | writeEntry(ConfigKey::ScrollEdge, set); | | |||
307 | writeEntry(ConfigKey::ScrollTwoFinger, !set); | | |||
308 | writeEntry(ConfigKey::ScrollOnButton, !set); | | |||
309 | } | | |||
310 | } | | |||
311 | | ||||
312 | void Device::setScrollOnButtonDown(bool set) { | | |||
313 | if (setScrollMethod(set, LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)) { | | |||
314 | writeEntry(ConfigKey::ScrollOnButton, set); | | |||
315 | writeEntry(ConfigKey::ScrollTwoFinger, !set); | | |||
316 | writeEntry(ConfigKey::ScrollEdge, !set); | | |||
317 | } | 292 | } | ||
318 | } | 293 | } | ||
319 | 294 | | |||
320 | void Device::setScrollButton(quint32 button) | 295 | void Device::setScrollButton(quint32 button) | ||
321 | { | 296 | { | ||
322 | if (!(m_supportedScrollMethods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)) { | 297 | if (!(m_supportedScrollMethods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)) { | ||
323 | return; | 298 | return; | ||
324 | } | 299 | } | ||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |