Changeset View
Changeset View
Standalone View
Standalone View
kcms/input/backends/kwin_wl/kwin_wl_backend.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_backend.h" | ||||
20 | #include "kwin_wl_device.h" | ||||
21 | | ||||
22 | #include <algorithm> | ||||
23 | | ||||
24 | #include <KLocalizedString> | ||||
25 | | ||||
26 | #include <QDBusMessage> | ||||
27 | #include <QDBusInterface> | ||||
28 | #include <QDBusReply> | ||||
29 | #include <QStringList> | ||||
30 | | ||||
31 | #include "logging.h" | ||||
32 | | ||||
33 | KWinWaylandBackend::KWinWaylandBackend(QObject *parent) : | ||||
34 | InputBackend(parent) | ||||
35 | { | ||||
36 | m_mode = InputBackendMode::KWinWayland; | ||||
37 | | ||||
38 | m_deviceManager = new QDBusInterface (QStringLiteral("org.kde.KWin"), | ||||
39 | QStringLiteral("/org/kde/KWin/InputDevice"), | ||||
40 | QStringLiteral("org.kde.KWin.InputDeviceManager"), | ||||
41 | QDBusConnection::sessionBus(), | ||||
42 | this); | ||||
43 | | ||||
44 | findDevices(); | ||||
45 | | ||||
46 | m_deviceManager->connection().connect(QStringLiteral("org.kde.KWin"), | ||||
47 | QStringLiteral("/org/kde/KWin/InputDevice"), | ||||
48 | QStringLiteral("org.kde.KWin.InputDeviceManager"), | ||||
49 | QStringLiteral("deviceAdded"), | ||||
50 | this, | ||||
51 | SLOT(onDeviceAdded(QString))); | ||||
52 | m_deviceManager->connection().connect(QStringLiteral("org.kde.KWin"), | ||||
53 | QStringLiteral("/org/kde/KWin/InputDevice"), | ||||
54 | QStringLiteral("org.kde.KWin.InputDeviceManager"), | ||||
55 | QStringLiteral("deviceRemoved"), | ||||
56 | this, | ||||
57 | SLOT(onDeviceRemoved(QString))); | ||||
58 | } | ||||
59 | | ||||
60 | KWinWaylandBackend::~KWinWaylandBackend() | ||||
61 | { | ||||
62 | qDeleteAll(m_devices); | ||||
63 | delete m_deviceManager; | ||||
64 | } | ||||
65 | | ||||
66 | void KWinWaylandBackend::findDevices() | ||||
67 | { | ||||
68 | QStringList devicesSysNames; | ||||
69 | const QVariant reply = m_deviceManager->property("devicesSysNames"); | ||||
70 | if (reply.isValid()) { | ||||
71 | qCDebug(KCM_INPUT) << "Devices list received successfully from KWin."; | ||||
72 | devicesSysNames = reply.toStringList(); | ||||
73 | } | ||||
74 | else { | ||||
75 | qCCritical(KCM_INPUT) << "Error on receiving device list from KWin."; | ||||
76 | m_errorString = i18n("Querying input devices failed. Please reopen this settings module."); | ||||
77 | return; | ||||
78 | } | ||||
79 | | ||||
80 | for (QString sn : devicesSysNames) { | ||||
81 | QDBusInterface deviceIface(QStringLiteral("org.kde.KWin"), | ||||
82 | QStringLiteral("/org/kde/KWin/InputDevice/") + sn, | ||||
83 | QStringLiteral("org.kde.KWin.InputDevice"), | ||||
84 | QDBusConnection::sessionBus(), | ||||
85 | this); | ||||
86 | QVariant reply = deviceIface.property("pointer"); | ||||
87 | if (reply.isValid() && reply.toBool()) { | ||||
88 | reply = deviceIface.property("touchpad"); | ||||
89 | if (reply.isValid() && reply.toBool()) { | ||||
90 | continue; | ||||
91 | } | ||||
92 | | ||||
93 | KWinWaylandDevice* dev = new KWinWaylandDevice(sn); | ||||
94 | if (!dev->init()) { | ||||
95 | qCCritical(KCM_INPUT) << "Error on creating device object" << sn; | ||||
96 | m_errorString = i18n("Critical error on reading fundamental device infos of %1.", sn); | ||||
97 | return; | ||||
98 | } | ||||
99 | m_devices.append(dev); | ||||
100 | qCDebug(KCM_INPUT).nospace() << "Device found: " << dev->name() << " (" << dev->sysName() << ")"; | ||||
101 | } | ||||
102 | } | ||||
103 | } | ||||
104 | | ||||
105 | bool KWinWaylandBackend::applyConfig() | ||||
106 | { | ||||
107 | return std::all_of(m_devices.constBegin(), m_devices.constEnd(), | ||||
108 | [] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->applyConfig(); }); | ||||
109 | } | ||||
110 | | ||||
111 | bool KWinWaylandBackend::getConfig() | ||||
112 | { | ||||
113 | return std::all_of(m_devices.constBegin(), m_devices.constEnd(), | ||||
114 | [] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->getConfig(); }); | ||||
115 | } | ||||
116 | | ||||
117 | bool KWinWaylandBackend::getDefaultConfig() | ||||
118 | { | ||||
119 | return std::all_of(m_devices.constBegin(), m_devices.constEnd(), | ||||
120 | [] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->getDefaultConfig(); }); | ||||
121 | } | ||||
122 | | ||||
123 | bool KWinWaylandBackend::isChangedConfig() const | ||||
124 | { | ||||
125 | return std::any_of(m_devices.constBegin(), m_devices.constEnd(), | ||||
126 | [] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->isChangedConfig(); }); | ||||
127 | } | ||||
128 | | ||||
129 | void KWinWaylandBackend::onDeviceAdded(QString sysName) | ||||
130 | { | ||||
131 | if (std::any_of(m_devices.constBegin(), m_devices.constEnd(), | ||||
132 | [sysName] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->sysName() == sysName; })) { | ||||
133 | return; | ||||
134 | } | ||||
135 | | ||||
136 | QDBusInterface deviceIface(QStringLiteral("org.kde.KWin"), | ||||
137 | QStringLiteral("/org/kde/KWin/InputDevice/") + sysName, | ||||
138 | QStringLiteral("org.kde.KWin.InputDevice"), | ||||
139 | QDBusConnection::sessionBus(), | ||||
140 | this); | ||||
141 | QVariant reply = deviceIface.property("pointer"); | ||||
142 | | ||||
143 | if (reply.isValid() && reply.toBool()) { | ||||
144 | reply = deviceIface.property("touchpad"); | ||||
145 | if (reply.isValid() && reply.toBool()) { | ||||
146 | return; | ||||
147 | } | ||||
148 | | ||||
149 | KWinWaylandDevice* dev = new KWinWaylandDevice(sysName); | ||||
150 | if (!dev->init() || !dev->getConfig()) { | ||||
151 | emit deviceAdded(false); | ||||
152 | return; | ||||
153 | } | ||||
154 | | ||||
155 | m_devices.append(dev); | ||||
156 | qCDebug(KCM_INPUT).nospace() << "Device connected: " << dev->name() << " (" << dev->sysName() << ")"; | ||||
157 | emit deviceAdded(true); | ||||
158 | } | ||||
159 | } | ||||
160 | | ||||
161 | void KWinWaylandBackend::onDeviceRemoved(QString sysName) | ||||
162 | { | ||||
163 | QVector<QObject*>::const_iterator it = std::find_if(m_devices.constBegin(), m_devices.constEnd(), | ||||
164 | [sysName] (QObject *t) { return static_cast<KWinWaylandDevice*>(t)->sysName() == sysName; }); | ||||
165 | if (it == m_devices.cend()) { | ||||
166 | return; | ||||
167 | } | ||||
168 | | ||||
169 | KWinWaylandDevice *dev = static_cast<KWinWaylandDevice*>(*it); | ||||
170 | qCDebug(KCM_INPUT).nospace() << "Device disconnected: " << dev->name() << " (" << dev->sysName() << ")"; | ||||
171 | | ||||
172 | int index = it - m_devices.cbegin(); | ||||
173 | m_devices.removeAt(index); | ||||
174 | emit deviceRemoved(index); | ||||
175 | } |