2020-11-07 19:34:55 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pluginmanager.h"
|
|
|
|
#include "dbusinterface.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "plugin.h"
|
2022-01-18 08:35:52 +00:00
|
|
|
#include "utils/common.h"
|
2020-11-07 19:34:55 +00:00
|
|
|
|
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KPluginFactory>
|
|
|
|
#include <KPluginMetaData>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
static const QString s_pluginDirectory = QStringLiteral("kwin/plugins");
|
|
|
|
|
|
|
|
static QJsonValue readPluginInfo(const QJsonObject &metadata, const QString &key)
|
|
|
|
{
|
|
|
|
return metadata.value(QLatin1String("KPlugin")).toObject().value(key);
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:52:22 +00:00
|
|
|
PluginManager::PluginManager()
|
2020-11-07 19:34:55 +00:00
|
|
|
{
|
|
|
|
const KConfigGroup config(kwinApp()->config(), QStringLiteral("Plugins"));
|
|
|
|
|
|
|
|
auto checkEnabled = [&config](const QString &pluginId, const QJsonObject &metadata) {
|
|
|
|
const QString configKey = pluginId + QLatin1String("Enabled");
|
|
|
|
if (config.hasKey(configKey)) {
|
|
|
|
return config.readEntry(configKey, false);
|
|
|
|
}
|
|
|
|
return readPluginInfo(metadata, QStringLiteral("EnabledByDefault")).toBool(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const QVector<QStaticPlugin> staticPlugins = QPluginLoader::staticPlugins();
|
|
|
|
for (const QStaticPlugin &staticPlugin : staticPlugins) {
|
|
|
|
const QJsonObject rootMetaData = staticPlugin.metaData();
|
|
|
|
if (rootMetaData.value(QLatin1String("IID")) != QLatin1String(PluginFactory_iid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QJsonObject pluginMetaData = rootMetaData.value(QLatin1String("MetaData")).toObject();
|
|
|
|
const QString pluginId = readPluginInfo(pluginMetaData, QStringLiteral("Id")).toString();
|
|
|
|
if (pluginId.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (m_staticPlugins.contains(pluginId)) {
|
|
|
|
qCWarning(KWIN_CORE) << "Conflicting plugin id" << pluginId;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_staticPlugins.insert(pluginId, staticPlugin);
|
|
|
|
|
|
|
|
if (checkEnabled(pluginId, pluginMetaData)) {
|
|
|
|
loadStaticPlugin(pluginId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:19:23 +00:00
|
|
|
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(s_pluginDirectory);
|
2020-11-07 19:34:55 +00:00
|
|
|
for (const KPluginMetaData &metadata : plugins) {
|
2022-07-05 12:00:41 +00:00
|
|
|
if (m_plugins.find(metadata.pluginId()) != m_plugins.end()) {
|
2020-11-07 19:34:55 +00:00
|
|
|
qCWarning(KWIN_CORE) << "Conflicting plugin id" << metadata.pluginId();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (checkEnabled(metadata.pluginId(), metadata.rawData())) {
|
|
|
|
loadDynamicPlugin(metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new PluginManagerDBusInterface(this);
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:52:22 +00:00
|
|
|
PluginManager::~PluginManager() = default;
|
2020-11-07 19:34:55 +00:00
|
|
|
|
|
|
|
QStringList PluginManager::loadedPlugins() const
|
|
|
|
{
|
2022-07-05 12:00:41 +00:00
|
|
|
QStringList ret;
|
|
|
|
ret.reserve(m_plugins.size());
|
|
|
|
for (const auto &[key, _] : m_plugins) {
|
|
|
|
ret.push_back(key);
|
|
|
|
}
|
|
|
|
return ret;
|
2020-11-07 19:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PluginManager::availablePlugins() const
|
|
|
|
{
|
|
|
|
QStringList ret = m_staticPlugins.keys();
|
|
|
|
|
2021-10-19 19:19:23 +00:00
|
|
|
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(s_pluginDirectory);
|
2020-11-07 19:34:55 +00:00
|
|
|
for (const KPluginMetaData &metadata : plugins) {
|
|
|
|
ret.append(metadata.pluginId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginManager::loadPlugin(const QString &pluginId)
|
|
|
|
{
|
2022-07-05 12:00:41 +00:00
|
|
|
if (m_plugins.find(pluginId) != m_plugins.end()) {
|
2020-11-07 19:34:55 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Plugin with id" << pluginId << "is already loaded";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return loadStaticPlugin(pluginId) || loadDynamicPlugin(pluginId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginManager::loadStaticPlugin(const QString &pluginId)
|
|
|
|
{
|
|
|
|
auto staticIt = m_staticPlugins.find(pluginId);
|
|
|
|
if (staticIt == m_staticPlugins.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScopedPointer<PluginFactory> factory(qobject_cast<PluginFactory *>(staticIt->instance()));
|
|
|
|
if (!factory) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to get plugin factory for" << pluginId;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instantiatePlugin(pluginId, factory.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginManager::loadDynamicPlugin(const QString &pluginId)
|
|
|
|
{
|
2021-07-22 20:59:13 +00:00
|
|
|
const KPluginMetaData metadata = KPluginMetaData::findPluginById(s_pluginDirectory, pluginId);
|
|
|
|
if (metadata.isValid()) {
|
2020-11-07 19:34:55 +00:00
|
|
|
if (loadDynamicPlugin(metadata)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginManager::loadDynamicPlugin(const KPluginMetaData &metadata)
|
|
|
|
{
|
|
|
|
if (!metadata.isValid()) {
|
|
|
|
qCDebug(KWIN_CORE) << "PluginManager::loadPlugin needs a valid plugin metadata";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString pluginId = metadata.pluginId();
|
2021-10-19 19:39:51 +00:00
|
|
|
QPluginLoader pluginLoader(metadata.fileName());
|
2021-06-01 10:06:54 +00:00
|
|
|
if (pluginLoader.metaData().value("IID").toString() != PluginFactory_iid) {
|
2020-11-07 19:34:55 +00:00
|
|
|
qCWarning(KWIN_CORE) << pluginId << "has mismatching plugin version";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScopedPointer<PluginFactory> factory(qobject_cast<PluginFactory *>(pluginLoader.instance()));
|
|
|
|
if (!factory) {
|
|
|
|
qCWarning(KWIN_CORE) << "Failed to get plugin factory for" << pluginId;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instantiatePlugin(pluginId, factory.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginManager::instantiatePlugin(const QString &pluginId, PluginFactory *factory)
|
|
|
|
{
|
2022-07-05 12:00:41 +00:00
|
|
|
if (std::unique_ptr<Plugin> plugin = factory->create()) {
|
|
|
|
m_plugins[pluginId] = std::move(plugin);
|
|
|
|
return true;
|
|
|
|
} else {
|
2020-11-07 19:34:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginManager::unloadPlugin(const QString &pluginId)
|
|
|
|
{
|
2022-07-05 12:00:41 +00:00
|
|
|
auto it = m_plugins.find(pluginId);
|
|
|
|
if (it != m_plugins.end()) {
|
|
|
|
m_plugins.erase(it);
|
|
|
|
} else {
|
2020-11-07 19:34:55 +00:00
|
|
|
qCWarning(KWIN_CORE) << "No plugin with the specified id:" << pluginId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|