From ea702f0dad20b7bb52234a1e088216067bb74300 Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Mon, 14 Nov 2011 20:48:57 +0100 Subject: [PATCH] refactor handling of the msgqueue This should make things cleaner and easier to add filters in the future" --- draw.h | 4 +++ dunst.c | 83 +++++++++++++++++++++++++++++++--------------------- dunst.h | 20 +++++++++++++ dunst_dbus.c | 26 +++++++++------- 4 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 dunst.h diff --git a/draw.h b/draw.h index 43a57bf..a69efd4 100644 --- a/draw.h +++ b/draw.h @@ -1,4 +1,7 @@ /* See LICENSE file for copyright and license details. */ +#ifndef DRAW_H +#define DRAW_H + #define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG]) #define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG]) @@ -33,3 +36,4 @@ void mapdc(DC *dc, Window win, unsigned int w, unsigned int h); void resizedc(DC *dc, unsigned int w, unsigned int h); int textnw(DC *dc, const char *text, size_t len); int textw(DC *dc, const char *text); +#endif diff --git a/dunst.c b/dunst.c index 65fbd41..14d1f8c 100644 --- a/dunst.c +++ b/dunst.c @@ -13,6 +13,7 @@ #include #endif +#include "dunst.h" #include "draw.h" #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh)) @@ -25,14 +26,6 @@ #define CRIT 2 /* structs */ -typedef struct _msg_queue_t { - char *msg; - struct _msg_queue_t *next; - time_t start; - int timeout; - int urgency; - unsigned long colors[ColLast]; -} msg_queue_t; typedef struct _dimension_t { int x; @@ -47,6 +40,7 @@ typedef struct _screen_info { dimension_t dim; } screen_info; + /* global variables */ static const char *font = NULL; static const char *normbgcolor = "#cccccc"; @@ -73,7 +67,7 @@ static const char *format = "%s %b"; static int verbose = False; /* list functions */ -msg_queue_t *append(msg_queue_t *queue, char *msg, int to, int urgency, const char *fg, const char *bg); +msg_queue_t *append(msg_queue_t *queue, msg_queue_t *msg); msg_queue_t *delete(msg_queue_t *elem); msg_queue_t *pop(msg_queue_t *queue); int list_len(msg_queue_t *list); @@ -86,7 +80,7 @@ void delete_msg(msg_queue_t *elem); void drawmsg(void); void dunst_printf(const char *fmt, ...); char *fix_markup(char *str); -char *format_msg(const char *app, const char *sum, const char *body, const char *icon); +void free_msgqueue_t(msg_queue_t *elem); void handleXEvents(void); char *string_replace(const char *needle, const char *replacement, char *haystack); void run(void); @@ -96,33 +90,41 @@ void usage(int exit_status); #include "dunst_dbus.h" -msg_queue_t* -append(msg_queue_t *queue, char *msg, int to, int urgency, const char *fg, const char *bg) { - msg_queue_t *new = malloc(sizeof(msg_queue_t)); - msg_queue_t *last; +static void +_set_color(msg_queue_t *msg, int color_idx) { Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); XColor color; - - - new->msg = fix_markup(msg); - new->urgency = urgency > CRIT ? CRIT : urgency; - - if(fg == NULL || !XAllocNamedColor(dc->dpy, cmap, fg, &color, &color)) { - new->colors[ColFG] = colors[new->urgency][ColFG]; + 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 { - new->colors[ColFG] = color.pixel; + msg->colors[color_idx] = color.pixel; } +} - if(bg == NULL || !XAllocNamedColor(dc->dpy, cmap, bg, &color, &color)) { - new->colors[ColBG] = colors[new->urgency][ColBG]; - } else { - new->colors[ColBG] = color.pixel; - } +msg_queue_t* +append(msg_queue_t *queue, msg_queue_t *new) { + msg_queue_t *last; - new->timeout = to == -1 ? timeouts[urgency] : to; + new->msg = string_replace("%a", new->appname, strdup(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("%s (timeout: %d, urgency: %d)\n", new->msg, new->timeout, urgency); + + dunst_printf("%s (timeout: %d, urgency: %d)\n", new->msg, new->timeout, new->urgency); new->next = NULL; if(queue == NULL) { return new; @@ -130,8 +132,10 @@ append(msg_queue_t *queue, char *msg, int to, int urgency, const char *fg, const for(last = queue; last->next; last = last->next); last->next = new; return queue; + } + msg_queue_t* delete(msg_queue_t *elem) { msg_queue_t *prev; @@ -142,8 +146,7 @@ delete(msg_queue_t *elem) { if(elem == msgqueue) { next = elem->next; - free(elem->msg); - free(elem); + free_msgqueue_t(elem); return next; } @@ -159,8 +162,7 @@ delete(msg_queue_t *elem) { } prev->next = next; - free(elem->msg); - free(elem); + free_msgqueue_t(elem); return msgqueue; } @@ -363,6 +365,21 @@ char } +void +free_msgqueue_t(msg_queue_t *elem) { + int i; + free(elem->appname); + free(elem->summary); + free(elem->body); + free(elem->msg); + for(i = 0; i < ColLast; i++) { + if(elem->color_strings[i] != NULL) { + free(elem->color_strings[i]); + } + } + free(elem); +} + void handleXEvents(void) { XEvent ev; diff --git a/dunst.h b/dunst.h new file mode 100644 index 0000000..a3d603c --- /dev/null +++ b/dunst.h @@ -0,0 +1,20 @@ +#ifndef DUNST_H +#define DUNST_H + +#include "draw.h" + +typedef struct _msg_queue_t { + char *appname; + char *summary; + char *body; + char *icon; + char *msg; + struct _msg_queue_t *next; + time_t start; + int timeout; + int urgency; + unsigned long colors[ColLast]; + char *color_strings[ColLast]; +} msg_queue_t; +#endif + diff --git a/dunst_dbus.c b/dunst_dbus.c index 0cde026..c7a613e 100644 --- a/dunst_dbus.c +++ b/dunst_dbus.c @@ -1,5 +1,7 @@ #include +#include "dunst.h" + #define DBUS_POLL_TIMEOUT 1000 DBusError dbus_err; @@ -166,6 +168,7 @@ notify(DBusMessage *dmsg) { DBusMessageIter hint_value; char *hint_name; + int i; int id = 23; const char *appname; const char *summary; @@ -174,7 +177,7 @@ notify(DBusMessage *dmsg) { const char *fgcolor = NULL; const char *bgcolor = NULL; int urgency = 1; - char *msg; + msg_queue_t *msg = malloc(sizeof(msg_queue_t)); dbus_uint32_t nid=0; dbus_int32_t expires=-1; @@ -233,14 +236,6 @@ notify(DBusMessage *dmsg) { - - msg = string_replace("%a", appname, strdup(format)); - msg = string_replace("%s", summary, msg); - msg = string_replace("%i", icon, msg); - msg = string_replace("%I", basename(icon), msg); - msg = string_replace("%b", body, msg); - - if(expires > 0) { /* do some rounding */ expires = (expires+500)/1000; @@ -248,7 +243,18 @@ notify(DBusMessage *dmsg) { expires = 1; } } - msgqueue = append(msgqueue, msg, expires, urgency, fgcolor, bgcolor); + msg->appname = strdup(appname); + msg->summary = strdup(summary); + msg->body = strdup(body); + msg->icon = strdup(icon); + msg->timeout = expires; + msg->urgency = urgency; + for(i = 0; i < ColLast; i++) { + msg->color_strings[i] = NULL; + } + msg->color_strings[ColFG] = fgcolor == NULL ? NULL : strdup(fgcolor); + msg->color_strings[ColBG] = bgcolor == NULL ? NULL : strdup(bgcolor); + msgqueue = append(msgqueue, msg); drawmsg(); reply = dbus_message_new_method_return(dmsg);