Changeset View
Changeset View
Standalone View
Standalone View
scripting/scriptingutils.h
Show First 20 Lines • Show All 165 Lines • ▼ Show 20 Line(s) | 163 | if (it == script->screenEdgeCallbacks().end()) { | |||
---|---|---|---|---|---|
166 | script->screenEdgeCallbacks().insert(edge, QList<QScriptValue>() << context->argument(1)); | 166 | script->screenEdgeCallbacks().insert(edge, QList<QScriptValue>() << context->argument(1)); | ||
167 | } else { | 167 | } else { | ||
168 | it->append(context->argument(1)); | 168 | it->append(context->argument(1)); | ||
169 | } | 169 | } | ||
170 | return engine->newVariant(true); | 170 | return engine->newVariant(true); | ||
171 | } | 171 | } | ||
172 | 172 | | |||
173 | template<class T> | 173 | template<class T> | ||
174 | QScriptValue unregisterScreenEdge(QScriptContext *context, QScriptEngine *engine) | | |||
175 | { | | |||
176 | T script = qobject_cast<T>(context->callee().data().toQObject()); | | |||
177 | if (!script) { | | |||
178 | return engine->undefinedValue(); | | |||
179 | } | | |||
180 | if (!validateParameters(context, 1, 1)) { | | |||
181 | return engine->undefinedValue(); | | |||
182 | } | | |||
183 | if (!validateArgumentType<int>(context)) { | | |||
184 | return engine->undefinedValue(); | | |||
185 | } | | |||
186 | | ||||
187 | const int edge = context->argument(0).toVariant().toInt(); | | |||
188 | QHash<int, QList<QScriptValue> >::iterator it = script->screenEdgeCallbacks().find(edge); | | |||
189 | if (it == script->screenEdgeCallbacks().end()) { | | |||
190 | //not previously registered | | |||
191 | return engine->newVariant(false); | | |||
192 | } | | |||
193 | ScreenEdges::self()->unreserve(static_cast<KWin::ElectricBorder>(edge), script); | | |||
194 | script->screenEdgeCallbacks().erase(it); | | |||
195 | return engine->newVariant(true); | | |||
196 | } | | |||
197 | | ||||
198 | template<class T> | | |||
199 | QScriptValue registerTouchScreenEdge(QScriptContext *context, QScriptEngine *engine) | 174 | QScriptValue registerTouchScreenEdge(QScriptContext *context, QScriptEngine *engine) | ||
200 | { | 175 | { | ||
201 | auto script = qobject_cast<T>(context->callee().data().toQObject()); | 176 | auto script = qobject_cast<T>(context->callee().data().toQObject()); | ||
202 | if (!script) { | 177 | if (!script) { | ||
203 | return engine->undefinedValue(); | 178 | return engine->undefinedValue(); | ||
204 | } | 179 | } | ||
205 | if (!validateParameters(context, 2, 2)) { | 180 | if (!validateParameters(context, 2, 2)) { | ||
206 | return engine->undefinedValue(); | 181 | return engine->undefinedValue(); | ||
Show All 24 Lines | 205 | if (!validateArgumentType<int>(context)) { | |||
231 | return engine->undefinedValue(); | 206 | return engine->undefinedValue(); | ||
232 | } | 207 | } | ||
233 | const int edge = context->argument(0).toVariant().toInt(); | 208 | const int edge = context->argument(0).toVariant().toInt(); | ||
234 | const auto ret = script->unregisterTouchScreenCallback(edge); | 209 | const auto ret = script->unregisterTouchScreenCallback(edge); | ||
235 | return engine->newVariant(ret); | 210 | return engine->newVariant(ret); | ||
236 | } | 211 | } | ||
237 | 212 | | |||
238 | template<class T> | 213 | template<class T> | ||
239 | QScriptValue registerUserActionsMenu(QScriptContext *context, QScriptEngine *engine) | | |||
240 | { | | |||
241 | T script = qobject_cast<T>(context->callee().data().toQObject()); | | |||
242 | if (!script) { | | |||
243 | return engine->undefinedValue(); | | |||
244 | } | | |||
245 | if (!validateParameters(context, 1, 1)) { | | |||
246 | return engine->undefinedValue(); | | |||
247 | } | | |||
248 | if (!context->argument(0).isFunction()) { | | |||
249 | context->throwError(QScriptContext::SyntaxError, i18nc("KWin Scripting error thrown due to incorrect argument", | | |||
250 | "Argument for registerUserActionsMenu needs to be a callback")); | | |||
251 | return engine->undefinedValue(); | | |||
252 | } | | |||
253 | script->registerUseractionsMenuCallback(context->argument(0)); | | |||
254 | return engine->newVariant(true); | | |||
255 | } | | |||
256 | | ||||
257 | template<class T> | | |||
258 | void screenEdgeActivated(T *script, int edge) | 214 | void screenEdgeActivated(T *script, int edge) | ||
259 | { | 215 | { | ||
260 | QHash<int, QList<QScriptValue> >::iterator it = script->screenEdgeCallbacks().find(edge); | 216 | QHash<int, QList<QScriptValue> >::iterator it = script->screenEdgeCallbacks().find(edge); | ||
261 | if (it != script->screenEdgeCallbacks().end()) { | 217 | if (it != script->screenEdgeCallbacks().end()) { | ||
262 | foreach (const QScriptValue &value, it.value()) { | 218 | foreach (const QScriptValue &value, it.value()) { | ||
263 | QScriptValue callback(value); | 219 | QScriptValue callback(value); | ||
264 | callback.call(); | 220 | callback.call(); | ||
265 | } | 221 | } | ||
266 | } | 222 | } | ||
267 | } | 223 | } | ||
268 | 224 | | |||
269 | template<class T> | | |||
270 | QScriptValue scriptingAssert(QScriptContext *context, QScriptEngine *engine, int min, int max, T defaultVal = T()) | | |||
271 | { | | |||
272 | if (!validateParameters(context, min, max)) { | | |||
273 | return engine->undefinedValue(); | | |||
274 | } | | |||
275 | switch (context->argumentCount()) { | | |||
276 | case 1: | | |||
277 | if (!validateArgumentType<T>(context)) { | | |||
278 | return engine->undefinedValue(); | | |||
279 | } | | |||
280 | break; | | |||
281 | case 2: | | |||
282 | if (max == 2) { | | |||
283 | if (!validateArgumentType<T, QString>(context)) { | | |||
284 | return engine->undefinedValue(); | | |||
285 | } | | |||
286 | } else { | | |||
287 | if (!validateArgumentType<T, T>(context)) { | | |||
288 | return engine->undefinedValue(); | | |||
289 | } | | |||
290 | } | | |||
291 | break; | | |||
292 | case 3: | | |||
293 | if (!validateArgumentType<T, T, QString>(context)) { | | |||
294 | return engine->undefinedValue(); | | |||
295 | } | | |||
296 | break; | | |||
297 | } | | |||
298 | if (max == 2) { | | |||
299 | if (context->argument(0).toVariant().value<T>() != defaultVal) { | | |||
300 | if (context->argumentCount() == max) { | | |||
301 | context->throwError(QScriptContext::UnknownError, context->argument(max - 1).toString()); | | |||
302 | } else { | | |||
303 | context->throwError(QScriptContext::UnknownError, | | |||
304 | i18nc("Assertion failed in KWin script with given value", | | |||
305 | "Assertion failed: %1", context->argument(0).toString())); | | |||
306 | } | | |||
307 | return engine->undefinedValue(); | | |||
308 | } | | |||
309 | } else { | | |||
310 | if (context->argument(0).toVariant().value<T>() != context->argument(1).toVariant().value<T>()) { | | |||
311 | if (context->argumentCount() == max) { | | |||
312 | context->throwError(QScriptContext::UnknownError, context->argument(max - 1).toString()); | | |||
313 | } else { | | |||
314 | context->throwError(QScriptContext::UnknownError, | | |||
315 | i18nc("Assertion failed in KWin script with expected value and actual value", | | |||
316 | "Assertion failed: Expected %1, got %2", | | |||
317 | context->argument(0).toString(), context->argument(1).toString())); | | |||
318 | } | | |||
319 | return engine->undefinedValue(); | | |||
320 | } | | |||
321 | } | | |||
322 | return engine->newVariant(true); | | |||
323 | } | | |||
324 | | ||||
325 | inline void registerGlobalShortcutFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | 225 | inline void registerGlobalShortcutFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | ||
326 | { | 226 | { | ||
327 | QScriptValue shortcutFunc = engine->newFunction(function); | 227 | QScriptValue shortcutFunc = engine->newFunction(function); | ||
328 | shortcutFunc.setData(engine->newQObject(parent)); | 228 | shortcutFunc.setData(engine->newQObject(parent)); | ||
329 | engine->globalObject().setProperty(QStringLiteral("registerShortcut"), shortcutFunc); | 229 | engine->globalObject().setProperty(QStringLiteral("registerShortcut"), shortcutFunc); | ||
330 | } | 230 | } | ||
331 | 231 | | |||
332 | inline void registerScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | 232 | inline void registerScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | ||
333 | { | 233 | { | ||
334 | QScriptValue shortcutFunc = engine->newFunction(function); | 234 | QScriptValue shortcutFunc = engine->newFunction(function); | ||
335 | shortcutFunc.setData(engine->newQObject(parent)); | 235 | shortcutFunc.setData(engine->newQObject(parent)); | ||
336 | engine->globalObject().setProperty(QStringLiteral("registerScreenEdge"), shortcutFunc); | 236 | engine->globalObject().setProperty(QStringLiteral("registerScreenEdge"), shortcutFunc); | ||
337 | } | 237 | } | ||
338 | 238 | | |||
339 | inline void unregisterScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | | |||
340 | { | | |||
341 | QScriptValue shortcutFunc = engine->newFunction(function); | | |||
342 | shortcutFunc.setData(engine->newQObject(parent)); | | |||
343 | engine->globalObject().setProperty(QStringLiteral("unregisterScreenEdge"), shortcutFunc); | | |||
344 | } | | |||
345 | | ||||
346 | inline void registerTouchScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | 239 | inline void registerTouchScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | ||
347 | { | 240 | { | ||
348 | QScriptValue touchScreenFunc = engine->newFunction(function); | 241 | QScriptValue touchScreenFunc = engine->newFunction(function); | ||
349 | touchScreenFunc.setData(engine->newQObject(parent)); | 242 | touchScreenFunc.setData(engine->newQObject(parent)); | ||
350 | engine->globalObject().setProperty(QStringLiteral("registerTouchScreenEdge"), touchScreenFunc); | 243 | engine->globalObject().setProperty(QStringLiteral("registerTouchScreenEdge"), touchScreenFunc); | ||
351 | } | 244 | } | ||
352 | 245 | | |||
353 | inline void unregisterTouchScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | 246 | inline void unregisterTouchScreenEdgeFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | ||
354 | { | 247 | { | ||
355 | QScriptValue touchScreenFunc = engine->newFunction(function); | 248 | QScriptValue touchScreenFunc = engine->newFunction(function); | ||
356 | touchScreenFunc.setData(engine->newQObject(parent)); | 249 | touchScreenFunc.setData(engine->newQObject(parent)); | ||
357 | engine->globalObject().setProperty(QStringLiteral("unregisterTouchScreenEdge"), touchScreenFunc); | 250 | engine->globalObject().setProperty(QStringLiteral("unregisterTouchScreenEdge"), touchScreenFunc); | ||
358 | } | 251 | } | ||
359 | 252 | | |||
360 | inline void registerUserActionsMenuFunction(QObject *parent, QScriptEngine *engine, QScriptEngine::FunctionSignature function) | 253 | QVariant dbusToVariant(const QVariant &variant); | ||
361 | { | | |||
362 | QScriptValue shortcutFunc = engine->newFunction(function); | | |||
363 | shortcutFunc.setData(engine->newQObject(parent)); | | |||
364 | engine->globalObject().setProperty(QStringLiteral("registerUserActionsMenu"), shortcutFunc); | | |||
365 | } | | |||
366 | 254 | | |||
367 | } // namespace KWin | 255 | } // namespace KWin | ||
368 | 256 | | |||
369 | #endif // KWIN_SCRIPTINGUTILS_H | 257 | #endif // KWIN_SCRIPTINGUTILS_H |