| 211 | ScriptedEffect::~ScriptedEffect() = default; |
| 212 | |
| 213 | bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript) |
| 214 | { |
| 215 | qRegisterMetaType<QJSValueList>(); |
| 216 | qRegisterMetaType<QList<KWin::EffectWindow *>>(); |
| 217 | |
| 218 | QFile scriptFile(pathToScript); |
| 219 | if (!scriptFile.open(QIODevice::ReadOnly)) { |
| 220 | qCDebug(KWIN_SCRIPTING) << "Could not open script file: " << pathToScript; |
| 221 | return false; |
| 222 | } |
| 223 | m_effectName = effectName; |
| 224 | m_scriptFile = pathToScript; |
| 225 | |
| 226 | // does the effect contain an KConfigXT file? |
| 227 | QString kconfigXTFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, KWIN_DATADIR + QLatin1String("/effects/") + m_effectName + QLatin1String("/contents/config/main.xml")); |
| 228 | if (kconfigXTFile.isNull()) { |
| 229 | kconfigXTFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kwin/effects/") + m_effectName + QLatin1String("/contents/config/main.xml")); |
| 230 | } |
| 231 | if (!kconfigXTFile.isNull()) { |
| 232 | KConfigGroup cg = QCoreApplication::instance()->property("config").value<KSharedConfigPtr>()->group(QStringLiteral("Effect-%1").arg(m_effectName)); |
| 233 | QFile xmlFile(kconfigXTFile); |
| 234 | m_config = new KConfigLoader(cg, &xmlFile, this); |
| 235 | m_config->load(); |
| 236 | } |
| 237 | |
| 238 | m_engine->installExtensions(QJSEngine::ConsoleExtension); |
| 239 | |
| 240 | QJSValue globalObject = m_engine->globalObject(); |
| 241 | |
| 242 | QJSValue effectsObject = m_engine->newQObject(effects); |
| 243 | QJSEngine::setObjectOwnership(effects, QJSEngine::CppOwnership); |
| 244 | globalObject.setProperty(QStringLiteral("effects"), effectsObject); |
| 245 | |
| 246 | QJSValue selfObject = m_engine->newQObject(this); |
| 247 | QJSEngine::setObjectOwnership(this, QJSEngine::CppOwnership); |
| 248 | globalObject.setProperty(QStringLiteral("effect"), selfObject); |
| 249 | |
| 250 | globalObject.setProperty(QStringLiteral("Effect"), |
| 251 | m_engine->newQMetaObject(&ScriptedEffect::staticMetaObject)); |
| 252 | globalObject.setProperty(QStringLiteral("KWin"), |
| 253 | m_engine->newQMetaObject(&QtScriptWorkspaceWrapper::staticMetaObject)); |
| 254 | globalObject.setProperty(QStringLiteral("Globals"), |
| 255 | m_engine->newQMetaObject(&KWin::staticMetaObject)); |
| 256 | globalObject.setProperty(QStringLiteral("QEasingCurve"), |
| 257 | m_engine->newQMetaObject(&QEasingCurve::staticMetaObject)); |
| 258 | |
| 259 | static const QStringList globalProperties{ |
| 260 | QStringLiteral("animationTime"), |
| 261 | QStringLiteral("displayWidth"), |
| 262 | QStringLiteral("displayHeight"), |
| 263 | |
| 264 | QStringLiteral("registerShortcut"), |
| 265 | QStringLiteral("registerScreenEdge"), |
| 266 | QStringLiteral("registerRealtimeScreenEdge"), |
| 267 | QStringLiteral("registerTouchScreenEdge"), |
| 268 | QStringLiteral("unregisterScreenEdge"), |
| 269 | QStringLiteral("unregisterTouchScreenEdge"), |
| 270 | |