Merge pull request #735 from Djeeberjr/master

multiple scripts for notification
This commit is contained in:
Nikos Tsipinakis 2020-07-18 22:12:26 +03:00 committed by GitHub
commit 9db8b76473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 26 deletions

View File

@ -88,16 +88,20 @@ void notification_print(const struct notification *n)
printf("\t\t\"%s\": \"%s\"\n", (char*)p_key, (char*)p_value); printf("\t\t\"%s\": \"%s\"\n", (char*)p_key, (char*)p_value);
printf("\t}\n"); 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"); printf("}\n");
} }
/* see notification.h */ /* see notification.h */
void notification_run_script(struct notification *n) void notification_run_script(struct notification *n)
{ {
if (STR_EMPTY(n->script))
return;
if (n->script_run && !settings.always_run_script) if (n->script_run && !settings.always_run_script)
return; return;
@ -110,27 +114,35 @@ void notification_run_script(struct notification *n)
const char *urgency = notification_urgency_to_string(n->urgency); const char *urgency = notification_urgency_to_string(n->urgency);
int pid1 = fork(); for(int i = 0; i < n->script_count; i++) {
if (pid1) { const char *script = n->scripts[i];
int status;
waitpid(pid1, &status, 0); if (STR_EMPTY(script))
} else { continue;
int pid2 = fork();
if (pid2) { int pid1 = fork();
exit(0);
if (pid1) {
int status;
waitpid(pid1, &status, 0);
} else { } else {
int ret = execlp(n->script, int pid2 = fork();
n->script, if (pid2) {
appname, exit(0);
summary, } else {
body, int ret = execlp(script,
icon, script,
urgency, appname,
(char *)NULL); summary,
if (ret != 0) { body,
LOG_W("Unable to run script: %s", strerror(errno)); icon,
exit(EXIT_FAILURE); urgency,
(char *)NULL);
if (ret != 0) {
LOG_W("Unable to run script: %s", strerror(errno));
exit(EXIT_FAILURE);
}
} }
} }
} }
@ -236,6 +248,10 @@ void notification_unref(struct notification *n)
notification_private_free(n->priv); notification_private_free(n->priv);
if (n->script_count > 0){
g_free(n->scripts);
}
g_free(n); g_free(n);
} }
@ -324,6 +340,7 @@ struct notification *notification_create(void)
n->actions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); n->actions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
n->script_count = 0;
return n; return n;
} }

View File

@ -62,7 +62,8 @@ struct notification {
enum markup_mode markup; enum markup_mode markup;
const char *format; const char *format;
const char *script; const char **scripts;
int script_count;
struct notification_colors colors; struct notification_colors colors;
char *stack_tag; /**< stack notifications by tag */ char *stack_tag; /**< stack notifications by tag */

View File

@ -44,8 +44,12 @@ void rule_apply(struct rule *r, struct notification *n)
} }
if (r->format) if (r->format)
n->format = r->format; n->format = r->format;
if (r->script) if (r->script){
n->script = r->script; n->scripts = g_renew(const char*,n->scripts,n->script_count + 1);
n->scripts[n->script_count] = r->script;
n->script_count++;
}
if (r->set_stack_tag) { if (r->set_stack_tag) {
g_free(n->stack_tag); g_free(n->stack_tag);
n->stack_tag = g_strdup(r->set_stack_tag); n->stack_tag = g_strdup(r->set_stack_tag);