File indexing completed on 2024-12-29 05:06:05
0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #include "windowlistener.h" 0005 0006 WindowListener::WindowListener(QObject *parent) 0007 : QObject{parent} 0008 { 0009 // initialize wayland window checking 0010 KWayland::Client::ConnectionThread *connection = KWayland::Client::ConnectionThread::fromApplication(this); 0011 if (!connection) { 0012 return; 0013 } 0014 0015 auto *registry = new KWayland::Client::Registry(this); 0016 registry->create(connection); 0017 0018 connect(registry, &KWayland::Client::Registry::plasmaWindowManagementAnnounced, this, [this, registry](quint32 name, quint32 version) { 0019 m_windowManagement = registry->createPlasmaWindowManagement(name, version, this); 0020 connect(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated, this, &WindowListener::windowCreated); 0021 }); 0022 0023 registry->setup(); 0024 connection->roundtrip(); 0025 } 0026 0027 WindowListener *WindowListener::instance() 0028 { 0029 static WindowListener *listener = new WindowListener(); 0030 return listener; 0031 } 0032 0033 QList<KWayland::Client::PlasmaWindow *> WindowListener::windowsFromStorageId(QString &storageId) const 0034 { 0035 if (!m_windows.contains(storageId)) { 0036 return {}; 0037 } 0038 return m_windows[storageId]; 0039 } 0040 0041 void WindowListener::windowCreated(KWayland::Client::PlasmaWindow *window) 0042 { 0043 QString storageId = window->appId() + QStringLiteral(".desktop"); 0044 0045 // ignore empty windows 0046 if (storageId == ".desktop" || storageId == "org.kde.plasmashell.desktop") { 0047 return; 0048 } 0049 0050 if (!m_windows.contains(storageId)) { 0051 m_windows[storageId] = {}; 0052 } 0053 m_windows[storageId].push_back(window); 0054 0055 // listen for window close 0056 connect(window, &KWayland::Client::PlasmaWindow::unmapped, this, [this, storageId]() { 0057 m_windows.remove(storageId); 0058 Q_EMIT windowChanged(storageId); 0059 }); 0060 0061 Q_EMIT windowChanged(storageId); 0062 }