run script via rule

This commit is contained in:
Sascha Kruse 2012-12-21 12:21:08 +01:00
parent aa0b63b270
commit 131016878b
4 changed files with 75 additions and 8 deletions

View File

@ -71,11 +71,11 @@ keyboard_shortcut context_ks = {.str = "none",
rule_t default_rules[] = { rule_t default_rules[] = {
/* name can be any unique string. It is used to identify the rule in dunstrc to override it there */ /* name can be any unique string. It is used to identify the rule in dunstrc to override it there */
/* name, appname, summary, body, icon, timeout, urgency, fg, bg, format */ /* name, appname, summary, body, icon, timeout, urgency, fg, bg, format, script */
{ "empty", NULL, NULL, NULL, NULL, -1, -1, NULL, NULL, NULL, }, { "empty", NULL, NULL, NULL, NULL, -1, -1, NULL, NULL, NULL, NULL},
/* { "rule1", "notify-send", NULL, NULL, NULL, -1, -1, NULL, NULL, "%s %b" }, */ /* { "rule1", "notify-send", NULL, NULL, NULL, -1, -1, NULL, NULL, "%s %b", NULL }, */
/* { "rule2", "Pidgin", "*says*, NULL, NULL, -1, CRITICAL, NULL, NULL, NULL }, */ /* { "rule2", "Pidgin", "*says*, NULL, NULL, -1, CRITICAL, NULL, NULL, NULL, NULL }, */
/* { "rule3", "Pidgin", "*signed on*", NULL, NULL, -1, LOW, NULL, NULL, NULL }, */ /* { "rule3", "Pidgin", "*signed on*", NULL, NULL, -1, LOW, NULL, NULL, NULL, NULL }, */
/* { "rule4", "Pidgin", "*signed off*", NULL, NULL, -1, LOW, NULL, NULL, NULL }, */ /* { "rule4", "Pidgin", "*signed off*", NULL, NULL, -1, LOW, NULL, NULL, NULL, NULL }, */
/* { "rule5", NULL, "*foobar*", NULL, NULL, -1, -1, NULL, "#00FF00", NULL, }, */ /* { "rule5", NULL, "*foobar*", NULL, NULL, -1, -1, NULL, "#00FF00", NULL, NULL }, */
}; };

54
dunst.c
View File

@ -107,6 +107,7 @@ void move_all_to_history(void);
void print_version(void); void print_version(void);
str_array *extract_urls(const char *str); str_array *extract_urls(const char *str);
void context_menu(void); void context_menu(void);
void run_script(notification *n);
void r_line_cache_init(r_line_cache *c); void r_line_cache_init(r_line_cache *c);
void r_line_cache_append(r_line_cache *c, const char *s, ColorSet *col, bool continues); void r_line_cache_append(r_line_cache *c, const char *s, ColorSet *col, bool continues);
@ -230,6 +231,50 @@ void context_menu(void) {
} }
} }
void run_script(notification *n)
{
if (!n->script || strlen(n->script) < 1)
return;
char *appname = n->appname ? n->appname : "";
char *summary = n->summary ? n->summary : "";
char *body = n->body ? n->body : "";
char *icon = n->icon ? n->icon : "";
char *urgency;
switch (n->urgency) {
case LOW:
urgency = "LOW";
break;
case NORM:
urgency = "NORMAL";
break;
case CRIT:
urgency = "CRITICAL";
break;
default:
urgency = "NORMAL";
break;
}
int pid = fork();
if (pid == 0) {
execlp(n->script, n->script,
appname,
summary,
body,
icon,
urgency,
(char *) NULL
);
} else if (pid < 0) {
PERR("Unable to fork", errno);
} else {
return;
}
}
void pause_signal_handler(int sig) void pause_signal_handler(int sig)
{ {
if (sig == SIGUSR1) { if (sig == SIGUSR1) {
@ -258,6 +303,7 @@ static void print_notification(notification * n)
printf("\t\t%s\n", (n->urls->strs)[i]); printf("\t\t%s\n", (n->urls->strs)[i]);
} }
printf("\t}\n"); printf("\t}\n");
printf("\tscript: %s\n", n->script);
printf("}\n"); printf("}\n");
} }
@ -337,6 +383,7 @@ void apply_rules(notification * n)
n->color_strings[ColBG] = n->color_strings[ColBG] =
r->bg ? r->bg : n->color_strings[ColBG]; r->bg ? r->bg : n->color_strings[ColBG];
n->format = r->format ? r->format : n->format; n->format = r->format ? r->format : n->format;
n->script = r->script ? r->script : n->script;
} }
} }
} }
@ -409,6 +456,9 @@ void update_lists()
if (!n) if (!n)
return; return;
n->start = now; n->start = now;
if (!n->redisplayed && n->script) {
run_script(n);
}
n_queue_enqueue(&displayed, n); n_queue_enqueue(&displayed, n);
} }
@ -959,6 +1009,8 @@ int init_notification(notification * n, int id)
return 0; return 0;
} }
n->script = NULL;
n->format = format; n->format = format;
apply_rules(n); apply_rules(n);
@ -1051,6 +1103,7 @@ int init_notification(notification * n, int id)
free(tmp); free(tmp);
if (print_notifications) if (print_notifications)
print_notification(n); print_notification(n);
@ -1698,6 +1751,7 @@ void load_options(char *cmdline_config_path)
r->fg = ini_get_string(cur_section, "foreground", r->fg); r->fg = ini_get_string(cur_section, "foreground", r->fg);
r->bg = ini_get_string(cur_section, "background", r->bg); r->bg = ini_get_string(cur_section, "background", r->bg);
r->format = ini_get_string(cur_section, "format", r->format); r->format = ini_get_string(cur_section, "format", r->format);
r->script = ini_get_string(cur_section, "script", NULL);
} }
#ifndef STATIC_CONFIG #ifndef STATIC_CONFIG

View File

@ -52,8 +52,10 @@ typedef struct _notification {
int dup_count; int dup_count;
ColorSet *colors; ColorSet *colors;
char *color_strings[2]; char *color_strings[2];
int progress; /* percentage + 1, 0 to hide */ int progress; /* percentage + 1, 0 to hide */
int line_count; int line_count;
const char *script;
struct { int count; char **strs; } *urls; struct { int count; char **strs; } *urls;
} notification; } notification;
@ -76,7 +78,7 @@ typedef struct _rule_t {
char *fg; char *fg;
char *bg; char *bg;
const char *format; const char *format;
const char *script;
} rule_t; } rule_t;
typedef struct _keyboard_shortcut { typedef struct _keyboard_shortcut {

11
dunstrc
View File

@ -145,10 +145,21 @@
# and 'format'. # and 'format'.
# Shell-like globbing will get expanded. # Shell-like globbing will get expanded.
# #
# SCRIPTING
# you can specify a script that gets run when the rule matches by setting
# the 'script' option.
# The script will be called as follows:
# script appname summary body icon urgency
# where urgency can be "LOW", "NORMAL" or "CRITICAL".
#
# NOTE: if you don't want a notification to be displayed, set the format to "" # NOTE: if you don't want a notification to be displayed, set the format to ""
# NOTE: It might be helpful to run dunst -print in a terminal in order to find # NOTE: It might be helpful to run dunst -print in a terminal in order to find
# fitting options for rules. # fitting options for rules.
#[script-test]
# summary = "*script*"
# script = dunst_test.sh
#[ignore] #[ignore]
## This notification will not be displayed ## This notification will not be displayed
# summary = "foobar" # summary = "foobar"