Commit a87f619a authored by Eike Hein's avatar Eike Hein

Lift app identification heuristic out of XWindowTasksModel and share it with WaylandTasksModel.

This factors the app identification heuristic out of XWindowTasksModel
and turns it into generic code in TaskTools, producing a URL from a set
of window metadata bits. The key metadata is the 'appId', which is the
classClass part of WM_CLASS on X11 and PlasmaWindow::appId on Wayland -
which KWin sets to the former for XWayland clients. The result is much
improved support for XWayland clients in the Wayland session, with most
X clients now identified correctly.

As a side effect, the Wayland model gains access to the X model's much
superior code for grabbing a suitable icon, with PlasmaWindow::icon now
serving only as a fallback, similar to KWindowSystem::icon in the X

Moving the code to TaskTools also means it now sports nice API docs.

The heuristic has seen some work as well, namely adding two passes that
try to parse the appId as a path to a desktop file, which we've never
seen on X11 but is common on Wayland - this heuristic was previously in
appDataFromAppId, which has been removed here since the shared heuristic
now satisfies this case.

Further, an old codepath handling kcmshell in a special way has been
removed. This is no longer necessary as we have better ways to tell
libtaskmanager about the KCM KService now, such as the .desktop file
window hint on X11 and a reliable appId on Wayland.

This patch also fixes some bugs around app data cache eviction and
telling model clients about data changes when cache eviction happens.
The X model didn't use to evict the cache and refresh when the
taskmanagerrulesrc file was changed at runtime, and the refresh for
sycoca changes didn't refresh the LauncherUrlWithoutIcon role. It does
now, and the Wayland model - which has gained taskmanagerrulesrc support
by way of the shared heuristic - now behaves in the same way.

The combined changes achieve near behavior parity between the X
and Wayland models when it comes to identifying apps by window meta
data, with the only exception being XWayland clients that need to be
identified by the (incorrectly used by the client developer) instance
name part of the WM_CLASS window property, which we can't access (this
case is so rare it's not worth handling at this time).

Depends on D5747, D5755.

Reviewers: #plasma, davidedmundson

Subscribers: plasma-devel

Tags: #plasma

