KWin
Loading...
Searching...
No Matches
serviceutils.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2020 Méven Car <meven.car@enioka.com>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#pragma once
11
12// cmake stuff
13#include <config-kwin.h>
14// kwin
15#include "effect/globals.h"
16// Qt
17#include <QFileInfo>
18#include <QLoggingCategory>
19#include <QProcess>
20// KF
21#include <KApplicationTrader>
22
23namespace KWin
24{
25
26const static QString s_waylandInterfaceName = QStringLiteral("X-KDE-Wayland-Interfaces");
27const static QString s_dbusRestrictedInterfaceName = QStringLiteral("X-KDE-DBUS-Restricted-Interfaces");
28
29static QStringList fetchProcessServiceField(const QString &executablePath, const QString &fieldName)
30{
31 // needed to be able to use the logging category in a header static function
32 static QLoggingCategory KWIN_UTILS("KWIN_UTILS", QtWarningMsg);
33 const auto servicesFound = KApplicationTrader::query([&executablePath](const KService::Ptr &service) {
34 const auto splitCommandList = QProcess::splitCommand(service->exec());
35 if (splitCommandList.isEmpty()) {
36 return false;
37 }
38 return QFileInfo(splitCommandList.first()).canonicalFilePath() == executablePath;
39 });
40
41 if (servicesFound.isEmpty()) {
42 qCDebug(KWIN_UTILS) << "Could not find the desktop file for" << executablePath;
43 return {};
44 }
45
46 const auto fieldValues = servicesFound.first()->property<QStringList>(fieldName);
47 if (KWIN_UTILS().isDebugEnabled()) {
48 qCDebug(KWIN_UTILS) << "Interfaces found for" << executablePath << fieldName << ":" << fieldValues;
49 }
50 return fieldValues;
51}
52
53static inline QStringList fetchRequestedInterfacesForDesktopId(const QString &desktopId)
54{
55 const auto service = KService::serviceByDesktopName(desktopId);
56 if (!service) {
57 return {};
58 }
59 return service->property<QStringList>(s_waylandInterfaceName);
60}
61
62static inline QStringList fetchRequestedInterfaces(const QString &executablePath)
63{
64 return fetchProcessServiceField(executablePath, s_waylandInterfaceName);
65}
66
67static inline QStringList fetchRestrictedDBusInterfacesFromPid(const uint pid)
68{
69 const auto executablePath = QFileInfo(QStringLiteral("/proc/%1/exe").arg(pid)).symLinkTarget();
70 return fetchProcessServiceField(executablePath, s_dbusRestrictedInterfaceName);
71}
72
73} // namespace