From 8a1ffcfd6f0eab565636103e3d9631ccec76d312 Mon Sep 17 00:00:00 2001 From: autumn Date: Tue, 3 Jan 2023 11:56:59 -0600 Subject: [PATCH] Action scripts (#194) Co-authored-by: auctumnus Closes https://github.com/ErikReider/SwayNotificationCenter/issues/192 --- README.md | 2 ++ man/swaync.5.scd | 7 +++++++ src/config.json.in | 5 +++++ src/configModel/configModel.vala | 6 ++++++ src/configSchema.json | 6 ++++++ src/notiDaemon/notiDaemon.vala | 21 +++++++++++++++++++-- src/notification/notification.vala | 1 + 7 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4dcf123..21f053c 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,8 @@ Scripting rules and logic: . If any of the properties doesn't match, the script will be skipped . If a notification doesn't include one of the properties, that property will be skipped +ยท If a script has `run-on` set to `action`, the script will only run when an +action is taken on the notification More information can be found in the `swaync(5)` man page diff --git a/man/swaync.5.scd b/man/swaync.5.scd index d73cb40..f345ee9 100644 --- a/man/swaync.5.scd +++ b/man/swaync.5.scd @@ -333,6 +333,13 @@ config file to be able to detect config errors type: string ++ optional: true ++ description: Which category the notification belongs to. Uses Regex.++ + *run-on*++ + type: string ++ + optional: true ++ + values: action, receive ++ + default: receive ++ + description: Whether to run this action when the notification is ++ + received, or when an action is taken on it. ++ description: Which scripts to check and potentially run for every ++ notification. If the notification doesn't include one of the properties, ++ that property will be ignored. All properties (except for exec) use regex. ++ diff --git a/src/config.json.in b/src/config.json.in index e23d63d..3ab00e8 100644 --- a/src/config.json.in +++ b/src/config.json.in @@ -28,6 +28,11 @@ "example-script": { "exec": "echo 'Do something...'", "urgency": "Normal" + }, + "example-action-script": { + "exec": "echo 'Do something actionable!'", + "urgency": "Normal", + "run-on": "action" } }, "notification-visibility": { diff --git a/src/configModel/configModel.vala b/src/configModel/configModel.vala index bb82d64..d311384 100644 --- a/src/configModel/configModel.vala +++ b/src/configModel/configModel.vala @@ -204,9 +204,15 @@ namespace SwayNotificationCenter { public NotificationStatusEnum state { get; set; } } + public enum ScriptRunOnType { + ACTION, + RECEIVE; + } + #if WANT_SCRIPTING public class Script : NotificationMatching { public string ? exec { get; set; default = null; } + public ScriptRunOnType run_on { get; set; default = ScriptRunOnType.RECEIVE; } public async bool run_script (NotifyParams param, out string msg) { msg = ""; diff --git a/src/configSchema.json b/src/configSchema.json index 647ef6b..ae9fbd7 100644 --- a/src/configSchema.json +++ b/src/configSchema.json @@ -176,6 +176,12 @@ "category": { "type": "string", "description": "Which category the notification belongs to. Uses Regex." + }, + "run-on": { + "type": "string", + "description": "Whether to run the script on an action being activated, or when the notification is received.", + "enum": ["action", "receive"], + "default": "receive" } } } diff --git a/src/notiDaemon/notiDaemon.vala b/src/notiDaemon/notiDaemon.vala index 101f057..87d3a43 100644 --- a/src/notiDaemon/notiDaemon.vala +++ b/src/notiDaemon/notiDaemon.vala @@ -195,9 +195,28 @@ namespace SwayNotificationCenter { // Run the first script if notification meets requirements HashTable scripts = ConfigModel.instance.scripts; if (scripts.length == 0) return id; + this.run_scripts (param, ScriptRunOnType.RECEIVE); +#endif + return id; + } + + /** + * Runs scripts that meet the requirements of the given `param`. + */ + [DBus (visible = false)] + public void run_scripts (NotifyParams param, ScriptRunOnType run_on) { +#if WANT_SCRIPTING + if (param.swaync_no_script) { + debug ("Skipped action scripts for this notification\n"); + return; + } + // Run the first script if notification meets requirements + HashTable scripts = ConfigModel.instance.scripts; + if (scripts.length == 0) return; foreach (string key in scripts.get_keys ()) { unowned Script script = scripts[key]; if (!script.matches_notification (param)) continue; + if (script.run_on != run_on) continue; script.run_script.begin (param, (obj, res) => { // Gets the end status @@ -238,8 +257,6 @@ namespace SwayNotificationCenter { break; } #endif - - return id; } /** diff --git a/src/notification/notification.vala b/src/notification/notification.vala index 9669cc0..32b54a6 100644 --- a/src/notification/notification.vala +++ b/src/notification/notification.vala @@ -298,6 +298,7 @@ namespace SwayNotificationCenter { } private void action_clicked (Action ? action, bool is_default = false) { + noti_daemon.run_scripts (param, ScriptRunOnType.ACTION); if (action != null && action.identifier != null && action.identifier != "") {