Differential Revision:
parent 140c65f2
This diff is collapsed.
......@@ -27,6 +27,9 @@ License along with this library. If not, see <>.
#include <QModelIndex>
#include <QUrl>
#include <KService>
#include <KSharedConfig>
namespace TaskManager
......@@ -65,20 +68,71 @@ enum UrlComparisonMode {
TASKMANAGER_EXPORT AppData appDataFromUrl(const QUrl &url, const QIcon &fallbackIcon = QIcon());
* Fills in and returns an AppData struct based on the given application
* id.
* Takes several bits of window metadata as input and tries to find
* the .desktop file for the application owning this window, or,
* failing that, the path to its executable.
* The source for the metadata is generally the the window's appId on
* Wayland, or the window class part of the WM_CLASS window property
* on X Windows.
* TODO: The supplied config object can contain various mapping and
* mangling rules that affect the behavior of this function, allowing
* to map bits of metadata to different values and other things. This
* config file format still needs to be documented fully; in the
* meantime the bundled default rules in taskmanagerrulesrc (the
* config file opened by various models in this library) can be used
* for reference.
* @param appId A string uniquely identifying the application owning
* the window, ideally matching a .desktop file name.
* @param pid The process id for the process owning the window.
* @param rulesConfig A KConfig object parameterizing the matching
* behavior.
* @param xWindowsWMClassName The instance name part of X Windows'
* WM_CLASS window property.
* @returns A .desktop file or executable path for the application
* owning the window.
TASKMANAGER_EXPORT QUrl windowUrlFromMetadata(const QString &appId, quint32 pid = 0,
KSharedConfig::Ptr config = KSharedConfig::Ptr(), const QString &xWindowsWMClassName = QString());
* Returns a list of (usually application) KService instances for the
* given process id, by examining the process and querying the service
* database for process metadata.
* Application ids are .desktop file names sans extension or an absolute
* path to a .desktop file.
* @param pid A process id.
* @param rulesConfig A KConfig object parameterizing the matching
* behavior.
* @returns A list of KService instances.
TASKMANAGER_EXPORT KService::List servicesFromPid(quint32 pid,
KSharedConfig::Ptr rulesConfig = KSharedConfig::Ptr());
* Returns a list of (usually application) KService instances for the
* given process command line and process name, by mangling the command
* line in various ways and checking the data against the Exec keys in
* the service database. Mangling is done e.g. to check for executable
* names with and without paths leading to them and to ignore arguments.
* if needed.
* NOTE: Unlike appDataFromUrl(), this makes no attempt to procure icon
* data at this time.
* The [Settings]TryIgnoreRuntimes key in the supplied config object can
* hold a comma-separated list of runtime executables that this code will
* try to ignore in the process command line. This is useful in cases where
* the command line has the contents of a .desktop Exec key prefixed with
* a runtime executable. The code tries to strip the path to the runtime
* executable if needed.
* @see appDataFromUrl
* @param appId An application id.
* @returns @c AppData filled in based on the given application id.
* @param cmdLine A process command line.
* @param processName The process name.
* @param rulesConfig A KConfig object parameterizing the matching
* behavior.
* @returns A list of KService instances.
TASKMANAGER_EXPORT AppData appDataFromAppId(const QString &appId);
TASKMANAGER_EXPORT KService::List servicesFromCmdLine(const QString &cmdLine, const QString &processName,
KSharedConfig::Ptr rulesConfig = KSharedConfig::Ptr());
* Returns an application id for an URL using the preferred:// scheme.
......@@ -22,8 +22,10 @@ License along with this library. If not, see <>.
#include "tasktools.h"
#include <KActivities/ResourceInstance>
#include <KDirWatch>
#include <KRun>
#include <KService>
#include <KSharedConfig>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
......@@ -47,12 +49,17 @@ public:
QList<KWayland::Client::PlasmaWindow*> windows;
QHash<KWayland::Client::PlasmaWindow*, AppData> appDataCache;
KWayland::Client::PlasmaWindowManagement *windowManagement = nullptr;
KSharedConfig::Ptr rulesConfig;
KDirWatch *configWatcher = nullptr;
void init();
void initWayland();
void addWindow(KWayland::Client::PlasmaWindow *window);
AppData appData(KWayland::Client::PlasmaWindow *window);
QIcon icon(KWayland::Client::PlasmaWindow *window);
void dataChanged(KWayland::Client::PlasmaWindow *window, int role);
void dataChanged(KWayland::Client::PlasmaWindow *window, const QVector<int> &roles);
......@@ -65,6 +72,42 @@ WaylandTasksModel::Private::Private(WaylandTasksModel *q)
void WaylandTasksModel::Private::init()
auto clearCacheAndRefresh = [this] {
if (!windows.count()) {
// Emit changes of all roles satisfied from app data cache.
q->dataChanged(q->index(0, 0), q->index(windows.count() - 1, 0),
QVector<int>{Qt::DecorationRole, AbstractTasksModel::AppId,
AbstractTasksModel::AppName, AbstractTasksModel::GenericName,
rulesConfig = KSharedConfig::openConfig(QStringLiteral("taskmanagerrulesrc"));
configWatcher = new KDirWatch(q);
foreach (const QString &location, QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)) {
configWatcher->addFile(location + QLatin1String("/taskmanagerrulesrc"));
auto rulesConfigChange = [this, &clearCacheAndRefresh] {
QObject::connect(configWatcher, &KDirWatch::dirty, rulesConfigChange);
QObject::connect(configWatcher, &KDirWatch::created, rulesConfigChange);
QObject::connect(configWatcher, &KDirWatch::deleted, rulesConfigChange);
void WaylandTasksModel::Private::initWayland()
if (!KWindowSystem::isPlatformWayland()) {
......@@ -135,18 +178,31 @@ void WaylandTasksModel::Private::addWindow(KWayland::Client::PlasmaWindow *windo
QObject::connect(window, &QObject::destroyed, q, removeWindow);
QObject::connect(window, &KWayland::Client::PlasmaWindow::titleChanged, q,
[window, this] { dataChanged(window, Qt::DisplayRole); }
[window, this] { this->dataChanged(window, Qt::DisplayRole); }
QObject::connect(window, &KWayland::Client::PlasmaWindow::iconChanged, q,
[window, this] { dataChanged(window, Qt::DecorationRole); }
[window, this] {
// The icon in the AppData struct might come from PlasmaWindow if it wasn't
// filled in by windowUrlFromMetadata+appDataFromUrl.
// TODO: Don't evict the cache unnecessarily if this isn't the case. As icons
// are currently very static on Wayland, this eviction is unlikely to happen
// frequently as of now.
this->dataChanged(window, Qt::DecorationRole);
QObject::connect(window, &KWayland::Client::PlasmaWindow::appIdChanged, q,
[window, this] {
// The AppData struct in the cache is derived from this and needs
// to be evicted in favor of a fresh struct based on the changed
// window metadata.
dataChanged(window, QVector<int>{AppId, AppName, GenericName,
// Refresh roles satisfied from the app data cache.
this->dataChanged(window, QVector<int>{AppId, AppName, GenericName,
LauncherUrl, LauncherUrlWithoutIcon});
......@@ -227,8 +283,18 @@ void WaylandTasksModel::Private::addWindow(KWayland::Client::PlasmaWindow *windo
[window, this] { this->dataChanged(window, SkipTaskbar); }
// NOTE: The pid will never actually change on a real system. But if it ever did ...
QObject::connect(window, &KWayland::Client::PlasmaWindow::pidChanged, q,
[window, this] { this->dataChanged(window, AppPid); }
[window, this] {
// The AppData struct in the cache is derived from this and needs
// to be evicted in favor of a fresh struct based on the changed
// window metadata.
// Refresh roles satisfied from the app data cache.
this->dataChanged(window, QVector<int>{AppId, AppName, GenericName,
LauncherUrl, LauncherUrlWithoutIcon});
......@@ -240,13 +306,27 @@ AppData WaylandTasksModel::Private::appData(KWayland::Client::PlasmaWindow *wind
return *it;
const AppData &data = appDataFromAppId(window->appId());
const AppData &data = appDataFromUrl(windowUrlFromMetadata(window->appId(),
window->pid(), rulesConfig));
appDataCache.insert(window, data);
return data;
QIcon WaylandTasksModel::Private::icon(KWayland::Client::PlasmaWindow *window)
const AppData &app = appData(window);
if (!app.icon.isNull()) {
return app.icon;
appDataCache[window].icon = window->icon();
return window->icon();
void WaylandTasksModel::Private::dataChanged(KWayland::Client::PlasmaWindow *window, int role)
QModelIndex idx = q->index(windows.indexOf(window));
......@@ -263,7 +343,7 @@ WaylandTasksModel::WaylandTasksModel(QObject *parent)
: AbstractWindowTasksModel(parent)
, d(new Private(this))
WaylandTasksModel::~WaylandTasksModel() = default;
......@@ -279,9 +359,15 @@ QVariant WaylandTasksModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) {
return window->title();
} else if (role == Qt::DecorationRole) {
return window->icon();
return d->icon(window);
} else if (role == AppId) {
return window->appId();
const QString &id = d->appData(window).id;
if (id.isEmpty()) {
return window->appId();
} else {
return id;
} else if (role == AppName) {
return d->appData(window).name;
} else if (role == GenericName) {
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment