KWindowSystem

windowsystem.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
3 SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7#include "windowsystem.h"
8#include "logging.h"
9#include "surfacehelper.h"
10#include "waylandxdgactivationv1_p.h"
11#include "waylandxdgforeignv2_p.h"
12
13#include <KWaylandExtras>
14#include <KWindowSystem>
15
16#include "qwayland-plasma-window-management.h"
17#include <QEvent>
18#include <QGuiApplication>
19#include <QPixmap>
20#include <QPoint>
21#include <QString>
22#include <QTimer>
23#include <QWaylandClientExtensionTemplate>
24#include <QWindow>
25#include <qpa/qplatformnativeinterface.h>
26#include <qpa/qplatformwindow_p.h>
27
28constexpr const char *c_kdeXdgForeignExportedProperty("_kde_xdg_foreign_exported_v2");
29constexpr const char *c_kdeXdgForeignImportedProperty("_kde_xdg_foreign_imported_v2");
30constexpr const char *c_kdeXdgForeignPendingHandleProperty("_kde_xdg_foreign_pending_handle");
31
32class WindowManagement : public QWaylandClientExtensionTemplate<WindowManagement>, public QtWayland::org_kde_plasma_window_management
33{
34public:
35 WindowManagement()
36 : QWaylandClientExtensionTemplate<WindowManagement>(16)
37 {
38 }
39
40 void org_kde_plasma_window_management_show_desktop_changed(uint32_t state) override
41 {
42 showingDesktop = state == show_desktop_enabled;
44 }
45
46 bool showingDesktop = false;
47};
48
49WindowSystem::WindowSystem()
50 : QObject()
51 , KWindowSystemPrivateV2()
52 , m_lastToken(qEnvironmentVariable("XDG_ACTIVATION_TOKEN"))
53{
54 m_windowManagement = new WindowManagement;
55}
56
57WindowSystem::~WindowSystem()
58{
59 delete m_windowManagement;
60}
61
62void WindowSystem::activateWindow(QWindow *win, long int time)
63{
64 Q_UNUSED(time);
65 auto s = surfaceForWindow(win);
66 if (!s) {
67 return;
68 }
69 WaylandXdgActivationV1 *activation = WaylandXdgActivationV1::self();
70 if (!activation->isActive()) {
71 return;
72 }
73 activation->activate(m_lastToken, s);
74}
75
76void WindowSystem::requestToken(QWindow *window, uint32_t serial, const QString &app_id)
77{
78 if (window) {
79 window->create();
80 }
81 wl_surface *wlSurface = surfaceForWindow(window);
82
83 WaylandXdgActivationV1 *activation = WaylandXdgActivationV1::self();
84 if (!activation->isActive()) {
85 // Ensure that xdgActivationTokenArrived is always emitted asynchronously
86 QTimer::singleShot(0, [serial] {
87 Q_EMIT KWaylandExtras::self()->xdgActivationTokenArrived(serial, {});
88 });
89 return;
90 }
91
92 auto waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>();
93 auto seat = waylandApp ? waylandApp->lastInputSeat() : nullptr;
94 auto tokenReq = activation->requestXdgActivationToken(seat, wlSurface, serial, app_id);
95 connect(tokenReq, &WaylandXdgActivationTokenV1::failed, KWindowSystem::self(), [serial, app_id]() {
96 Q_EMIT KWaylandExtras::self()->xdgActivationTokenArrived(serial, {});
97 });
98 connect(tokenReq, &WaylandXdgActivationTokenV1::done, KWindowSystem::self(), [serial](const QString &token) {
99 Q_EMIT KWaylandExtras::self()->xdgActivationTokenArrived(serial, token);
100 });
101}
102
103void WindowSystem::setCurrentToken(const QString &token)
104{
105 m_lastToken = token;
106}
107
108quint32 WindowSystem::lastInputSerial(QWindow *window)
109{
110 Q_UNUSED(window)
111 if (auto waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>()) {
112 return waylandApp->lastInputSerial();
113 }
114 return 0;
115}
116
117void WindowSystem::setShowingDesktop(bool showing)
118{
119 if (!m_windowManagement->isActive()) {
120 return;
121 }
122 m_windowManagement->show_desktop(showing ? WindowManagement::show_desktop_enabled : WindowManagement::show_desktop_disabled);
123}
124
125bool WindowSystem::showingDesktop()
126{
127 if (!m_windowManagement->isActive()) {
128 return false;
129 }
130 return m_windowManagement->showingDesktop;
131}
132
133void WindowSystem::exportWindow(QWindow *window)
134{
135 auto emitHandle = [window](const QString &handle) {
136 // Ensure that windowExported is always emitted asynchronously.
138 window,
139 [window, handle] {
140 Q_EMIT KWaylandExtras::self()->windowExported(window, handle);
141 },
143 };
144
145 if (!window) {
146 return;
147 }
148
149 window->create();
150
151 auto waylandWindow = window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
152 if (!waylandWindow) {
153 emitHandle({});
154 return;
155 }
156
157 auto &exporter = WaylandXdgForeignExporterV2::self();
158 if (!exporter.isActive()) {
159 emitHandle({});
160 return;
161 }
162
163 // We want to use QObject::property(char*) and use dynamic properties on the object rather than
164 // call QWaylandWindow::property(QString) and send it around.
165 WaylandXdgForeignExportedV2 *exported = waylandWindow->property(c_kdeXdgForeignExportedProperty).value<WaylandXdgForeignExportedV2 *>();
166 if (!exported) {
167 exported = exporter.exportToplevel(surfaceForWindow(window));
168 exported->setParent(waylandWindow);
169
170 waylandWindow->setProperty(c_kdeXdgForeignExportedProperty, QVariant::fromValue(exported));
171 connect(exported, &QObject::destroyed, waylandWindow, [waylandWindow] {
172 waylandWindow->setProperty(c_kdeXdgForeignExportedProperty, QVariant());
173 });
174
175 connect(exported, &WaylandXdgForeignExportedV2::handleReceived, window, [window](const QString &handle) {
176 Q_EMIT KWaylandExtras::self()->windowExported(window, handle);
177 });
178 }
179
180 if (!exported->handle().isEmpty()) {
181 emitHandle(exported->handle());
182 }
183}
184
185void WindowSystem::unexportWindow(QWindow *window)
186{
187 auto waylandWindow = window ? window->nativeInterface<QNativeInterface::Private::QWaylandWindow>() : nullptr;
188 if (!waylandWindow) {
189 return;
190 }
191
192 WaylandXdgForeignExportedV2 *exported = waylandWindow->property(c_kdeXdgForeignExportedProperty).value<WaylandXdgForeignExportedV2 *>();
193 delete exported;
194 Q_ASSERT(!waylandWindow->property(c_kdeXdgForeignExportedProperty).isValid());
195}
196
197void WindowSystem::setMainWindow(QWindow *window, const QString &handle)
198{
199 if (!window) {
200 return;
201 }
202
203 window->create();
204 auto waylandWindow = window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
205 if (!waylandWindow) {
206 return;
207 }
208
209 // We want to use QObject::property(char*) and use dynamic properties on the object rather than
210 // call QWaylandWindow::property(QString) and send it around.
211 auto *imported = waylandWindow->property(c_kdeXdgForeignImportedProperty).value<WaylandXdgForeignImportedV2 *>();
212 // Window already parented with a different handle? Delete imported so we import the new one later.
213 if (imported && imported->handle() != handle) {
214 delete imported;
215 imported = nullptr;
216 Q_ASSERT(!waylandWindow->property(c_kdeXdgForeignImportedProperty).isValid());
217 }
218
219 // Don't bother.
220 if (handle.isEmpty()) {
221 return;
222 }
223
224 if (window->isExposed()) {
225 doSetMainWindow(window, handle);
226 } else {
227 // We can only import an XDG toplevel. QtWayland currently has no proper signal
228 // for shell surface creation. wlSurfaceCreated() is too early.
229 // Instead, we wait for it being exposed and then set its parent.
230 window->setProperty(c_kdeXdgForeignPendingHandleProperty, handle);
232 }
233}
234
235bool WindowSystem::eventFilter(QObject *watched, QEvent *event)
236{
237 if (event->type() == QEvent::Expose) {
238 auto *window = static_cast<QWindow *>(watched);
239 if (window->isExposed()) {
240 const QString handle = window->property(c_kdeXdgForeignPendingHandleProperty).toString();
241 if (!handle.isEmpty()) {
242 doSetMainWindow(window, handle);
243 window->setProperty(c_kdeXdgForeignPendingHandleProperty, QVariant());
244 }
245
247 }
248 }
249
250 return QObject::eventFilter(watched, event);
251}
252
253void WindowSystem::doSetMainWindow(QWindow *window, const QString &handle)
254{
255 Q_ASSERT(window);
256 Q_ASSERT(!handle.isEmpty());
257
258 auto waylandWindow = window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
259 if (!waylandWindow) {
260 return;
261 }
262
263 auto &importer = WaylandXdgForeignImporterV2::self();
264 if (!importer.isActive()) {
265 return;
266 }
267
268 Q_ASSERT(!waylandWindow->property(c_kdeXdgForeignImportedProperty).isValid());
269
270 WaylandXdgForeignImportedV2 *imported = importer.importToplevel(handle);
271 imported->set_parent_of(surfaceForWindow(window)); // foreign parent.
272 imported->setParent(waylandWindow); // memory owner.
273
274 waylandWindow->setProperty(c_kdeXdgForeignImportedProperty, QVariant::fromValue(imported));
275 connect(imported, &QObject::destroyed, waylandWindow, [waylandWindow] {
276 waylandWindow->setProperty(c_kdeXdgForeignImportedProperty, QVariant());
277 });
278}
279
280#include "moc_windowsystem.cpp"
void xdgActivationTokenArrived(int serial, const QString &token)
Activation token to pass to the client.
void windowExported(QWindow *window, const QString &handle)
Window handle to pass to the client.
void showingDesktopChanged(bool showing)
The state of showing the desktop has changed.
static KWindowSystem * self()
Access to the singleton instance.
QWidget * window(QObject *job)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
virtual wl_seat * lastInputSeat() const const=0
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
void removeEventFilter(QObject *obj)
bool setProperty(const char *name, QVariant &&value)
bool isEmpty() const const
QueuedConnection
QVariant fromValue(T &&value)
QString toString() const const
T value() const const
void create(WId window, bool initializeWindow, bool destroyOldWindow)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:45:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.