From 4d66a60a4f038c61cf0051345e13c70d372195a0 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 12 Jul 2020 00:50:52 +0200 Subject: [PATCH 1/2] multiple scripts for notification --- src/notification.c | 75 ++++++++++++++++++++++++++++------------------ src/notification.h | 5 ++-- src/rules.c | 11 +++++-- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/notification.c b/src/notification.c index 0689e48..5449dc1 100644 --- a/src/notification.c +++ b/src/notification.c @@ -88,21 +88,20 @@ void notification_print(const struct notification *n) printf("\t\t\"%s\": \"%s\"\n", (char*)p_key, (char*)p_value); printf("\t}\n"); } - printf("\tscript: %s\n", n->script); + printf("\tscript_count: %d\n", n->script_count); + if (n->script_count > 0) { + printf("\tscripts: "); + for (int i = 0; i < n->script_count; i++) { + printf("'%s' ",n->scripts[i]); + } + printf("\n"); + } printf("}\n"); } /* see notification.h */ void notification_run_script(struct notification *n) { - if (STR_EMPTY(n->script)) - return; - - if (n->script_run && !settings.always_run_script) - return; - - n->script_run = true; - const char *appname = n->appname ? n->appname : ""; const char *summary = n->summary ? n->summary : ""; const char *body = n->body ? n->body : ""; @@ -110,27 +109,40 @@ void notification_run_script(struct notification *n) const char *urgency = notification_urgency_to_string(n->urgency); - int pid1 = fork(); + for(int i = 0; i < n->script_count; i++) { - if (pid1) { - int status; - waitpid(pid1, &status, 0); - } else { - int pid2 = fork(); - if (pid2) { - exit(0); + if(n->script_run[i] && !settings.always_run_script) + continue; + + const char *script = n->scripts[i]; + + if (STR_EMPTY(script)) + continue; + + n->script_run[i] = true; + + int pid1 = fork(); + + if (pid1) { + int status; + waitpid(pid1, &status, 0); } else { - int ret = execlp(n->script, - n->script, - appname, - summary, - body, - icon, - urgency, - (char *)NULL); - if (ret != 0) { - LOG_W("Unable to run script: %s", strerror(errno)); - exit(EXIT_FAILURE); + int pid2 = fork(); + if (pid2) { + exit(0); + } else { + int ret = execlp(script, + script, + appname, + summary, + body, + icon, + urgency, + (char *)NULL); + if (ret != 0) { + LOG_W("Unable to run script: %s", strerror(errno)); + exit(EXIT_FAILURE); + } } } } @@ -236,6 +248,11 @@ void notification_unref(struct notification *n) notification_private_free(n->priv); + if (n->script_count > 0){ + g_free(n->scripts); + g_free(n->script_run); + } + g_free(n); } @@ -317,13 +334,13 @@ struct notification *notification_create(void) n->transient = false; n->progress = -1; - n->script_run = false; n->dbus_valid = false; n->fullscreen = FS_SHOW; n->actions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + n->script_count = 0; return n; } diff --git a/src/notification.h b/src/notification.h index 2ba76f0..6578882 100644 --- a/src/notification.h +++ b/src/notification.h @@ -62,7 +62,8 @@ struct notification { enum markup_mode markup; const char *format; - const char *script; + const char **scripts; + int script_count; struct notification_colors colors; char *stack_tag; /**< stack notifications by tag */ @@ -79,7 +80,7 @@ struct notification { int dup_count; /**< amount of duplicate notifications stacked onto this */ int displayed_height; enum behavior_fullscreen fullscreen; //!< The instruction what to do with it, when desktop enters fullscreen - bool script_run; /**< Has the script been executed already? */ + bool *script_run; /**< Has the script been executed already? */ /* derived fields */ char *msg; /**< formatted message */ diff --git a/src/rules.c b/src/rules.c index 70a15f0..4471185 100644 --- a/src/rules.c +++ b/src/rules.c @@ -44,8 +44,15 @@ void rule_apply(struct rule *r, struct notification *n) } if (r->format) n->format = r->format; - if (r->script) - n->script = r->script; + if (r->script){ + n->scripts = g_renew(const char*,n->scripts,n->script_count + 1); + n->scripts[n->script_count] = r->script; + + n->script_run = g_renew(bool,n->script_run,n->script_count + 1); + n->script_run[n->script_count] = false; + + n->script_count++; + } if (r->set_stack_tag) { g_free(n->stack_tag); n->stack_tag = g_strdup(r->set_stack_tag); From 1b8f56eb454a7ddc74ff1569a18aa9dfad756511 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 18 Jul 2020 15:51:04 +0200 Subject: [PATCH 2/2] changed script_run back to bool --- src/notification.c | 12 ++++++------ src/notification.h | 2 +- src/rules.c | 3 --- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/notification.c b/src/notification.c index 5449dc1..1c18db4 100644 --- a/src/notification.c +++ b/src/notification.c @@ -102,6 +102,11 @@ void notification_print(const struct notification *n) /* see notification.h */ void notification_run_script(struct notification *n) { + if (n->script_run && !settings.always_run_script) + return; + + n->script_run = true; + const char *appname = n->appname ? n->appname : ""; const char *summary = n->summary ? n->summary : ""; const char *body = n->body ? n->body : ""; @@ -111,16 +116,11 @@ void notification_run_script(struct notification *n) for(int i = 0; i < n->script_count; i++) { - if(n->script_run[i] && !settings.always_run_script) - continue; - const char *script = n->scripts[i]; if (STR_EMPTY(script)) continue; - n->script_run[i] = true; - int pid1 = fork(); if (pid1) { @@ -250,7 +250,6 @@ void notification_unref(struct notification *n) if (n->script_count > 0){ g_free(n->scripts); - g_free(n->script_run); } g_free(n); @@ -334,6 +333,7 @@ struct notification *notification_create(void) n->transient = false; n->progress = -1; + n->script_run = false; n->dbus_valid = false; n->fullscreen = FS_SHOW; diff --git a/src/notification.h b/src/notification.h index 6578882..e387c4a 100644 --- a/src/notification.h +++ b/src/notification.h @@ -80,7 +80,7 @@ struct notification { int dup_count; /**< amount of duplicate notifications stacked onto this */ int displayed_height; enum behavior_fullscreen fullscreen; //!< The instruction what to do with it, when desktop enters fullscreen - bool *script_run; /**< Has the script been executed already? */ + bool script_run; /**< Has the script been executed already? */ /* derived fields */ char *msg; /**< formatted message */ diff --git a/src/rules.c b/src/rules.c index 4471185..bb9fd91 100644 --- a/src/rules.c +++ b/src/rules.c @@ -48,9 +48,6 @@ void rule_apply(struct rule *r, struct notification *n) n->scripts = g_renew(const char*,n->scripts,n->script_count + 1); n->scripts[n->script_count] = r->script; - n->script_run = g_renew(bool,n->script_run,n->script_count + 1); - n->script_run[n->script_count] = false; - n->script_count++; } if (r->set_stack_tag) {