File indexing completed on 2024-02-25 17:24:07

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "xdgactivationv1.h"
0011 #include "effects.h"
0012 #include "utils/common.h"
0013 #include "wayland/display.h"
0014 #include "wayland/plasmawindowmanagement_interface.h"
0015 #include "wayland/surface_interface.h"
0016 #include "wayland/xdgactivation_v1_interface.h"
0017 #include "wayland_server.h"
0018 #include "window.h"
0019 #include "workspace.h"
0020 #include <KApplicationTrader>
0021 #include <KDesktopFile>
0022 
0023 using namespace KWaylandServer;
0024 
0025 namespace KWin
0026 {
0027 
0028 static bool isPrivilegedInWindowManagement(const ClientConnection *client)
0029 {
0030     Q_ASSERT(client);
0031     auto requestedInterfaces = client->property("requestedInterfaces").toStringList();
0032     return requestedInterfaces.contains(QLatin1String("org_kde_plasma_window_management")) || requestedInterfaces.contains(QLatin1String("kde_lockscreen_overlay_v1"));
0033 }
0034 
0035 static const QString windowDesktopFileName(Window *window)
0036 {
0037     QString ret = window->desktopFileName();
0038     if (!ret.isEmpty()) {
0039         return ret;
0040     }
0041 
0042     // Fallback to StartupWMClass for legacy apps
0043     const auto resourceName = window->resourceName();
0044     const auto service = KApplicationTrader::query([&resourceName](const KService::Ptr &service) {
0045         return service->property("StartupWMClass").toString().compare(resourceName, Qt::CaseInsensitive) == 0;
0046     });
0047 
0048     if (!service.isEmpty()) {
0049         ret = service.constFirst()->desktopEntryName();
0050     }
0051     return ret;
0052 }
0053 
0054 XdgActivationV1Integration::XdgActivationV1Integration(XdgActivationV1Interface *activation, QObject *parent)
0055     : QObject(parent)
0056 {
0057     Workspace *ws = Workspace::self();
0058     connect(ws, &Workspace::windowActivated, this, [this](Window *window) {
0059         if (!m_currentActivationToken || !window || window->property("token").toString() == m_currentActivationToken->token) {
0060             return;
0061         }
0062 
0063         // We check that it's not the app that we are trying to activate
0064         if (windowDesktopFileName(window) != m_currentActivationToken->applicationId) {
0065             // But also that the new one has been requested after the token was requested
0066             if (window->lastUsageSerial() < m_currentActivationToken->serial) {
0067                 return;
0068             }
0069         }
0070 
0071         clear();
0072     });
0073     activation->setActivationTokenCreator([this](ClientConnection *client, SurfaceInterface *surface, uint serial, SeatInterface *seat, const QString &appId) -> QString {
0074         Workspace *ws = Workspace::self();
0075         Q_ASSERT(client); // Should always be available as it's coming straight from the wayland implementation
0076         const bool isPrivileged = isPrivilegedInWindowManagement(client);
0077         if (!isPrivileged && ws->activeWindow() && ws->activeWindow()->surface() != surface) {
0078             qCWarning(KWIN_CORE) << "Cannot grant a token to" << client;
0079             return QStringLiteral("not-granted-666");
0080         }
0081 
0082         return requestToken(isPrivileged, surface, serial, seat, appId);
0083     });
0084 
0085     connect(activation, &XdgActivationV1Interface::activateRequested, this, &XdgActivationV1Integration::activateSurface);
0086 }
0087 
0088 QString XdgActivationV1Integration::requestToken(bool isPrivileged, SurfaceInterface *surface, uint serial, SeatInterface *seat, const QString &appId)
0089 {
0090     static int i = 0;
0091     const auto newToken = QStringLiteral("kwin-%1").arg(++i);
0092 
0093     if (m_currentActivationToken) {
0094         clear();
0095     }
0096     bool showNotify = false;
0097     QIcon icon = QIcon::fromTheme(QStringLiteral("system-run"));
0098     if (const QString desktopFilePath = Window::findDesktopFile(appId); !desktopFilePath.isEmpty()) {
0099         KDesktopFile df(desktopFilePath);
0100         Window *window = Workspace::self()->activeWindow();
0101         if (!window || appId != window->desktopFileName()) {
0102             const auto desktop = df.desktopGroup();
0103             showNotify = desktop.readEntry("X-KDE-StartupNotify", desktop.readEntry("StartupNotify", true));
0104         }
0105         icon = QIcon::fromTheme(df.readIcon(), icon);
0106     }
0107     std::unique_ptr<KWaylandServer::PlasmaWindowActivationInterface> activation;
0108     if (showNotify) {
0109         activation = waylandServer()->plasmaActivationFeedback()->createActivation(appId);
0110     }
0111     m_currentActivationToken.reset(new ActivationToken{newToken, isPrivileged, surface, serial, seat, appId, showNotify, std::move(activation)});
0112     if (showNotify) {
0113         Q_EMIT effects->startupAdded(m_currentActivationToken->token, icon);
0114     }
0115     return newToken;
0116 }
0117 
0118 void XdgActivationV1Integration::activateSurface(SurfaceInterface *surface, const QString &token)
0119 {
0120     Workspace *ws = Workspace::self();
0121     auto window = waylandServer()->findWindow(surface);
0122     if (!window) {
0123         qCWarning(KWIN_CORE) << "could not find the toplevel to activate" << surface;
0124         return;
0125     }
0126 
0127     if (!m_currentActivationToken || m_currentActivationToken->token != token) {
0128         qCDebug(KWIN_CORE) << "Refusing to activate " << window << " (provided token: " << token << ", current token:" << (m_currentActivationToken ? m_currentActivationToken->token : QStringLiteral("null")) << ")";
0129         window->demandAttention();
0130         return;
0131     }
0132 
0133     auto ownerWindow = waylandServer()->findWindow(m_currentActivationToken->surface);
0134     qCDebug(KWIN_CORE) << "activating" << window << surface << "on behalf of" << m_currentActivationToken->surface << "into" << ownerWindow;
0135     if (!ws->activeWindow() || ws->activeWindow() == ownerWindow || ws->activeWindow()->lastUsageSerial() < m_currentActivationToken->serial || m_currentActivationToken->isPrivileged) {
0136         ws->activateWindow(window);
0137     } else {
0138         qCWarning(KWIN_CORE) << "Activation requested while owner isn't active" << (ownerWindow ? ownerWindow->desktopFileName() : "null")
0139                              << m_currentActivationToken->applicationId;
0140         window->demandAttention();
0141         clear();
0142     }
0143 }
0144 
0145 void XdgActivationV1Integration::clear()
0146 {
0147     Q_ASSERT(m_currentActivationToken);
0148     if (m_currentActivationToken->showNotify) {
0149         Q_EMIT effects->startupRemoved(m_currentActivationToken->token);
0150     }
0151     m_currentActivationToken.reset();
0152 }
0153 
0154 }