diff --git a/src/dbus.c b/src/dbus.c index 2688efa..634355c 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -303,7 +303,7 @@ static void on_close_notification(GDBusConnection *connection, { guint32 id; g_variant_get(parameters, "(u)", &id); - queues_notification_close_id(id, 3); + queues_notification_close_id(id, REASON_SIG); wake_up(); g_dbus_method_invocation_return_value(invocation, NULL); g_dbus_connection_flush(connection, NULL, NULL, NULL); @@ -322,8 +322,14 @@ static void on_get_server_information(GDBusConnection *connection, g_dbus_connection_flush(connection, NULL, NULL, NULL); } -void notification_closed(notification *n, int reason) +void notification_closed(notification *n, enum reason reason) { + if (reason < REASON_MIN || REASON_MAX < reason) { + fprintf(stderr, "ERROR: Closing notification with reason '%d' not supported. " + "Closing it with reason '%d'.\n", reason, REASON_UNDEF); + reason = REASON_UNDEF; + } + if (!dbus_conn) { fprintf(stderr, "ERROR: Tried to close notification but dbus connection not set!\n"); return; diff --git a/src/dbus.h b/src/dbus.h index 2d231e0..51fd323 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -5,10 +5,19 @@ #include "notification.h" +enum reason { + REASON_MIN = 1, + REASON_TIME = 1, + REASON_USER = 2, + REASON_SIG = 3, + REASON_UNDEF = 4, + REASON_MAX = 4, +}; + int initdbus(void); void dbus_tear_down(int id); /* void dbus_poll(int timeout); */ -void notification_closed(notification *n, int reason); +void notification_closed(notification *n, enum reason reason); void action_invoked(notification *n, const char *identifier); #endif diff --git a/src/dunst.c b/src/dunst.c index 80a1400..0a2b495 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -157,7 +157,7 @@ int dunst_main(int argc, char *argv[]) n->progress = -1; n->timeout = 10 * G_USEC_PER_SEC; n->markup = MARKUP_NO; - n->urgency = LOW; + n->urgency = URG_LOW; notification_init(n); queues_notification_insert(n, 0); // we do not call wakeup now, wake_up does not work here yet diff --git a/src/notification.c b/src/notification.c index 68c025d..ad6beea 100644 --- a/src/notification.c +++ b/src/notification.c @@ -38,7 +38,7 @@ void notification_print(notification *n) printf("\traw_icon set: %s\n", (n->raw_icon ? "true" : "false")); printf("\tcategory: %s\n", n->category); printf("\ttimeout: %ld\n", n->timeout/1000); - printf("\turgency: %d\n", n->urgency); + printf("\turgency: %s\n", notification_urgency_to_string(n->urgency)); printf("\ttransient: %d\n", n->transient); printf("\tformatted: '%s'\n", n->msg); printf("\tfg: %s\n", n->color_strings[ColFG]); @@ -80,21 +80,7 @@ void notification_run_script(notification *n) 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; - } + const char *urgency = notification_urgency_to_string(n->urgency); int pid1 = fork(); @@ -121,6 +107,25 @@ void notification_run_script(notification *n) } } +/* + * Helper function to convert an urgency to a string + */ +const char *notification_urgency_to_string(enum urgency urgency) +{ + switch (urgency) { + case URG_NONE: + return "NONE"; + case URG_LOW: + return "LOW"; + case URG_NORM: + return "NORMAL"; + case URG_CRIT: + return "CRITICAL"; + default: + return "UNDEF"; + } +} + /* * Helper function to compare to given * notifications. @@ -432,8 +437,11 @@ void notification_init(notification *n) n->dup_count = 0; - /* urgency > CRIT -> array out of range */ - n->urgency = n->urgency > CRIT ? CRIT : n->urgency; + /* urgency > URG_CRIT -> array out of range */ + if (n->urgency < URG_MIN) + n->urgency = URG_LOW; + if (n->urgency > URG_MAX) + n->urgency = URG_CRIT; if (!n->color_strings[ColFG]) { n->color_strings[ColFG] = xctx.color_strings[ColFG][n->urgency]; diff --git a/src/notification.h b/src/notification.h index d5b0ae9..29b6c6c 100644 --- a/src/notification.h +++ b/src/notification.h @@ -7,12 +7,17 @@ #include "settings.h" -#define LOW 0 -#define NORM 1 -#define CRIT 2 - #define DUNST_NOTIF_MAX_CHARS 5000 +enum urgency { + URG_NONE = -1, + URG_MIN = 0, + URG_LOW = 0, + URG_NORM = 1, + URG_CRIT = 2, + URG_MAX = 2, +}; + typedef struct _raw_image { int width; int height; @@ -43,7 +48,7 @@ typedef struct _notification { gint64 start; gint64 timestamp; gint64 timeout; - int urgency; + enum urgency urgency; enum markup_mode markup; bool redisplayed; /* has been displayed before? */ int id; @@ -74,5 +79,6 @@ void notification_replace_single_field(char **haystack, char **needle, const cha void notification_update_text_to_render(notification *n); void notification_do_action(notification *n); +const char *notification_urgency_to_string(enum urgency urgency); #endif /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/queues.c b/src/queues.c index 5185f1a..997ab5a 100644 --- a/src/queues.c +++ b/src/queues.c @@ -7,7 +7,6 @@ #include #include -#include "dbus.h" #include "notification.h" #include "settings.h" @@ -179,7 +178,7 @@ bool queues_notification_replace_id(notification *new) return false; } -int queues_notification_close_id(int id, int reason) +int queues_notification_close_id(int id, enum reason reason) { notification *target = NULL; @@ -205,17 +204,14 @@ int queues_notification_close_id(int id, int reason) } if (target) { - - if (reason > 0 && reason < 4) - notification_closed(target, reason); - + notification_closed(target, reason); queues_history_push(target); } return reason; } -int queues_notification_close(notification *n, int reason) +int queues_notification_close(notification *n, enum reason reason) { assert(n != NULL); return queues_notification_close_id(n->id, reason); @@ -250,11 +246,11 @@ void queues_history_push(notification *n) void queues_history_push_all(void) { while (displayed->length > 0) { - queues_notification_close(g_queue_peek_head_link(displayed)->data, 2); + queues_notification_close(g_queue_peek_head_link(displayed)->data, REASON_USER); } while (waiting->length > 0) { - queues_notification_close(g_queue_peek_head_link(waiting)->data, 2); + queues_notification_close(g_queue_peek_head_link(waiting)->data, REASON_USER); } } @@ -288,7 +284,7 @@ void queues_check_timeouts(bool idle) /* remove old message */ if (g_get_monotonic_time() - n->start > n->timeout) { - queues_notification_close(n, 1); + queues_notification_close(n, REASON_TIME); } } } diff --git a/src/queues.h b/src/queues.h index d318180..8bd23f6 100644 --- a/src/queues.h +++ b/src/queues.h @@ -4,6 +4,7 @@ #define DUNST_QUEUE_H #include "notification.h" +#include "dbus.h" /* * Initialise neccessary queues @@ -58,21 +59,15 @@ bool queues_notification_replace_id(notification *new); * * After closing, call wake_up to synchronize the queues with the UI * (which closes the notification on screen) - * - * reasons: - * -1 -> notification is a replacement, no NotificationClosed signal emitted - * 1 -> the notification expired - * 2 -> the notification was dismissed by the user_data - * 3 -> The notification was closed by a call to CloseNotification */ -int queues_notification_close_id(int id, int reason); +int queues_notification_close_id(int id, enum reason reason); /* Close the given notification. SEE queues_notification_close_id. * * @n: (transfer full): The notification to close * @reason: The reason to close * */ -int queues_notification_close(notification *n, int reason); +int queues_notification_close(notification *n, enum reason reason); /* * Pushed the latest notification of history to the displayed queue diff --git a/src/rules.c b/src/rules.c index a9455b2..086c627 100644 --- a/src/rules.c +++ b/src/rules.c @@ -14,7 +14,7 @@ void rule_apply(rule_t *r, notification *n) { if (r->timeout != -1) n->timeout = r->timeout; - if (r->urgency != -1) + if (r->urgency != URG_NONE) n->urgency = r->urgency; if (r->history_ignore != -1) n->history_ignore = r->history_ignore; @@ -62,9 +62,9 @@ void rule_init(rule_t *r) r->body = NULL; r->icon = NULL; r->category = NULL; - r->msg_urgency = -1; + r->msg_urgency = URG_NONE; r->timeout = -1; - r->urgency = -1; + r->urgency = URG_NONE; r->markup = MARKUP_NULL; r->new_icon = NULL; r->history_ignore = false; @@ -86,6 +86,6 @@ bool rule_matches_notification(rule_t *r, notification *n) && (!r->icon || !fnmatch(r->icon, n->icon, 0)) && (!r->category || !fnmatch(r->category, n->category, 0)) && (r->match_transient == -1 || (r->match_transient == n->transient)) - && (r->msg_urgency == -1 || r->msg_urgency == n->urgency)); + && (r->msg_urgency == URG_NONE || r->msg_urgency == n->urgency)); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/rules.h b/src/rules.h index a813d98..b8d1d87 100644 --- a/src/rules.h +++ b/src/rules.h @@ -20,7 +20,7 @@ typedef struct _rule_t { /* actions */ gint64 timeout; - int urgency; + enum urgency urgency; enum markup_mode markup; int history_ignore; int match_transient; diff --git a/src/settings.c b/src/settings.c index d9566ad..0d4a569 100644 --- a/src/settings.c +++ b/src/settings.c @@ -47,18 +47,18 @@ static enum markup_mode parse_markup_mode(const char *mode) } } -static int ini_get_urgency(const char *section, const char *key, const int def) +static enum urgency ini_get_urgency(const char *section, const char *key, const int def) { int ret = def; char *urg = ini_get_string(section, key, ""); if (strlen(urg) > 0) { if (strcmp(urg, "low") == 0) - ret = LOW; + ret = URG_LOW; else if (strcmp(urg, "normal") == 0) - ret = NORM; + ret = URG_NORM; else if (strcmp(urg, "critical") == 0) - ret = CRIT; + ret = URG_CRIT; else fprintf(stderr, "unknown urgency: %s, ignoring\n", @@ -492,15 +492,15 @@ void load_settings(char *cmdline_config_path) "Frame color for notifications with low urgency" ); - settings.timeouts[LOW] = option_get_time( + settings.timeouts[URG_LOW] = option_get_time( "urgency_low", - "timeout", "-lto", defaults.timeouts[LOW], + "timeout", "-lto", defaults.timeouts[URG_LOW], "Timeout for notifications with low urgency" ); - settings.icons[LOW] = option_get_string( + settings.icons[URG_LOW] = option_get_string( "urgency_low", - "icon", "-li", defaults.icons[LOW], + "icon", "-li", defaults.icons[URG_LOW], "Icon for notifications with low urgency" ); @@ -522,15 +522,15 @@ void load_settings(char *cmdline_config_path) "Frame color for notifications with normal urgency" ); - settings.timeouts[NORM] = option_get_time( + settings.timeouts[URG_NORM] = option_get_time( "urgency_normal", - "timeout", "-nto", defaults.timeouts[NORM], + "timeout", "-nto", defaults.timeouts[URG_NORM], "Timeout for notifications with normal urgency" ); - settings.icons[NORM] = option_get_string( + settings.icons[URG_NORM] = option_get_string( "urgency_normal", - "icon", "-ni", defaults.icons[NORM], + "icon", "-ni", defaults.icons[URG_NORM], "Icon for notifications with normal urgency" ); @@ -552,15 +552,15 @@ void load_settings(char *cmdline_config_path) "Frame color for notifications with critical urgency" ); - settings.timeouts[CRIT] = option_get_time( + settings.timeouts[URG_CRIT] = option_get_time( "urgency_critical", - "timeout", "-cto", defaults.timeouts[CRIT], + "timeout", "-cto", defaults.timeouts[URG_CRIT], "Timeout for notifications with critical urgency" ); - settings.icons[CRIT] = option_get_string( + settings.icons[URG_CRIT] = option_get_string( "urgency_critical", - "icon", "-ci", defaults.icons[CRIT], + "icon", "-ci", defaults.icons[URG_CRIT], "Icon for notifications with critical urgency" ); diff --git a/src/x11/x.c b/src/x11/x.c index 0710b67..1b17983 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -25,6 +25,7 @@ #include #include +#include "src/dbus.h" #include "src/dunst.h" #include "src/markup.h" #include "src/notification.h" @@ -856,7 +857,7 @@ gboolean x_mainloop_fd_dispatch(GSource *source, GSourceFunc callback, && settings.close_ks.mask == state) { const GList *displayed = queues_get_displayed(); if (displayed && displayed->data) { - queues_notification_close(displayed->data, 2); + queues_notification_close(displayed->data, REASON_USER); wake_up(); } } @@ -937,7 +938,7 @@ static void x_handle_click(XEvent ev) if (n) { if (ev.xbutton.button == Button1) - queues_notification_close(n, 2); + queues_notification_close(n, REASON_USER); else notification_do_action(n); } @@ -980,26 +981,26 @@ void x_setup(void) x_shortcut_grab(&settings.context_ks); x_shortcut_ungrab(&settings.context_ks); - xctx.color_strings[ColFG][LOW] = settings.lowfgcolor; - xctx.color_strings[ColFG][NORM] = settings.normfgcolor; - xctx.color_strings[ColFG][CRIT] = settings.critfgcolor; + xctx.color_strings[ColFG][URG_LOW] = settings.lowfgcolor; + xctx.color_strings[ColFG][URG_NORM] = settings.normfgcolor; + xctx.color_strings[ColFG][URG_CRIT] = settings.critfgcolor; - xctx.color_strings[ColBG][LOW] = settings.lowbgcolor; - xctx.color_strings[ColBG][NORM] = settings.normbgcolor; - xctx.color_strings[ColBG][CRIT] = settings.critbgcolor; + xctx.color_strings[ColBG][URG_LOW] = settings.lowbgcolor; + xctx.color_strings[ColBG][URG_NORM] = settings.normbgcolor; + xctx.color_strings[ColBG][URG_CRIT] = settings.critbgcolor; if (settings.lowframecolor) - xctx.color_strings[ColFrame][LOW] = settings.lowframecolor; + xctx.color_strings[ColFrame][URG_LOW] = settings.lowframecolor; else - xctx.color_strings[ColFrame][LOW] = settings.frame_color; + xctx.color_strings[ColFrame][URG_LOW] = settings.frame_color; if (settings.normframecolor) - xctx.color_strings[ColFrame][NORM] = settings.normframecolor; + xctx.color_strings[ColFrame][URG_NORM] = settings.normframecolor; else - xctx.color_strings[ColFrame][NORM] = settings.frame_color; + xctx.color_strings[ColFrame][URG_NORM] = settings.frame_color; if (settings.critframecolor) - xctx.color_strings[ColFrame][CRIT] = settings.critframecolor; + xctx.color_strings[ColFrame][URG_CRIT] = settings.critframecolor; else - xctx.color_strings[ColFrame][CRIT] = settings.frame_color; + xctx.color_strings[ColFrame][URG_CRIT] = settings.frame_color; /* parse and set xctx.geometry and monitor position */ if (settings.geom[0] == '-') { diff --git a/test/notification.c b/test/notification.c index e5812dd..c4e3494 100644 --- a/test/notification.c +++ b/test/notification.c @@ -60,11 +60,11 @@ TEST test_notification_is_duplicate(void *notifications) ASSERT(notification_is_duplicate(a, b)); - b->urgency = LOW; + b->urgency = URG_LOW; ASSERT_FALSE(notification_is_duplicate(a, b)); - b->urgency = NORM; + b->urgency = URG_NORM; ASSERT(notification_is_duplicate(a, b)); - b->urgency = CRIT; + b->urgency = URG_CRIT; ASSERT_FALSE(notification_is_duplicate(a, b)); PASS(); @@ -107,7 +107,7 @@ SUITE(suite_notification) a->summary = "Summary"; a->body = "Body"; a->icon = "Icon"; - a->urgency = NORM; + a->urgency = URG_NORM; notification *b = notification_create(); memcpy(b, a, sizeof(*b));