diff --git a/Makefile b/Makefile index b34c638..4f7bdd3 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk -SRC = draw.c dunst.c dbus.c utils.c option_parser.c settings.c +SRC = draw.c dunst.c dbus.c utils.c option_parser.c settings.c rules.c OBJ = ${SRC:.c=.o} all: doc options dunst service diff --git a/dunst.c b/dunst.c index b92d009..fd6fa69 100644 --- a/dunst.c +++ b/dunst.c @@ -33,6 +33,7 @@ #include "draw.h" #include "dbus.h" #include "utils.h" +#include "rules.h" #include "option_parser.h" #include "settings.h" @@ -98,10 +99,6 @@ GSList *rules = NULL; // }}} // {{{ FUNCTION DEFINITIONS -/* rules */ -void rule_apply(rule_t *r, notification *n); -void rule_apply_all(notification *n); -bool rule_matches_notification(rule_t *r, notification *n); /* notifications */ int notification_cmp(const void *a, const void *b); @@ -765,74 +762,6 @@ int notification_close(notification * n, int reason) // }}} -// {{{ RULE - - /* - * Apply rule to notification. - */ -void rule_apply(rule_t *r, notification *n) -{ // {{{ - if (r->timeout != -1) - n->timeout = r->timeout; - if (r->urgency != -1) - n->urgency = r->urgency; - if (r->fg) - n->color_strings[ColFG] = r->fg; - if (r->bg) - n->color_strings[ColBG] = r->bg; - if (r->format) - n->format = r->format; - if (r->script) - n->script = r->script; -} -// }}} - - /* - * Check all rules if they match n and apply. - */ -void rule_apply_all(notification *n) -{ // {{{ - for (GSList *iter = rules; iter; iter = iter->next) { - rule_t *r = iter->data; - if (rule_matches_notification(r, n)) { - rule_apply(r, n); - } - } -} -// }}} - - /* - * Initialize rule with default values. - */ -void rule_init(rule_t *r) -{ // {{{ - r->name = NULL; - r->appname = NULL; - r->summary = NULL; - r->body = NULL; - r->icon = NULL; - r->timeout = -1; - r->urgency = -1; - r->fg = NULL; - r->bg = NULL; - r->format = NULL; -} -// }}} - - /* - * Check whether rule should be applied to n. - */ -bool rule_matches_notification(rule_t *r, notification *n) -{ // {{{ - - return ((!r->appname || !fnmatch(r->appname, n->appname, 0)) - && (!r->summary || !fnmatch(r->summary, n->summary, 0)) - && (!r->body || !fnmatch(r->body, n->body, 0)) - && (!r->icon || !fnmatch(r->icon, n->icon, 0))); -} -// }}} - -// }}} // {{{ X diff --git a/dunst.h b/dunst.h index e8dbd5a..1366f2c 100644 --- a/dunst.h +++ b/dunst.h @@ -68,22 +68,6 @@ typedef struct _notification { Actions *actions; } notification; -typedef struct _rule_t { - char *name; - /* filters */ - char *appname; - char *summary; - char *body; - char *icon; - - /* actions */ - int timeout; - int urgency; - char *fg; - char *bg; - const char *format; - const char *script; -} rule_t; typedef struct _keyboard_shortcut { const char *str; @@ -99,13 +83,11 @@ typedef struct _render_text { } render_text; extern int verbosity; -extern GSList *rules; /* return id of notification */ int notification_init(notification * n, int id); int notification_close_by_id(int id, int reason); gboolean run(void *data); void wake_up(void); -void rule_init(rule_t *r); /* vim: set ts=8 sw=8 tw=0: */ diff --git a/rules.c b/rules.c new file mode 100644 index 0000000..bcc03ab --- /dev/null +++ b/rules.c @@ -0,0 +1,69 @@ +// {{{ INVLUDES +#include +#include + +#include "dunst.h" +#include "rules.h" +// }}} + +/* + * Apply rule to notification. + */ +void rule_apply(rule_t *r, notification *n) +{ + if (r->timeout != -1) + n->timeout = r->timeout; + if (r->urgency != -1) + n->urgency = r->urgency; + if (r->fg) + n->color_strings[ColFG] = r->fg; + if (r->bg) + n->color_strings[ColBG] = r->bg; + if (r->format) + n->format = r->format; + if (r->script) + n->script = r->script; +} + +/* + * Check all rules if they match n and apply. + */ +void rule_apply_all(notification *n) +{ + for (GSList *iter = rules; iter; iter = iter->next) { + rule_t *r = iter->data; + if (rule_matches_notification(r, n)) { + rule_apply(r, n); + } + } +} + +/* + * Initialize rule with default values. + */ +void rule_init(rule_t *r) +{ + r->name = NULL; + r->appname = NULL; + r->summary = NULL; + r->body = NULL; + r->icon = NULL; + r->timeout = -1; + r->urgency = -1; + r->fg = NULL; + r->bg = NULL; + r->format = NULL; +} + + +/* + * Check whether rule should be applied to n. + */ +bool rule_matches_notification(rule_t *r, notification *n) +{ + + return ((!r->appname || !fnmatch(r->appname, n->appname, 0)) + && (!r->summary || !fnmatch(r->summary, n->summary, 0)) + && (!r->body || !fnmatch(r->body, n->body, 0)) + && (!r->icon || !fnmatch(r->icon, n->icon, 0))); +} diff --git a/rules.h b/rules.h new file mode 100644 index 0000000..56c8e4b --- /dev/null +++ b/rules.h @@ -0,0 +1,37 @@ +#pragma once + +// {{{ INCLUDES +#include + +#include "dunst.h" +// }}} + +// {{{ STRUCTS +typedef struct _rule_t { + char *name; + /* filters */ + char *appname; + char *summary; + char *body; + char *icon; + + /* actions */ + int timeout; + int urgency; + char *fg; + char *bg; + const char *format; + const char *script; +} rule_t; +// }}} + +// {{{ GLOBALS +extern GSList *rules; +// }}} + +// {{{ FUNCTIONS +void rule_init(rule_t *r); +void rule_apply(rule_t *r, notification *n); +void rule_apply_all(notification *n); +bool rule_matches_notification(rule_t *r, notification *n); +// }}} diff --git a/settings.c b/settings.c index e07ff6d..e565e1f 100644 --- a/settings.c +++ b/settings.c @@ -5,6 +5,7 @@ #include #include "dunst.h" +#include "rules.h" #include "option_parser.h" #include "settings.h" #include "config.h"