Changeset View
Changeset View
Standalone View
Standalone View
kcms/input/backends/kwin_wl/kwin_wl_device.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Roman Gilg <subdiff@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kwin_wl_device.h" | ||||
20 | | ||||
21 | #include <QDBusInterface> | ||||
22 | #include <QDBusError> | ||||
23 | #include <QVector> | ||||
24 | | ||||
25 | #include "logging.h" | ||||
26 | | ||||
27 | namespace { | ||||
28 | template<typename T> | ||||
29 | T valueLoaderPart(QVariant const &reply) { Q_UNUSED(reply); return T(); } | ||||
30 | | ||||
31 | template<> | ||||
32 | bool valueLoaderPart(QVariant const &reply) { return reply.toBool(); } | ||||
33 | | ||||
34 | template<> | ||||
35 | int valueLoaderPart(QVariant const &reply) { return reply.toInt(); } | ||||
36 | | ||||
37 | template<> | ||||
38 | quint32 valueLoaderPart(QVariant const &reply) { return reply.toInt(); } | ||||
39 | | ||||
40 | template<> | ||||
41 | qreal valueLoaderPart(QVariant const &reply) { return reply.toReal(); } | ||||
42 | | ||||
43 | template<> | ||||
44 | QString valueLoaderPart(QVariant const &reply) { return reply.toString(); } | ||||
45 | | ||||
46 | template<> | ||||
47 | Qt::MouseButtons valueLoaderPart(QVariant const &reply) { return static_cast<Qt::MouseButtons>(reply.toInt()); } | ||||
48 | } | ||||
49 | | ||||
50 | KWinWaylandDevice::KWinWaylandDevice(QString dbusName) | ||||
51 | { | ||||
52 | m_iface = new QDBusInterface(QStringLiteral("org.kde.KWin"), | ||||
53 | QStringLiteral("/org/kde/KWin/InputDevice/") + dbusName, | ||||
54 | QStringLiteral("org.kde.KWin.InputDevice"), | ||||
55 | QDBusConnection::sessionBus(), | ||||
56 | this); | ||||
57 | } | ||||
58 | | ||||
59 | KWinWaylandDevice::~KWinWaylandDevice() | ||||
60 | { | ||||
61 | delete m_iface; | ||||
62 | } | ||||
63 | | ||||
64 | bool KWinWaylandDevice::init() | ||||
65 | { | ||||
66 | // need to do it here in order to populate combobox and handle events | ||||
67 | return valueLoader(m_name) && valueLoader(m_sysName); | ||||
68 | } | ||||
69 | | ||||
70 | bool KWinWaylandDevice::getConfig() | ||||
71 | { | ||||
72 | bool success = true; | ||||
73 | | ||||
74 | // general | ||||
75 | success &= valueLoader(m_supportsDisableEvents); | ||||
76 | success &= valueLoader(m_enabled); | ||||
77 | // advanced | ||||
78 | success &= valueLoader(m_supportedButtons); | ||||
79 | success &= valueLoader(m_supportsLeftHanded); | ||||
80 | success &= valueLoader(m_leftHandedEnabledByDefault); | ||||
81 | success &= valueLoader(m_leftHanded); | ||||
82 | success &= valueLoader(m_supportsMiddleEmulation); | ||||
83 | success &= valueLoader(m_middleEmulationEnabledByDefault); | ||||
84 | success &= valueLoader(m_middleEmulation); | ||||
85 | // acceleration | ||||
86 | success &= valueLoader(m_supportsPointerAcceleration); | ||||
87 | success &= valueLoader(m_supportsPointerAccelerationProfileFlat); | ||||
88 | success &= valueLoader(m_supportsPointerAccelerationProfileAdaptive); | ||||
89 | success &= valueLoader(m_defaultPointerAcceleration); | ||||
90 | success &= valueLoader(m_defaultPointerAccelerationProfileFlat); | ||||
91 | success &= valueLoader(m_defaultPointerAccelerationProfileAdaptive); | ||||
92 | success &= valueLoader(m_pointerAcceleration); | ||||
93 | success &= valueLoader(m_pointerAccelerationProfileFlat); | ||||
94 | success &= valueLoader(m_pointerAccelerationProfileAdaptive); | ||||
95 | // natural scroll | ||||
96 | success &= valueLoader(m_supportsNaturalScroll); | ||||
97 | success &= valueLoader(m_naturalScrollEnabledByDefault); | ||||
98 | success &= valueLoader(m_naturalScroll); | ||||
99 | | ||||
100 | return success; | ||||
101 | } | ||||
102 | | ||||
103 | bool KWinWaylandDevice::getDefaultConfig() | ||||
104 | { | ||||
105 | m_enabled.set(true); | ||||
106 | m_leftHanded.set(false); | ||||
107 | | ||||
108 | m_pointerAcceleration.set(m_defaultPointerAcceleration); | ||||
109 | m_pointerAccelerationProfileFlat.set(m_defaultPointerAccelerationProfileFlat); | ||||
110 | m_pointerAccelerationProfileAdaptive.set(m_defaultPointerAccelerationProfileAdaptive); | ||||
111 | | ||||
112 | m_middleEmulation.set(m_middleEmulationEnabledByDefault); | ||||
113 | m_naturalScroll.set(m_naturalScrollEnabledByDefault); | ||||
114 | | ||||
115 | return true; | ||||
116 | } | ||||
117 | | ||||
118 | bool KWinWaylandDevice::applyConfig() | ||||
119 | { | ||||
120 | QVector<QString> msgs; | ||||
121 | | ||||
122 | msgs << valueWriter(m_enabled) | ||||
123 | << valueWriter(m_leftHanded) | ||||
124 | << valueWriter(m_pointerAcceleration) | ||||
125 | << valueWriter(m_defaultPointerAccelerationProfileFlat) | ||||
126 | << valueWriter(m_defaultPointerAccelerationProfileAdaptive) | ||||
127 | << valueWriter(m_middleEmulation) | ||||
128 | << valueWriter(m_naturalScroll); | ||||
129 | | ||||
130 | bool success = true; | ||||
131 | QString error_msg; | ||||
132 | | ||||
133 | for (QString m : msgs) { | ||||
134 | if (!m.isNull()) { | ||||
135 | qCCritical(KCM_INPUT) << "in error:" << m; | ||||
136 | if (!success) { | ||||
137 | error_msg.append("\n"); | ||||
138 | } | ||||
139 | error_msg.append(m); | ||||
140 | success = false; | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | if (!success) { | ||||
145 | qCCritical(KCM_INPUT) << error_msg; | ||||
146 | } | ||||
147 | return success; | ||||
148 | } | ||||
149 | | ||||
150 | bool KWinWaylandDevice::isChangedConfig() const | ||||
151 | { | ||||
152 | return m_enabled.changed() || | ||||
153 | m_leftHanded.changed() || | ||||
154 | m_pointerAcceleration.changed() || | ||||
155 | m_pointerAccelerationProfileFlat.changed() || | ||||
156 | m_pointerAccelerationProfileAdaptive.changed() || | ||||
157 | m_middleEmulation.changed() || | ||||
158 | m_naturalScroll.changed(); | ||||
159 | } | ||||
160 | | ||||
161 | template<typename T> | ||||
162 | QString KWinWaylandDevice::valueWriter(const Prop<T> &prop) | ||||
163 | { | ||||
164 | if (!prop.changed()) { | ||||
165 | return QString(); | ||||
166 | } | ||||
167 | m_iface->setProperty(prop.dbus, prop.val); | ||||
168 | QDBusError error = m_iface->lastError(); | ||||
169 | if (error.isValid()) { | ||||
170 | qCCritical(KCM_INPUT) << error.message(); | ||||
171 | return error.message(); | ||||
172 | } | ||||
173 | return QString(); | ||||
174 | } | ||||
175 | | ||||
176 | template<typename T> | ||||
177 | bool KWinWaylandDevice::valueLoader(Prop<T> &prop) | ||||
178 | { | ||||
179 | QVariant reply = m_iface->property(prop.dbus); | ||||
180 | if (!reply.isValid()) { | ||||
181 | qCCritical(KCM_INPUT) << "Error on d-bus read of" << prop.dbus; | ||||
182 | prop.avail = false; | ||||
183 | return false; | ||||
184 | } | ||||
185 | prop.avail = true; | ||||
186 | | ||||
187 | T replyValue = valueLoaderPart<T>(reply); | ||||
188 | | ||||
189 | prop.old = replyValue; | ||||
190 | prop.val = replyValue; | ||||
191 | return true; | ||||
192 | } |