2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2014-03-22 08:48:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-03-22 08:48:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2014-03-22 08:48:07 +00:00
|
|
|
// own
|
|
|
|
#include "effectloader.h"
|
|
|
|
// KWin
|
2014-03-23 08:23:30 +00:00
|
|
|
#include <config-kwin.h>
|
2014-03-22 08:48:07 +00:00
|
|
|
#include <kwineffects.h>
|
|
|
|
#include "effects/effect_builtins.h"
|
2021-06-01 10:06:54 +00:00
|
|
|
#include "plugin.h"
|
2014-03-23 08:23:30 +00:00
|
|
|
#include "scripting/scriptedeffect.h"
|
2014-12-05 10:42:15 +00:00
|
|
|
#include "utils.h"
|
2014-03-22 08:48:07 +00:00
|
|
|
// KDE
|
|
|
|
#include <KConfigGroup>
|
2015-07-07 07:36:15 +00:00
|
|
|
#include <KPluginLoader>
|
2015-07-06 14:50:33 +00:00
|
|
|
#include <KPackage/Package>
|
|
|
|
#include <KPackage/PackageLoader>
|
2014-03-22 08:48:07 +00:00
|
|
|
// Qt
|
2014-03-23 08:23:30 +00:00
|
|
|
#include <QtConcurrentRun>
|
2014-03-22 08:48:07 +00:00
|
|
|
#include <QDebug>
|
2014-03-23 08:23:30 +00:00
|
|
|
#include <QFutureWatcher>
|
2021-10-08 17:27:56 +00:00
|
|
|
#include <QStaticPlugin>
|
2014-03-22 08:48:07 +00:00
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
AbstractEffectLoader::AbstractEffectLoader(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractEffectLoader::~AbstractEffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractEffectLoader::setConfig(KSharedConfig::Ptr config)
|
|
|
|
{
|
|
|
|
m_config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadEffectFlags AbstractEffectLoader::readConfig(const QString &effectName, bool defaultValue) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_config);
|
|
|
|
KConfigGroup plugins(m_config, QStringLiteral("Plugins"));
|
|
|
|
|
|
|
|
const QString key = effectName + QStringLiteral("Enabled");
|
|
|
|
|
|
|
|
// do we have a key for the effect?
|
|
|
|
if (plugins.hasKey(key)) {
|
|
|
|
// we have a key in the config, so read the enabled state
|
|
|
|
const bool load = plugins.readEntry(key, defaultValue);
|
|
|
|
return load ? LoadEffectFlags(LoadEffectFlag::Load) : LoadEffectFlags();
|
|
|
|
}
|
|
|
|
// we don't have a key, so we just use the enabled by default value
|
|
|
|
if (defaultValue) {
|
|
|
|
return LoadEffectFlag::Load | LoadEffectFlag::CheckDefaultFunction;
|
|
|
|
}
|
|
|
|
return LoadEffectFlags();
|
|
|
|
}
|
|
|
|
|
|
|
|
BuiltInEffectLoader::BuiltInEffectLoader(QObject *parent)
|
|
|
|
: AbstractEffectLoader(parent)
|
|
|
|
, m_queue(new EffectLoadQueue<BuiltInEffectLoader, BuiltInEffect>(this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BuiltInEffectLoader::~BuiltInEffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuiltInEffectLoader::hasEffect(const QString &name) const
|
|
|
|
{
|
|
|
|
return BuiltInEffects::available(internalName(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuiltInEffectLoader::isEffectSupported(const QString &name) const
|
|
|
|
{
|
2014-04-03 08:43:23 +00:00
|
|
|
return BuiltInEffects::supported(BuiltInEffects::builtInForName(internalName(name)));
|
2014-03-22 08:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList BuiltInEffectLoader::listOfKnownEffects() const
|
|
|
|
{
|
2014-04-03 08:43:23 +00:00
|
|
|
return BuiltInEffects::availableEffectNames();
|
2014-03-22 08:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BuiltInEffectLoader::loadEffect(const QString &name)
|
|
|
|
{
|
|
|
|
return loadEffect(name, BuiltInEffects::builtInForName(internalName(name)), LoadEffectFlag::Load);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuiltInEffectLoader::queryAndLoadAll()
|
|
|
|
{
|
|
|
|
const QList<BuiltInEffect> effects = BuiltInEffects::availableEffects();
|
|
|
|
for (BuiltInEffect effect : effects) {
|
|
|
|
// check whether it is already loaded
|
|
|
|
if (m_loadedEffects.contains(effect)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-04 09:49:31 +00:00
|
|
|
const QString key = BuiltInEffects::nameForEffect(effect);
|
2014-03-22 08:48:07 +00:00
|
|
|
const LoadEffectFlags flags = readConfig(key, BuiltInEffects::enabledByDefault(effect));
|
|
|
|
if (flags.testFlag(LoadEffectFlag::Load)) {
|
|
|
|
m_queue->enqueue(qMakePair(effect, flags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuiltInEffectLoader::loadEffect(BuiltInEffect effect, LoadEffectFlags flags)
|
|
|
|
{
|
2014-04-03 08:43:23 +00:00
|
|
|
return loadEffect(BuiltInEffects::nameForEffect(effect), effect, flags);
|
2014-03-22 08:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BuiltInEffectLoader::loadEffect(const QString &name, BuiltInEffect effect, LoadEffectFlags flags)
|
|
|
|
{
|
|
|
|
if (effect == BuiltInEffect::Invalid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!flags.testFlag(LoadEffectFlag::Load)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Loading flags disable effect: " << name;
|
2014-03-22 08:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// check that it is not already loaded
|
|
|
|
if (m_loadedEffects.contains(effect)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// supported might need a context
|
|
|
|
#ifndef KWIN_UNIT_TEST
|
|
|
|
effects->makeOpenGLContextCurrent();
|
|
|
|
#endif
|
|
|
|
if (!BuiltInEffects::supported(effect)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Effect is not supported: " << name;
|
2014-03-22 08:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags.testFlag(LoadEffectFlag::CheckDefaultFunction)) {
|
|
|
|
if (!BuiltInEffects::checkEnabledByDefault(effect)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Enabled by default function disables effect: " << name;
|
2014-03-22 08:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, now we can try to create the Effect
|
|
|
|
Effect *e = BuiltInEffects::create(effect);
|
|
|
|
if (!e) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Failed to create effect: " << name;
|
2014-03-22 08:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// insert in our loaded effects
|
|
|
|
m_loadedEffects.insert(effect, e);
|
|
|
|
connect(e, &Effect::destroyed, this,
|
|
|
|
[this, effect]() {
|
|
|
|
m_loadedEffects.remove(effect);
|
|
|
|
}
|
|
|
|
);
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Successfully loaded built-in effect: " << name;
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT effectLoaded(e, name);
|
2014-03-22 08:48:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:43:23 +00:00
|
|
|
QString BuiltInEffectLoader::internalName(const QString& name) const
|
2014-03-22 08:48:07 +00:00
|
|
|
{
|
2014-04-04 09:49:31 +00:00
|
|
|
return name.toLower();
|
2014-03-22 08:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 13:06:23 +00:00
|
|
|
void BuiltInEffectLoader::clear()
|
|
|
|
{
|
|
|
|
m_queue->clear();
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:23:30 +00:00
|
|
|
static const QString s_nameProperty = QStringLiteral("X-KDE-PluginInfo-Name");
|
|
|
|
static const QString s_jsConstraint = QStringLiteral("[X-Plasma-API] == 'javascript'");
|
|
|
|
static const QString s_serviceType = QStringLiteral("KWin/Effect");
|
|
|
|
|
|
|
|
ScriptedEffectLoader::ScriptedEffectLoader(QObject *parent)
|
|
|
|
: AbstractEffectLoader(parent)
|
2015-07-06 14:50:33 +00:00
|
|
|
, m_queue(new EffectLoadQueue<ScriptedEffectLoader, KPluginMetaData>(this))
|
2014-03-23 08:23:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptedEffectLoader::~ScriptedEffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptedEffectLoader::hasEffect(const QString &name) const
|
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
return findEffect(name).isValid();
|
2014-03-23 08:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptedEffectLoader::isEffectSupported(const QString &name) const
|
|
|
|
{
|
|
|
|
// scripted effects are in general supported
|
2016-08-10 07:24:53 +00:00
|
|
|
if (!ScriptedEffect::supported()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-23 08:23:30 +00:00
|
|
|
return hasEffect(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ScriptedEffectLoader::listOfKnownEffects() const
|
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
const auto effects = findAllEffects();
|
2014-03-23 08:23:30 +00:00
|
|
|
QStringList result;
|
2015-07-06 14:50:33 +00:00
|
|
|
for (const auto &service : effects) {
|
|
|
|
result << service.pluginId();
|
2014-03-23 08:23:30 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptedEffectLoader::loadEffect(const QString &name)
|
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
auto effect = findEffect(name);
|
|
|
|
if (!effect.isValid()) {
|
2014-03-23 08:23:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return loadEffect(effect, LoadEffectFlag::Load);
|
|
|
|
}
|
|
|
|
|
2015-07-06 14:50:33 +00:00
|
|
|
bool ScriptedEffectLoader::loadEffect(const KPluginMetaData &effect, LoadEffectFlags flags)
|
2014-03-23 08:23:30 +00:00
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
const QString name = effect.pluginId();
|
2014-03-23 08:23:30 +00:00
|
|
|
if (!flags.testFlag(LoadEffectFlag::Load)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Loading flags disable effect: " << name;
|
2014-03-23 08:23:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (m_loadedEffects.contains(name)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << name << "already loaded";
|
2014-03-23 08:23:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-10 07:24:53 +00:00
|
|
|
if (!ScriptedEffect::supported()) {
|
|
|
|
qCDebug(KWIN_CORE) << "Effect is not supported: " << name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-24 10:02:28 +00:00
|
|
|
ScriptedEffect *e = ScriptedEffect::create(effect);
|
2014-03-23 08:23:30 +00:00
|
|
|
if (!e) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Could not initialize scripted effect: " << name;
|
2014-03-23 08:23:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
connect(e, &ScriptedEffect::destroyed, this,
|
|
|
|
[this, name]() {
|
|
|
|
m_loadedEffects.removeAll(name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Successfully loaded scripted effect: " << name;
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT effectLoaded(e, name);
|
2014-03-23 08:23:30 +00:00
|
|
|
m_loadedEffects << name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptedEffectLoader::queryAndLoadAll()
|
|
|
|
{
|
2016-04-26 12:55:47 +00:00
|
|
|
if (m_queryConnection) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-23 08:23:30 +00:00
|
|
|
// perform querying for the services in a thread
|
2015-07-06 14:50:33 +00:00
|
|
|
QFutureWatcher<QList<KPluginMetaData>> *watcher = new QFutureWatcher<QList<KPluginMetaData>>(this);
|
2016-04-26 12:55:47 +00:00
|
|
|
m_queryConnection = connect(watcher, &QFutureWatcher<QList<KPluginMetaData>>::finished, this,
|
2014-03-23 08:23:30 +00:00
|
|
|
[this, watcher]() {
|
2015-07-06 14:50:33 +00:00
|
|
|
const auto effects = watcher->result();
|
2021-05-13 16:41:39 +00:00
|
|
|
for (const auto &effect : effects) {
|
2015-07-06 14:50:33 +00:00
|
|
|
const LoadEffectFlags flags = readConfig(effect.pluginId(), effect.isEnabledByDefault());
|
2014-03-23 08:23:30 +00:00
|
|
|
if (flags.testFlag(LoadEffectFlag::Load)) {
|
|
|
|
m_queue->enqueue(qMakePair(effect, flags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
watcher->deleteLater();
|
2016-04-26 12:55:47 +00:00
|
|
|
m_queryConnection = QMetaObject::Connection();
|
2014-03-23 08:23:30 +00:00
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
watcher->setFuture(QtConcurrent::run(this, &ScriptedEffectLoader::findAllEffects));
|
|
|
|
}
|
|
|
|
|
2015-07-06 14:50:33 +00:00
|
|
|
QList<KPluginMetaData> ScriptedEffectLoader::findAllEffects() const
|
2014-03-23 08:23:30 +00:00
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
return KPackage::PackageLoader::self()->listPackages(s_serviceType, QStringLiteral("kwin/effects"));
|
2014-03-23 08:23:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 14:50:33 +00:00
|
|
|
KPluginMetaData ScriptedEffectLoader::findEffect(const QString &name) const
|
2014-03-23 08:23:30 +00:00
|
|
|
{
|
2015-07-06 14:50:33 +00:00
|
|
|
const auto plugins = KPackage::PackageLoader::self()->findPackages(s_serviceType, QStringLiteral("kwin/effects"),
|
|
|
|
[name] (const KPluginMetaData &metadata) {
|
|
|
|
return metadata.pluginId().compare(name, Qt::CaseInsensitive) == 0;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!plugins.isEmpty()) {
|
|
|
|
return plugins.first();
|
2014-03-23 08:23:30 +00:00
|
|
|
}
|
2015-07-06 14:50:33 +00:00
|
|
|
return KPluginMetaData();
|
2014-03-23 08:23:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:39:07 +00:00
|
|
|
|
2015-11-16 13:06:23 +00:00
|
|
|
void ScriptedEffectLoader::clear()
|
|
|
|
{
|
2016-04-26 12:55:47 +00:00
|
|
|
disconnect(m_queryConnection);
|
|
|
|
m_queryConnection = QMetaObject::Connection();
|
2015-11-16 13:06:23 +00:00
|
|
|
m_queue->clear();
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:27:56 +00:00
|
|
|
static QJsonValue readPluginInfo(const QJsonObject &metadata, const QString &key)
|
|
|
|
{
|
|
|
|
return metadata.value(QLatin1String("KPlugin")).toObject().value(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticPluginEffectLoader::StaticPluginEffectLoader(QObject *parent)
|
|
|
|
: AbstractEffectLoader(parent)
|
|
|
|
, m_queue(new EffectLoadQueue<StaticPluginEffectLoader, QString>(this))
|
|
|
|
{
|
|
|
|
const QVector<QStaticPlugin> staticPlugins = QPluginLoader::staticPlugins();
|
|
|
|
for (const QStaticPlugin &staticPlugin : staticPlugins) {
|
|
|
|
const QJsonObject rootMetaData = staticPlugin.metaData();
|
|
|
|
if (rootMetaData.value(QLatin1String("IID")) != QLatin1String(EffectPluginFactory_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticPluginEffectLoader::~StaticPluginEffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StaticPluginEffectLoader::hasEffect(const QString &name) const
|
|
|
|
{
|
|
|
|
return m_staticPlugins.contains(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StaticPluginEffectLoader::isEffectSupported(const QString &name) const
|
|
|
|
{
|
|
|
|
auto it = m_staticPlugins.constFind(name);
|
|
|
|
if (it == m_staticPlugins.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (EffectPluginFactory *effectFactory = factory(*it)) {
|
|
|
|
return effectFactory->isSupported();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList StaticPluginEffectLoader::listOfKnownEffects() const
|
|
|
|
{
|
|
|
|
return m_staticPlugins.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticPluginEffectLoader::clear()
|
|
|
|
{
|
|
|
|
m_queue->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StaticPluginEffectLoader::checkEnabledByDefault(const QStaticPlugin &staticPlugin) const
|
|
|
|
{
|
|
|
|
const QJsonObject metadata = staticPlugin.metaData().value("MetaData").toObject();
|
|
|
|
if (metadata.value("org.kde.kwin.effect").toObject().value("enabledByDefaultMethod").toBool()) {
|
|
|
|
if (EffectPluginFactory *effectFactory = factory(staticPlugin)) {
|
|
|
|
return effectFactory->enabledByDefault();
|
|
|
|
}
|
|
|
|
} else if (metadata.value("KPlugin").toObject().value("EnabledByDefault").toBool()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticPluginEffectLoader::queryAndLoadAll()
|
|
|
|
{
|
|
|
|
for (auto it = m_staticPlugins.constBegin(); it != m_staticPlugins.constEnd(); ++it) {
|
|
|
|
const LoadEffectFlags flags = readConfig(it.key(), checkEnabledByDefault(it.value()));
|
|
|
|
if (flags.testFlag(LoadEffectFlag::Load)) {
|
|
|
|
m_queue->enqueue(qMakePair(it.key(), flags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StaticPluginEffectLoader::loadEffect(const QString &name)
|
|
|
|
{
|
|
|
|
return loadEffect(name, LoadEffectFlag::Load);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StaticPluginEffectLoader::loadEffect(const QString &name, LoadEffectFlags flags)
|
|
|
|
{
|
|
|
|
if (m_loadedEffects.contains(name)) {
|
|
|
|
qCDebug(KWIN_CORE) << name << "is already loaded";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto staticPlugin = m_staticPlugins.constFind(name);
|
|
|
|
if (staticPlugin == m_staticPlugins.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectPluginFactory *effectFactory = factory(*staticPlugin);
|
|
|
|
if (!effectFactory) {
|
|
|
|
qCDebug(KWIN_CORE) << "Couldn't get an EffectPluginFactory for: " << name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef KWIN_UNIT_TEST
|
|
|
|
effects->makeOpenGLContextCurrent();
|
|
|
|
#endif
|
|
|
|
if (!effectFactory->isSupported()) {
|
|
|
|
qCDebug(KWIN_CORE) << "Effect is not supported: " << name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & LoadEffectFlag::CheckDefaultFunction) {
|
|
|
|
if (!checkEnabledByDefault(*staticPlugin)) {
|
|
|
|
qCDebug(KWIN_CORE) << "Enabled by default function disables effect: " << name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Effect *effect = effectFactory->createEffect();
|
|
|
|
if (!effect) {
|
|
|
|
qCDebug(KWIN_CORE) << "Failed to create effect: " << name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert in our loaded effects
|
|
|
|
m_loadedEffects << name;
|
|
|
|
connect(effect, &Effect::destroyed, this, [this, name]() {
|
|
|
|
m_loadedEffects.removeAll(name);
|
|
|
|
});
|
|
|
|
|
|
|
|
qCDebug(KWIN_CORE) << "Successfully loaded plugin effect: " << name;
|
|
|
|
Q_EMIT effectLoaded(effect, name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectPluginFactory *StaticPluginEffectLoader::factory(const QStaticPlugin &staticPlugin) const
|
|
|
|
{
|
|
|
|
return qobject_cast<EffectPluginFactory *>(staticPlugin.instance());
|
|
|
|
}
|
|
|
|
|
2014-03-25 10:39:07 +00:00
|
|
|
PluginEffectLoader::PluginEffectLoader(QObject *parent)
|
|
|
|
: AbstractEffectLoader(parent)
|
2015-07-07 07:36:15 +00:00
|
|
|
, m_queue(new EffectLoadQueue< PluginEffectLoader, KPluginMetaData>(this))
|
2014-03-25 10:39:07 +00:00
|
|
|
, m_pluginSubDirectory(QStringLiteral("kwin/effects/plugins/"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginEffectLoader::~PluginEffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginEffectLoader::hasEffect(const QString &name) const
|
|
|
|
{
|
2015-07-07 07:36:15 +00:00
|
|
|
const auto info = findEffect(name);
|
2014-03-25 10:39:07 +00:00
|
|
|
return info.isValid();
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:36:15 +00:00
|
|
|
KPluginMetaData PluginEffectLoader::findEffect(const QString &name) const
|
2014-03-25 10:39:07 +00:00
|
|
|
{
|
2015-07-07 07:36:15 +00:00
|
|
|
const auto plugins = KPluginLoader::findPlugins(m_pluginSubDirectory,
|
|
|
|
[name] (const KPluginMetaData &data) {
|
|
|
|
return data.pluginId().compare(name, Qt::CaseInsensitive) == 0 && data.serviceTypes().contains(s_serviceType);
|
|
|
|
}
|
|
|
|
);
|
2014-03-25 10:39:07 +00:00
|
|
|
if (plugins.isEmpty()) {
|
2015-07-07 07:36:15 +00:00
|
|
|
return KPluginMetaData();
|
2014-03-25 10:39:07 +00:00
|
|
|
}
|
|
|
|
return plugins.first();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginEffectLoader::isEffectSupported(const QString &name) const
|
|
|
|
{
|
|
|
|
if (EffectPluginFactory *effectFactory = factory(findEffect(name))) {
|
|
|
|
return effectFactory->isSupported();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:36:15 +00:00
|
|
|
EffectPluginFactory *PluginEffectLoader::factory(const KPluginMetaData &info) const
|
2014-03-25 10:39:07 +00:00
|
|
|
{
|
|
|
|
if (!info.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-07 07:36:15 +00:00
|
|
|
KPluginLoader loader(info.fileName());
|
2021-06-01 10:06:54 +00:00
|
|
|
if (loader.metaData().value("IID").toString() != EffectPluginFactory_iid) {
|
|
|
|
qCDebug(KWIN_CORE) << info.pluginId() << " has not matching plugin version, expected " << PluginFactory_iid << "got " << loader.metaData().value("IID");
|
2014-03-25 10:39:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
KPluginFactory *factory = loader.factory();
|
|
|
|
if (!factory) {
|
2015-07-07 07:36:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Did not get KPluginFactory for " << info.pluginId();
|
2014-03-25 10:39:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return dynamic_cast< EffectPluginFactory* >(factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PluginEffectLoader::listOfKnownEffects() const
|
|
|
|
{
|
2015-07-07 07:36:15 +00:00
|
|
|
const auto plugins = findAllEffects();
|
2014-03-25 10:39:07 +00:00
|
|
|
QStringList result;
|
2015-07-07 07:36:15 +00:00
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
result << plugin.pluginId();
|
2014-03-25 10:39:07 +00:00
|
|
|
}
|
2015-07-31 08:17:43 +00:00
|
|
|
qCDebug(KWIN_CORE) << result;
|
2014-03-25 10:39:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PluginEffectLoader::loadEffect(const QString &name)
|
|
|
|
{
|
2015-07-07 07:36:15 +00:00
|
|
|
const auto info = findEffect(name);
|
2014-03-25 10:39:07 +00:00
|
|
|
if (!info.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return loadEffect(info, LoadEffectFlag::Load);
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:36:15 +00:00
|
|
|
bool PluginEffectLoader::loadEffect(const KPluginMetaData &info, LoadEffectFlags flags)
|
2014-03-25 10:39:07 +00:00
|
|
|
{
|
|
|
|
if (!info.isValid()) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Plugin info is not valid";
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-07 07:36:15 +00:00
|
|
|
const QString name = info.pluginId();
|
2014-03-25 10:39:07 +00:00
|
|
|
if (!flags.testFlag(LoadEffectFlag::Load)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Loading flags disable effect: " << name;
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (m_loadedEffects.contains(name)) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << name << " already loaded";
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EffectPluginFactory *effectFactory = factory(info);
|
|
|
|
if (!effectFactory) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Couldn't get an EffectPluginFactory for: " << name;
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef KWIN_UNIT_TEST
|
|
|
|
effects->makeOpenGLContextCurrent();
|
|
|
|
#endif
|
|
|
|
if (!effectFactory->isSupported()) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Effect is not supported: " << name;
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags.testFlag(LoadEffectFlag::CheckDefaultFunction)) {
|
|
|
|
if (!effectFactory->enabledByDefault()) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Enabled by default function disables effect: " << name;
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, now we can try to create the Effect
|
|
|
|
Effect *e = effectFactory->createEffect();
|
|
|
|
if (!e) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Failed to create effect: " << name;
|
2014-03-25 10:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// insert in our loaded effects
|
|
|
|
m_loadedEffects << name;
|
|
|
|
connect(e, &Effect::destroyed, this,
|
|
|
|
[this, name]() {
|
|
|
|
m_loadedEffects.removeAll(name);
|
|
|
|
}
|
|
|
|
);
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Successfully loaded plugin effect: " << name;
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT effectLoaded(e, name);
|
2014-03-25 10:39:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginEffectLoader::queryAndLoadAll()
|
|
|
|
{
|
2016-04-26 12:55:47 +00:00
|
|
|
if (m_queryConnection) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-25 10:39:07 +00:00
|
|
|
// perform querying for the services in a thread
|
2015-07-07 07:36:15 +00:00
|
|
|
QFutureWatcher<QVector<KPluginMetaData>> *watcher = new QFutureWatcher<QVector<KPluginMetaData>>(this);
|
2016-04-26 12:55:47 +00:00
|
|
|
m_queryConnection = connect(watcher, &QFutureWatcher<QVector<KPluginMetaData>>::finished, this,
|
2014-03-25 10:39:07 +00:00
|
|
|
[this, watcher]() {
|
2015-07-07 07:36:15 +00:00
|
|
|
const auto effects = watcher->result();
|
|
|
|
for (const auto &effect : effects) {
|
|
|
|
const LoadEffectFlags flags = readConfig(effect.pluginId(), effect.isEnabledByDefault());
|
2014-03-25 10:39:07 +00:00
|
|
|
if (flags.testFlag(LoadEffectFlag::Load)) {
|
|
|
|
m_queue->enqueue(qMakePair(effect, flags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
watcher->deleteLater();
|
2016-04-26 12:55:47 +00:00
|
|
|
m_queryConnection = QMetaObject::Connection();
|
2014-03-25 10:39:07 +00:00
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
watcher->setFuture(QtConcurrent::run(this, &PluginEffectLoader::findAllEffects));
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:36:15 +00:00
|
|
|
QVector<KPluginMetaData> PluginEffectLoader::findAllEffects() const
|
2014-03-25 10:39:07 +00:00
|
|
|
{
|
2015-07-07 07:36:15 +00:00
|
|
|
return KPluginLoader::findPlugins(m_pluginSubDirectory, [] (const KPluginMetaData &data) { return data.serviceTypes().contains(s_serviceType); });
|
2014-03-25 10:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginEffectLoader::setPluginSubDirectory(const QString &directory)
|
|
|
|
{
|
|
|
|
m_pluginSubDirectory = directory;
|
|
|
|
}
|
|
|
|
|
2015-11-16 13:06:23 +00:00
|
|
|
void PluginEffectLoader::clear()
|
|
|
|
{
|
2016-04-26 12:55:47 +00:00
|
|
|
disconnect(m_queryConnection);
|
|
|
|
m_queryConnection = QMetaObject::Connection();
|
2015-11-16 13:06:23 +00:00
|
|
|
m_queue->clear();
|
|
|
|
}
|
|
|
|
|
2014-03-25 12:43:56 +00:00
|
|
|
EffectLoader::EffectLoader(QObject *parent)
|
|
|
|
: AbstractEffectLoader(parent)
|
|
|
|
{
|
|
|
|
m_loaders << new BuiltInEffectLoader(this)
|
|
|
|
<< new ScriptedEffectLoader(this)
|
2021-10-08 17:27:56 +00:00
|
|
|
<< new StaticPluginEffectLoader(this)
|
2014-03-25 12:43:56 +00:00
|
|
|
<< new PluginEffectLoader(this);
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
connect(*it, &AbstractEffectLoader::effectLoaded, this, &AbstractEffectLoader::effectLoaded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectLoader::~EffectLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BOOL_MERGE( method ) \
|
|
|
|
bool EffectLoader::method(const QString &name) const \
|
|
|
|
{ \
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) { \
|
|
|
|
if ((*it)->method(name)) { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL_MERGE(hasEffect)
|
|
|
|
BOOL_MERGE(isEffectSupported)
|
|
|
|
|
|
|
|
#undef BOOL_MERGE
|
|
|
|
|
|
|
|
QStringList EffectLoader::listOfKnownEffects() const
|
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
result << (*it)->listOfKnownEffects();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EffectLoader::loadEffect(const QString &name)
|
|
|
|
{
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
if ((*it)->loadEffect(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectLoader::queryAndLoadAll()
|
|
|
|
{
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
(*it)->queryAndLoadAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectLoader::setConfig(KSharedConfig::Ptr config)
|
|
|
|
{
|
|
|
|
AbstractEffectLoader::setConfig(config);
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
(*it)->setConfig(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 13:06:23 +00:00
|
|
|
void EffectLoader::clear()
|
|
|
|
{
|
|
|
|
for (auto it = m_loaders.constBegin(); it != m_loaders.constEnd(); ++it) {
|
|
|
|
(*it)->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 08:48:07 +00:00
|
|
|
} // namespace KWin
|