Changeset View
Changeset View
Standalone View
Standalone View
waylandserver.cpp
Show All 12 Lines | |||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | GNU General Public License for more details. | 15 | GNU General Public License for more details. | ||
16 | 16 | | |||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | #include "waylandserver.h" | 20 | #include "waylandserver.h" | ||
21 | #include "powermanagement.h" | ||||
21 | // ksld | 22 | // ksld | ||
22 | #include <config-kscreenlocker.h> | 23 | #include <config-kscreenlocker.h> | ||
23 | // Wayland | 24 | // Wayland | ||
24 | #include <wayland-server.h> | 25 | #include <wayland-server.h> | ||
25 | #include <wayland-ksld-server-protocol.h> | 26 | #include <wayland-ksld-server-protocol.h> | ||
26 | // KWayland | 27 | // KWayland | ||
27 | #include <KWayland/Server/display.h> | 28 | #include <KWayland/Server/display.h> | ||
28 | // Qt | 29 | // Qt | ||
Show All 20 Lines | 48 | QDBusConnection::sessionBus().connect(s_plasmaShellService, | |||
49 | s_osdServiceInterface, | 50 | s_osdServiceInterface, | ||
50 | QStringLiteral("osdProgress"), | 51 | QStringLiteral("osdProgress"), | ||
51 | this, SLOT(osdProgress(QString,int,QString))); | 52 | this, SLOT(osdProgress(QString,int,QString))); | ||
52 | QDBusConnection::sessionBus().connect(s_plasmaShellService, | 53 | QDBusConnection::sessionBus().connect(s_plasmaShellService, | ||
53 | s_osdServicePath, | 54 | s_osdServicePath, | ||
54 | s_osdServiceInterface, | 55 | s_osdServiceInterface, | ||
55 | QStringLiteral("osdText"), | 56 | QStringLiteral("osdText"), | ||
56 | this, SLOT(osdText(QString,QString))); | 57 | this, SLOT(osdText(QString,QString))); | ||
58 | connect(PowerManagement::instance(), &PowerManagement::canSuspendChanged, this, &WaylandServer::sendCanSuspend); | ||||
59 | connect(PowerManagement::instance(), &PowerManagement::canHibernateChanged, this, &WaylandServer::sendCanHibernate); | ||||
57 | } | 60 | } | ||
58 | 61 | | |||
59 | WaylandServer::~WaylandServer() | 62 | WaylandServer::~WaylandServer() | ||
60 | { | 63 | { | ||
61 | stop(); | 64 | stop(); | ||
62 | } | 65 | } | ||
63 | 66 | | |||
64 | int WaylandServer::start() | 67 | int WaylandServer::start() | ||
Show All 15 Lines | 68 | { | |||
80 | if (!m_allowedClient) { | 83 | if (!m_allowedClient) { | ||
81 | // failed creating the Wayland client | 84 | // failed creating the Wayland client | ||
82 | stop(); | 85 | stop(); | ||
83 | close(socketPair[0]); | 86 | close(socketPair[0]); | ||
84 | close(socketPair[1]); | 87 | close(socketPair[1]); | ||
85 | return -1; | 88 | return -1; | ||
86 | } | 89 | } | ||
87 | connect(m_allowedClient, &KWayland::Server::ClientConnection::disconnected, this, [this] { m_allowedClient = nullptr; }); | 90 | connect(m_allowedClient, &KWayland::Server::ClientConnection::disconnected, this, [this] { m_allowedClient = nullptr; }); | ||
88 | m_interface = wl_global_create(*m_display.data(), &org_kde_ksld_interface, 2, this, bind); | 91 | m_interface = wl_global_create(*m_display.data(), &org_kde_ksld_interface, 3, this, bind); | ||
89 | return socketPair[1]; | 92 | return socketPair[1]; | ||
90 | } | 93 | } | ||
91 | 94 | | |||
92 | void WaylandServer::stop() | 95 | void WaylandServer::stop() | ||
93 | { | 96 | { | ||
94 | if (m_allowedClient) { | 97 | if (m_allowedClient) { | ||
95 | m_allowedClient->destroy(); | 98 | m_allowedClient->destroy(); | ||
96 | } | 99 | } | ||
97 | if (m_interface) { | 100 | if (m_interface) { | ||
98 | wl_global_destroy(m_interface); | 101 | wl_global_destroy(m_interface); | ||
99 | m_interface = nullptr; | 102 | m_interface = nullptr; | ||
100 | } | 103 | } | ||
101 | m_display.reset(); | 104 | m_display.reset(); | ||
102 | m_allowedClient = nullptr; | 105 | m_allowedClient = nullptr; | ||
103 | } | 106 | } | ||
104 | 107 | | |||
105 | void WaylandServer::bind(wl_client *client, void *data, uint32_t version, uint32_t id) | 108 | void WaylandServer::bind(wl_client *client, void *data, uint32_t version, uint32_t id) | ||
106 | { | 109 | { | ||
107 | auto s = reinterpret_cast<WaylandServer*>(data); | 110 | auto s = reinterpret_cast<WaylandServer*>(data); | ||
108 | if (client != s->m_allowedClient->client()) { | 111 | if (client != s->m_allowedClient->client()) { | ||
109 | // a proper error would be better | 112 | // a proper error would be better | ||
110 | wl_client_post_no_memory(client); | 113 | wl_client_post_no_memory(client); | ||
111 | return; | 114 | return; | ||
112 | } | 115 | } | ||
113 | wl_resource *r = s->m_allowedClient->createResource(&org_kde_ksld_interface, qMin(version, 2u), id); | 116 | wl_resource *r = s->m_allowedClient->createResource(&org_kde_ksld_interface, qMin(version, 3u), id); | ||
114 | if (!r) { | 117 | if (!r) { | ||
115 | wl_client_post_no_memory(client); | 118 | wl_client_post_no_memory(client); | ||
116 | return; | 119 | return; | ||
117 | } | 120 | } | ||
118 | 121 | | |||
119 | static const struct org_kde_ksld_interface s_interface = { | 122 | static const struct org_kde_ksld_interface s_interface = { | ||
120 | x11WindowCallback | 123 | x11WindowCallback, | ||
124 | suspendSystemCallback, | ||||
125 | hibernateSystemCallback | ||||
121 | }; | 126 | }; | ||
122 | wl_resource_set_implementation(r, &s_interface, s, unbind); | 127 | wl_resource_set_implementation(r, &s_interface, s, unbind); | ||
123 | s->addResource(r); | 128 | s->addResource(r); | ||
129 | s->sendCanSuspend(); | ||||
130 | s->sendCanHibernate(); | ||||
124 | s->m_allowedClient->flush(); | 131 | s->m_allowedClient->flush(); | ||
125 | } | 132 | } | ||
126 | 133 | | |||
127 | void WaylandServer::unbind(wl_resource *resource) | 134 | void WaylandServer::unbind(wl_resource *resource) | ||
128 | { | 135 | { | ||
129 | reinterpret_cast<WaylandServer*>(wl_resource_get_user_data(resource))->removeResource(resource); | 136 | reinterpret_cast<WaylandServer*>(wl_resource_get_user_data(resource))->removeResource(resource); | ||
130 | } | 137 | } | ||
131 | 138 | | |||
132 | void WaylandServer::x11WindowCallback(wl_client *client, wl_resource *resource, uint32_t id) | 139 | void WaylandServer::x11WindowCallback(wl_client *client, wl_resource *resource, uint32_t id) | ||
133 | { | 140 | { | ||
134 | auto s = reinterpret_cast<WaylandServer*>(wl_resource_get_user_data(resource)); | 141 | auto s = reinterpret_cast<WaylandServer*>(wl_resource_get_user_data(resource)); | ||
135 | if (s->m_allowedClient->client() != client) { | 142 | if (s->m_allowedClient->client() != client) { | ||
136 | return; | 143 | return; | ||
137 | } | 144 | } | ||
138 | emit s->x11WindowAdded(id); | 145 | emit s->x11WindowAdded(id); | ||
139 | } | 146 | } | ||
140 | 147 | | |||
148 | void WaylandServer::suspendSystemCallback(wl_client *client, wl_resource *resource) | ||||
149 | { | ||||
150 | Q_UNUSED(client) | ||||
151 | Q_UNUSED(resource) | ||||
152 | PowerManagement::instance()->suspend(); | ||||
153 | } | ||||
154 | | ||||
155 | void WaylandServer::hibernateSystemCallback(wl_client *client, wl_resource *resource) | ||||
156 | { | ||||
157 | Q_UNUSED(client) | ||||
158 | Q_UNUSED(resource) | ||||
159 | PowerManagement::instance()->hibernate(); | ||||
160 | } | ||||
161 | | ||||
141 | void WaylandServer::addResource(wl_resource *r) | 162 | void WaylandServer::addResource(wl_resource *r) | ||
142 | { | 163 | { | ||
143 | m_resources.append(r); | 164 | m_resources.append(r); | ||
144 | } | 165 | } | ||
145 | 166 | | |||
146 | void WaylandServer::removeResource(wl_resource *r) | 167 | void WaylandServer::removeResource(wl_resource *r) | ||
147 | { | 168 | { | ||
148 | m_resources.removeAll(r); | 169 | m_resources.removeAll(r); | ||
Show All 22 Lines | 191 | for (auto r : m_resources) { | |||
171 | if (wl_resource_get_version(r) < 2) { | 192 | if (wl_resource_get_version(r) < 2) { | ||
172 | continue; | 193 | continue; | ||
173 | } | 194 | } | ||
174 | org_kde_ksld_send_osdText(r, icon.toUtf8().constData(), additionalText.toUtf8().constData()); | 195 | org_kde_ksld_send_osdText(r, icon.toUtf8().constData(), additionalText.toUtf8().constData()); | ||
175 | m_allowedClient->flush(); | 196 | m_allowedClient->flush(); | ||
176 | } | 197 | } | ||
177 | } | 198 | } | ||
178 | 199 | | |||
200 | void WaylandServer::sendCanSuspend() | ||||
201 | { | ||||
202 | if (!m_allowedClient) { | ||||
203 | return; | ||||
204 | } | ||||
205 | Q_FOREACH (auto r, m_resources) { | ||||
broulik: Be careful with range-for and Qt containers, this detaches. | |||||
graesslin: You're right - that's what happens if one blindly copy'n'pastes. | |||||
206 | if (wl_resource_get_version(r) < ORG_KDE_KSLD_CANSUSPENDSYSTEM_SINCE_VERSION) { | ||||
207 | continue; | ||||
208 | } | ||||
209 | org_kde_ksld_send_canSuspendSystem(r, PowerManagement::instance()->canSuspend() ? 1 : 0); | ||||
210 | } | ||||
211 | m_allowedClient->flush(); | ||||
212 | } | ||||
213 | | ||||
214 | void WaylandServer::sendCanHibernate() | ||||
215 | { | ||||
216 | if (!m_allowedClient) { | ||||
217 | return; | ||||
218 | } | ||||
219 | Q_FOREACH (auto r, m_resources) { | ||||
broulik: Same here. | |||||
220 | if (wl_resource_get_version(r) < ORG_KDE_KSLD_CANHIBERNATESYSTEM_SINCE_VERSION) { | ||||
221 | continue; | ||||
222 | } | ||||
223 | org_kde_ksld_send_canHibernateSystem(r, PowerManagement::instance()->canHibernate() ? 1 : 0); | ||||
224 | } | ||||
225 | m_allowedClient->flush(); | ||||
226 | } | ||||
227 | | ||||
179 | } | 228 | } |
Be careful with range-for and Qt containers, this detaches.