From 5915cbdd9f533a7b919bb033becdc4fe7220c71d Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Fri, 18 Nov 2011 07:23:33 +0100 Subject: [PATCH] new option to sort messages by order --- README.pod | 8 ++++ config.def.h | 1 + dunst.c | 119 +++++++++++++++++++++++++++++++++------------------ dunst_dbus.c | 3 +- 4 files changed, 88 insertions(+), 43 deletions(-) diff --git a/README.pod b/README.pod index fc5d55d..b7f22c1 100644 --- a/README.pod +++ b/README.pod @@ -60,6 +60,14 @@ defines the format of the message. See FORMAT. show the notification on monitor n. +=item B<-s> + +sort messages by urgency. + +=item B<-ns> + +don't sort messages by urgency. + =item B<-geometry [{width}]x{height}][+/-{x}+/-{y}]> The geometry of the message window. The height is measured diff --git a/config.def.h b/config.def.h index 7a125c8..d2762b4 100644 --- a/config.def.h +++ b/config.def.h @@ -19,6 +19,7 @@ const char *lowfgcolor = "#000000"; const char *format = "%s %b"; /* default format */ int timeouts[] = { 10, 10, 0 }; /* low, normal, critical */ const char *geom = "0x3-30+20"; /* geometry */ +int sort = True; /* sort messages by urgency */ /* const char *geom = "x1"; */ diff --git a/dunst.c b/dunst.c index ec9350b..4863281 100644 --- a/dunst.c +++ b/dunst.c @@ -51,7 +51,7 @@ static dimension_t geometry; static int font_h; /* list functions */ -msg_queue_t *append(msg_queue_t *queue, msg_queue_t *msg); +msg_queue_t *add(msg_queue_t *queue, msg_queue_t *msg); msg_queue_t *delete(msg_queue_t *elem); int list_len(msg_queue_t *list); @@ -66,6 +66,7 @@ void dunst_printf(int level, const char *fmt, ...); char *fix_markup(char *str); void free_msgqueue_t(msg_queue_t *elem); void handleXEvents(void); +void initmsg(msg_queue_t *msg); char *string_replace(const char *needle, const char *replacement, char *haystack); void run(void); void setup(void); @@ -74,53 +75,40 @@ void usage(int exit_status); #include "dunst_dbus.h" -static void -_set_color(msg_queue_t *msg, int color_idx) { - Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); - XColor color; - if(msg->color_strings[color_idx] == NULL - || !XAllocNamedColor(dc->dpy, cmap, - msg->color_strings[color_idx], &color, &color)) { - msg->colors[color_idx] = colors[msg->urgency][color_idx]; - } else { - msg->colors[color_idx] = color.pixel; - } -} msg_queue_t* -append(msg_queue_t *queue, msg_queue_t *new) { - msg_queue_t *last; - - - new->format = format; - apply_rules(new); - - new->msg = string_replace("%a", new->appname, strdup(new->format)); - new->msg = string_replace("%s", new->summary, new->msg); - new->msg = string_replace("%i", new->icon, new->msg); - new->msg = string_replace("%I", basename(new->icon), new->msg); - new->msg = string_replace("%b", new->body, new->msg); - - new->msg = fix_markup(new->msg); - /* urgency > CRIT -> array out of range */ - new->urgency = new->urgency > CRIT ? CRIT : new->urgency; - - _set_color(new, ColFG); - _set_color(new, ColBG); - - new->timeout = new->timeout == -1 ? timeouts[new->urgency] : new->timeout; - new->start = 0; - - dunst_printf(MSG, "%s\n", new->msg); - dunst_printf(INFO, "{\n appname: %s\n summary: %s\n body: %s\n icon: %s\n urgency: %d\n timeout: %d\n}", - new->appname, new->summary, new->body, new->icon, new->urgency, new->timeout); +add(msg_queue_t *queue, msg_queue_t *new) { + msg_queue_t *i; + msg_queue_t *prev = NULL; new->next = NULL; if(queue == NULL) { return new; } - for(last = queue; last->next; last = last->next); - last->next = new; + + if(sort) { + for(i = queue; i->next; i = i->next) { + if(new->urgency > i->urgency) { + if (!prev) { + /* we are at the first element */ + queue = new; + new->next = i; + return queue; + } else { + prev->next = new; + new->next = i; + return queue; + } + } + prev = i; + } + /* we've reached the end of the queue */ + i->next = new; + new->next = NULL; + } else { + for(i = queue; i->next; i = i->next); + i->next = new; + } return queue; } @@ -424,6 +412,46 @@ handleXEvents(void) { } } +static void +_set_color(msg_queue_t *msg, int color_idx) { + Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); + XColor color; + if(msg->color_strings[color_idx] == NULL + || !XAllocNamedColor(dc->dpy, cmap, + msg->color_strings[color_idx], &color, &color)) { + msg->colors[color_idx] = colors[msg->urgency][color_idx]; + } else { + msg->colors[color_idx] = color.pixel; + } +} + +void +initmsg(msg_queue_t *msg) { + msg->format = format; + apply_rules(msg); + + msg->msg = string_replace("%a", msg->appname, strdup(msg->format)); + msg->msg = string_replace("%s", msg->summary, msg->msg); + msg->msg = string_replace("%i", msg->icon, msg->msg); + msg->msg = string_replace("%I", basename(msg->icon), msg->msg); + msg->msg = string_replace("%b", msg->body, msg->msg); + + msg->msg = fix_markup(msg->msg); + /* urgency > CRIT -> array out of range */ + msg->urgency = msg->urgency > CRIT ? CRIT : msg->urgency; + + _set_color(msg, ColFG); + _set_color(msg, ColBG); + + msg->timeout = msg->timeout == -1 ? timeouts[msg->urgency] : msg->timeout; + msg->start = 0; + + dunst_printf(MSG, "%s\n", msg->msg); + dunst_printf(INFO, "{\n appname: %s\n summary: %s\n body: %s\n icon: %s\n urgency: %d\n timeout: %d\n}", + msg->appname, msg->summary, msg->body, msg->icon, msg->urgency, msg->timeout); + +} + char * string_replace(const char *needle, const char *replacement, char *haystack) { char *tmp, *start; @@ -572,13 +600,14 @@ main(int argc, char *argv[]) { {"key", required_argument, NULL, 'k'}, {"geometry", required_argument, NULL, 'g'}, {"mod", required_argument, NULL, 'M'}, + {"ns", no_argument, NULL, 'x'}, {0,0,0,0} }; int option_index = 0; - c = getopt_long_only(argc, argv, "bhv", long_options, &option_index); + c = getopt_long_only(argc, argv, "bhsv", long_options, &option_index); if(c == -1) { break; @@ -659,6 +688,12 @@ main(int argc, char *argv[]) { fprintf(stderr, "See manpage for list of available masks\n"); } break; + case 's': + sort = True; + break; + case 'x': + sort = False; + break; case 'v': verbosity++; break; diff --git a/dunst_dbus.c b/dunst_dbus.c index bd269a1..65a9324 100644 --- a/dunst_dbus.c +++ b/dunst_dbus.c @@ -274,7 +274,8 @@ notify(DBusMessage *dmsg) { } msg->color_strings[ColFG] = fgcolor == NULL ? NULL : strdup(fgcolor); msg->color_strings[ColBG] = bgcolor == NULL ? NULL : strdup(bgcolor); - msgqueue = append(msgqueue, msg); + initmsg(msg); + msgqueue = add(msgqueue, msg); drawmsg(); reply = dbus_message_new_method_return(dmsg);