diff --git a/effectloader.cpp b/effectloader.cpp index e2a02b1a46..70be722310 100644 --- a/effectloader.cpp +++ b/effectloader.cpp @@ -89,17 +89,12 @@ bool BuiltInEffectLoader::hasEffect(const QString &name) const bool BuiltInEffectLoader::isEffectSupported(const QString &name) const { - return BuiltInEffects::supported(internalName(name)); + return BuiltInEffects::supported(BuiltInEffects::builtInForName(internalName(name))); } QStringList BuiltInEffectLoader::listOfKnownEffects() const { - const QList availableEffects = BuiltInEffects::availableEffectNames(); - QStringList result; - for (const QByteArray name : availableEffects) { - result << QString::fromUtf8(name); - } - return result; + return BuiltInEffects::availableEffectNames(); } bool BuiltInEffectLoader::loadEffect(const QString &name) @@ -117,7 +112,7 @@ void BuiltInEffectLoader::queryAndLoadAll() } // as long as the KCM uses kwin4_effect_ we need to add it, TODO remove const QString key = QStringLiteral("kwin4_effect_") + - QString::fromUtf8(BuiltInEffects::nameForEffect(effect)); + BuiltInEffects::nameForEffect(effect); const LoadEffectFlags flags = readConfig(key, BuiltInEffects::enabledByDefault(effect)); if (flags.testFlag(LoadEffectFlag::Load)) { m_queue->enqueue(qMakePair(effect, flags)); @@ -127,7 +122,7 @@ void BuiltInEffectLoader::queryAndLoadAll() bool BuiltInEffectLoader::loadEffect(BuiltInEffect effect, LoadEffectFlags flags) { - return loadEffect(QString::fromUtf8(BuiltInEffects::nameForEffect(effect)), effect, flags); + return loadEffect(BuiltInEffects::nameForEffect(effect), effect, flags); } bool BuiltInEffectLoader::loadEffect(const QString &name, BuiltInEffect effect, LoadEffectFlags flags) @@ -178,14 +173,14 @@ bool BuiltInEffectLoader::loadEffect(const QString &name, BuiltInEffect effect, return true; } -QByteArray BuiltInEffectLoader::internalName(const QString& name) const +QString BuiltInEffectLoader::internalName(const QString& name) const { QString internalName = name.toLower(); // as long as the KCM uses kwin4_effect_ we need to add it, TODO remove if (internalName.startsWith(QStringLiteral("kwin4_effect_"))) { internalName = internalName.mid(13); } - return internalName.toUtf8(); + return internalName; } static const QString s_nameProperty = QStringLiteral("X-KDE-PluginInfo-Name"); diff --git a/effectloader.h b/effectloader.h index ad6552a063..ca463d6262 100644 --- a/effectloader.h +++ b/effectloader.h @@ -282,7 +282,7 @@ public: private: bool loadEffect(const QString &name, BuiltInEffect effect, LoadEffectFlags flags); - QByteArray internalName(const QString &name) const; + QString internalName(const QString &name) const; EffectLoadQueue *m_queue; QMap m_loadedEffects; }; diff --git a/effects/effect_builtins.cpp b/effects/effect_builtins.cpp index a83356a63a..977df94f20 100644 --- a/effects/effect_builtins.cpp +++ b/effects/effect_builtins.cpp @@ -64,262 +64,358 @@ along with this program. If not, see . namespace KWin { +namespace BuiltInEffects +{ + template inline Effect *createHelper() { return new T(); } -class EffectLoader -{ -public: - EffectLoader(); - Effect *create(const QByteArray &name); - Effect *create(BuiltInEffect effect); - bool hasEffect(const QByteArray &name) const; - bool supported(const QByteArray &name) const; - bool supported(BuiltInEffect effect) const; - bool checkEnabledByDefault(const QByteArray &name) const; - bool checkEnabledByDefault(BuiltInEffect effect) const; - bool enabledByDefault(BuiltInEffect effect) const; - QList availableEffectNames() const; - QList availableEffects() const; - BuiltInEffect builtInForName(const QByteArray &name) const; - QByteArray nameForEffect(BuiltInEffect effect) const; - -private: - typedef Effect *(*CreateInstanceFunction)(); - typedef bool (*SupportedFunction)(); - QHash m_effects; - QMap m_createHash; - QMap m_supportedHash; - QMap m_checkEnabledHash; - QMap m_enabledByDefault; +struct EffectData { + QString name; + bool enabled; + std::function createFunction; + std::function supportedFunction; + std::function enabledFunction; }; -EffectLoader::EffectLoader() -{ -#define EFFECT(name, className, enabled) \ - m_effects.insert(QByteArrayLiteral(#name).toLower(), BuiltInEffect::name);\ - m_createHash.insert(BuiltInEffect::name, &createHelper< className >); \ - m_enabledByDefault.insert(BuiltInEffect::name, enabled); - EFFECT(Blur, BlurEffect, true) - EFFECT(Contrast, ContrastEffect, true) - EFFECT(CoverSwitch, CoverSwitchEffect, false) - EFFECT(Cube, CubeEffect, false) - EFFECT(CubeSlide, CubeSlideEffect, false) - EFFECT(Dashboard, DashboardEffect, true) - EFFECT(DesktopGrid, DesktopGridEffect, true) - EFFECT(DimInactive, DimInactiveEffect, false) - EFFECT(DimScreen, DimScreenEffect, false) - EFFECT(FallApart, FallApartEffect, false) - EFFECT(FlipSwitch, FlipSwitchEffect, false) - EFFECT(Glide, GlideEffect, false) - EFFECT(HighlightWindow, HighlightWindowEffect, true) - EFFECT(Invert, InvertEffect, false) - EFFECT(Kscreen, KscreenEffect, true) - EFFECT(Logout, LogoutEffect, true) - EFFECT(LookingGlass, LookingGlassEffect, false) - EFFECT(MagicLamp, MagicLampEffect, false) - EFFECT(Magnifier, MagnifierEffect, false) - EFFECT(MinimizeAnimation, MinimizeAnimationEffect, true) - EFFECT(MouseClick, MouseClickEffect, false) - EFFECT(MouseMark, MouseMarkEffect, false) - EFFECT(PresentWindows, PresentWindowsEffect, true) - EFFECT(Resize, ResizeEffect, false) - EFFECT(ScreenEdge, ScreenEdgeEffect, true) - EFFECT(ScreenShot, ScreenShotEffect, true) - EFFECT(Sheet, SheetEffect, false) - EFFECT(ShowFps, ShowFpsEffect, false) - EFFECT(ShowPaint, ShowPaintEffect, false) - EFFECT(Slide, SlideEffect, true) - EFFECT(SlideBack, SlideBackEffect, false) - EFFECT(SlidingPopups, SlidingPopupsEffect, true) - EFFECT(SnapHelper, SnapHelperEffect, false) - EFFECT(StartupFeedback, StartupFeedbackEffect, true) - EFFECT(ThumbnailAside, ThumbnailAsideEffect, false) - EFFECT(TrackMouse, TrackMouseEffect, false) - EFFECT(WindowGeometry, WindowGeometry, false) - EFFECT(WobblyWindows, WobblyWindowsEffect, false) - EFFECT(Zoom, ZoomEffect, true) - -#undef EFFECT - -#define SUPPORTED(name, method) \ - m_supportedHash.insert(BuiltInEffect::name, &method); - SUPPORTED(Blur, BlurEffect::supported) - SUPPORTED(Contrast, ContrastEffect::supported) - SUPPORTED(CoverSwitch, CoverSwitchEffect::supported) - SUPPORTED(Cube, CubeEffect::supported) - SUPPORTED(CubeSlide, CubeSlideEffect::supported) - SUPPORTED(FallApart, FallApartEffect::supported) - SUPPORTED(FlipSwitch, FlipSwitchEffect::supported) - SUPPORTED(Glide, GlideEffect::supported) - SUPPORTED(Invert, InvertEffect::supported) - SUPPORTED(LookingGlass, LookingGlassEffect::supported) - SUPPORTED(MagicLamp, MagicLampEffect::supported) - SUPPORTED(Magnifier, MagnifierEffect::supported) - SUPPORTED(ScreenShot, ScreenShotEffect::supported) - SUPPORTED(Sheet, SheetEffect::supported) - SUPPORTED(StartupFeedback, StartupFeedbackEffect::supported) - SUPPORTED(WobblyWindows, WobblyWindowsEffect::supported) - -#undef SUPPORTED - -#define CHECKENABLED(name, method) \ - m_checkEnabledHash.insert(BuiltInEffect::name, &method); - CHECKENABLED(Blur, BlurEffect::enabledByDefault) - CHECKENABLED(Contrast, ContrastEffect::enabledByDefault) - -#undef CHECKENABLED -} - -Effect *EffectLoader::create(const QByteArray &name) -{ - return create(builtInForName(name)); -} - -Effect *EffectLoader::create(BuiltInEffect effect) -{ - auto it = m_createHash.constFind(effect); - if (it == m_createHash.constEnd()) { - return nullptr; +static const QVector s_effectData = { + { + QString(), + false, + nullptr, + nullptr, + nullptr + }, { + QStringLiteral("blur"), + true, + &createHelper, + &BlurEffect::supported, + &BlurEffect::enabledByDefault + }, { + QStringLiteral("contrast"), + true, + &createHelper, + &ContrastEffect::supported, + &ContrastEffect::enabledByDefault + }, { + QStringLiteral("coverswitch"), + false, + &createHelper, + &CoverSwitchEffect::supported, + nullptr + }, { + QStringLiteral("cube"), + false, + &createHelper, + &CubeEffect::supported, + nullptr + }, { + QStringLiteral("cubeslide"), + false, + &createHelper, + &CubeSlideEffect::supported, + nullptr + }, { + QStringLiteral("dashboard"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("desktopgrid"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("diminactive"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("dimscreen"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("fallapart"), + false, + &createHelper, + &FallApartEffect::supported, + nullptr + }, { + QStringLiteral("flipswitch"), + false, + &createHelper, + &FlipSwitchEffect::supported, + nullptr + }, { + QStringLiteral("glide"), + false, + &createHelper, + &GlideEffect::supported, + nullptr + }, { + QStringLiteral("highlightwindow"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("invert"), + false, + &createHelper, + &InvertEffect::supported, + nullptr + }, { + QStringLiteral("kscreen"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("logout"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("lookingglass"), + false, + &createHelper, + &LookingGlassEffect::supported, + nullptr + }, { + QStringLiteral("magiclamp"), + false, + &createHelper, + &MagicLampEffect::supported, + nullptr + }, { + QStringLiteral("magnifier"), + false, + &createHelper, + &MagnifierEffect::supported, + nullptr + }, { + QStringLiteral("minimizeanimation"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("mouseclick"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("mousemark"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("presentwindows"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("resize"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("screenedge"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("screenshot"), + true, + &createHelper, + &ScreenShotEffect::supported, + nullptr + }, { + QStringLiteral("sheet"), + false, + &createHelper, + &SheetEffect::supported, + nullptr + }, { + QStringLiteral("showfps"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("showpaint"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("slide"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("slideback"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("slidingpopups"), + true, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("snaphelper"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("startupfeedback"), + true, + &createHelper, + &StartupFeedbackEffect::supported, + nullptr + }, { + QStringLiteral("thumbnailaside"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("trackmouse"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("windowgeometry"), + false, + &createHelper, + nullptr, + nullptr + }, { + QStringLiteral("wobblywindows"), + false, + &createHelper, + &WobblyWindowsEffect::supported, + nullptr + }, { + QStringLiteral("zoom"), + true, + &createHelper, + nullptr, + nullptr } - return it.value()(); -} +}; -bool EffectLoader::hasEffect(const QByteArray &name) const +static inline int index(BuiltInEffect effect) { - return m_effects.contains(name); -} - -bool EffectLoader::supported(const QByteArray &name) const -{ - return supported(builtInForName(name)); -} - -bool EffectLoader::supported(BuiltInEffect effect) const -{ - if (effect == BuiltInEffect::Invalid) { - return false; - } - auto it = m_supportedHash.constFind(effect); - if (it != m_supportedHash.constEnd()) { - return it.value()(); - } - return true; -} - -bool EffectLoader::checkEnabledByDefault(const QByteArray &name) const -{ - return checkEnabledByDefault(builtInForName(name)); -} - -bool EffectLoader::checkEnabledByDefault(BuiltInEffect effect) const -{ - auto it = m_checkEnabledHash.constFind(effect); - if (it != m_checkEnabledHash.constEnd()) { - return it.value()(); - } - return true; -} - -bool EffectLoader::enabledByDefault(BuiltInEffect effect) const -{ - auto it = m_enabledByDefault.constFind(effect); - if (it != m_enabledByDefault.constEnd()) { - return it.value(); - } - return false; -} - -QList< QByteArray > EffectLoader::availableEffectNames() const -{ - return m_effects.keys(); -} - -QList< BuiltInEffect > EffectLoader::availableEffects() const -{ - return m_effects.values(); -} - -BuiltInEffect EffectLoader::builtInForName(const QByteArray &name) const -{ - auto it = m_effects.find(name); - if (it == m_effects.end()) { - return BuiltInEffect::Invalid; - } - return it.value(); -} - -QByteArray EffectLoader::nameForEffect(BuiltInEffect effect) const -{ - return m_effects.key(effect); -} - -Q_GLOBAL_STATIC(EffectLoader, s_effectLoader) - -namespace BuiltInEffects -{ - -Effect *create(const QByteArray &name) -{ - return s_effectLoader->create(name); + return static_cast(effect); } Effect *create(BuiltInEffect effect) { - return s_effectLoader->create(effect); + const EffectData &effectData = s_effectData.at(index(effect)); + if (effectData.createFunction == nullptr) { + return nullptr; + } + return effectData.createFunction(); } -bool available(const QByteArray &name) +bool available(const QString &name) { - return s_effectLoader->hasEffect(name); -} - -bool supported(const QByteArray &name) -{ - return s_effectLoader->supported(name); + auto it = std::find_if(s_effectData.begin(), s_effectData.end(), + [name](const EffectData &data) { + return data.name == name; + } + ); + return it != s_effectData.end(); } bool supported(BuiltInEffect effect) { - return s_effectLoader->supported(effect); -} - -bool checkEnabledByDefault(const QByteArray &name) -{ - return s_effectLoader->checkEnabledByDefault(name); + if (effect == BuiltInEffect::Invalid) { + return false; + } + const EffectData &effectData = s_effectData.at(index(effect)); + if (effectData.supportedFunction == nullptr) { + return true; + } + return effectData.supportedFunction(); } bool checkEnabledByDefault(BuiltInEffect effect) { - return s_effectLoader->checkEnabledByDefault(effect); + if (effect == BuiltInEffect::Invalid) { + return false; + } + const EffectData &effectData = s_effectData.at(index(effect)); + if (effectData.enabledFunction == nullptr) { + return true; + } + return effectData.enabledFunction(); } bool enabledByDefault(BuiltInEffect effect) { - return s_effectLoader->enabledByDefault(effect); + const EffectData &effectData = s_effectData.at(index(effect)); + return effectData.enabled; } -QList< QByteArray > availableEffectNames() +QStringList availableEffectNames() { - return s_effectLoader->availableEffectNames(); + QStringList result; + for (const EffectData &data : s_effectData) { + if (data.name.isEmpty()) { + continue; + } + result << data.name; + } + return result; } QList< BuiltInEffect > availableEffects() { - return s_effectLoader->availableEffects(); + QList result; + for (int i = index(BuiltInEffect::Invalid) + 1; i <= index(BuiltInEffect::Zoom); ++i) { + result << BuiltInEffect(i); + } + return result; } -BuiltInEffect builtInForName(const QByteArray &name) +BuiltInEffect builtInForName(const QString &name) { - return s_effectLoader->builtInForName(name); + auto it = std::find_if(s_effectData.begin(), s_effectData.end(), + [name](const EffectData &data) { + return data.name == name; + } + ); + if (it == s_effectData.end()) { + return BuiltInEffect::Invalid; + } + return BuiltInEffect(std::distance(s_effectData.begin(), it)); } -QByteArray nameForEffect(BuiltInEffect effect) +QString nameForEffect(BuiltInEffect effect) { - return s_effectLoader->nameForEffect(effect); + return s_effectData.at(index(effect)).name; } } // BuiltInEffects diff --git a/effects/effect_builtins.h b/effects/effect_builtins.h index 2971c0c735..a7cf027c63 100644 --- a/effects/effect_builtins.h +++ b/effects/effect_builtins.h @@ -20,9 +20,7 @@ along with this program. If not, see . #ifndef KWIN_EFFECT_BUILTINS_H #define KWIN_EFFECT_BUILTINS_H #include -#include - -class QByteArray; +#include namespace KWin { @@ -78,17 +76,14 @@ enum class BuiltInEffect namespace BuiltInEffects { -KWINEFFECTS_EXPORT Effect *create(const QByteArray &name); KWINEFFECTS_EXPORT Effect *create(BuiltInEffect effect); -KWINEFFECTS_EXPORT bool available(const QByteArray &name); -KWINEFFECTS_EXPORT bool supported(const QByteArray &name); +KWINEFFECTS_EXPORT bool available(const QString &name); KWINEFFECTS_EXPORT bool supported(BuiltInEffect effect); -KWINEFFECTS_EXPORT bool checkEnabledByDefault(const QByteArray &name); KWINEFFECTS_EXPORT bool checkEnabledByDefault(BuiltInEffect effect); KWINEFFECTS_EXPORT bool enabledByDefault(BuiltInEffect effect); -KWINEFFECTS_EXPORT QByteArray nameForEffect(BuiltInEffect effect); -KWINEFFECTS_EXPORT BuiltInEffect builtInForName(const QByteArray &name); -KWINEFFECTS_EXPORT QList availableEffectNames(); +KWINEFFECTS_EXPORT QString nameForEffect(BuiltInEffect effect); +KWINEFFECTS_EXPORT BuiltInEffect builtInForName(const QString &name); +KWINEFFECTS_EXPORT QStringList availableEffectNames(); KWINEFFECTS_EXPORT QList availableEffects(); }