refactor/cleanup

* run code through `indent --no-tabs -linux`
 * use generic list implemenation for queues

This sould have been multiple commits but some git foobar on my part
made me loose the history :-/
This commit is contained in:
Sascha Kruse 2012-06-21 17:40:44 +02:00
parent 305a791e21
commit b0bf0b0b07
7 changed files with 1544 additions and 1498 deletions

View File

@ -3,7 +3,7 @@
include config.mk
SRC = draw.c dunst.c ini.c
SRC = draw.c dunst.c list.c dunst_dbus.c ini.c
OBJ = ${SRC:.c=.o}
all: doc options dunst
@ -20,9 +20,9 @@ options:
${OBJ}: config.mk
dunst: draw.o dunst.o ini.o
dunst: draw.o dunst.o list.o dunst_dbus.o ini.o
@echo CC -o $@
@${CC} ${CFLAGS} -o $@ dunst.o draw.o ini.o ${LDFLAGS}
@${CC} ${CFLAGS} -o $@ dunst.o draw.o list.o dunst_dbus.o ini.o ${LDFLAGS}
clean:
@echo cleaning

115
draw.c
View File

@ -42,21 +42,26 @@ DEALINGS IN THE SOFTWARE.
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void
drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
drawrect(DC * dc, int x, int y, unsigned int w, unsigned int h, Bool fill,
unsigned long color)
{
XSetForeground(dc->dpy, dc->gc, color);
if (fill)
XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h);
XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x,
dc->y + y, w, h);
else
XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1);
XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x,
dc->y + y, w - 1, h - 1);
}
void
drawtext(DC *dc, const char *text, ColorSet *col) {
void drawtext(DC * dc, const char *text, ColorSet * col)
{
char buf[BUFSIZ];
size_t mn, n = strlen(text);
/* shorten text if necessary */
for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--)
for (mn = MIN(n, sizeof buf);
textnw(dc, text, mn) + dc->font.height / 2 > dc->w; mn--)
if (mn == 0)
return;
memcpy(buf, text, mn);
@ -67,8 +72,8 @@ drawtext(DC *dc, const char *text, ColorSet *col) {
drawtextn(dc, buf, mn, col);
}
void
drawtextn(DC *dc, const char *text, size_t n, ColorSet *col) {
void drawtextn(DC * dc, const char *text, size_t n, ColorSet * col)
{
int x = dc->x + dc->font.height / 2;
int y = dc->y + dc->font.ascent + 1;
@ -77,18 +82,20 @@ drawtextn(DC *dc, const char *text, size_t n, ColorSet *col) {
if (!dc->xftdraw)
eprintf("error, xft drawable does not exist");
XftDrawStringUtf8(dc->xftdraw, &col->FG_xft,
dc->font.xft_font, x, y, (unsigned char*)text, n);
dc->font.xft_font, x, y,
(unsigned char *)text, n);
} else if (dc->font.set) {
printf("XmbDrawString\n");
XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y,
text, n);
} else {
XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
}
}
void
eprintf(const char *fmt, ...) {
void eprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@ -102,18 +109,22 @@ eprintf(const char *fmt, ...) {
exit(EXIT_FAILURE);
}
void
freecol(DC *dc, ColorSet *col) {
void freecol(DC * dc, ColorSet * col)
{
if (col) {
if (&col->FG_xft)
XftColorFree(dc->dpy, DefaultVisual(dc->dpy, DefaultScreen(dc->dpy)),
DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)), &col->FG_xft);
XftColorFree(dc->dpy,
DefaultVisual(dc->dpy,
DefaultScreen(dc->dpy)),
DefaultColormap(dc->dpy,
DefaultScreen(dc->dpy)),
&col->FG_xft);
free(col);
}
}
void
freedc(DC *dc) {
void freedc(DC * dc)
{
if (dc->font.xft_font) {
XftFontClose(dc->dpy, dc->font.xft_font);
XftDrawDestroy(dc->xftdraw);
@ -132,8 +143,8 @@ freedc(DC *dc) {
free(dc);
}
unsigned long
getcolor(DC *dc, const char *colstr) {
unsigned long getcolor(DC * dc, const char *colstr)
{
Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
XColor color;
@ -142,22 +153,25 @@ getcolor(DC *dc, const char *colstr) {
return color.pixel;
}
ColorSet *
initcolor(DC *dc, const char * foreground, const char * background) {
ColorSet *initcolor(DC * dc, const char *foreground, const char *background)
{
ColorSet *col = (ColorSet *) malloc(sizeof(ColorSet));
if (!col)
eprintf("error, cannot allocate memory for color set");
col->BG = getcolor(dc, background);
col->FG = getcolor(dc, foreground);
if (dc->font.xft_font)
if(!XftColorAllocName(dc->dpy, DefaultVisual(dc->dpy, DefaultScreen(dc->dpy)),
DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)), foreground, &col->FG_xft))
eprintf("error, cannot allocate xft font color '%s'\n", foreground);
if (!XftColorAllocName
(dc->dpy, DefaultVisual(dc->dpy, DefaultScreen(dc->dpy)),
DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)),
foreground, &col->FG_xft))
eprintf("error, cannot allocate xft font color '%s'\n",
foreground);
return col;
}
DC *
initdc(void) {
DC *initdc(void)
{
DC *dc;
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
@ -172,8 +186,8 @@ initdc(void) {
return dc;
}
void
initfont(DC *dc, const char *fontstr) {
void initfont(DC * dc, const char *fontstr)
{
char *def, **missing, **names;
int i, n;
XFontStruct **xfonts;
@ -183,14 +197,21 @@ initfont(DC *dc, const char *fontstr) {
dc->font.ascent = dc->font.xfont->ascent;
dc->font.descent = dc->font.xfont->descent;
dc->font.width = dc->font.xfont->max_bounds.width;
} else if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
} else
if ((dc->font.set =
XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
for (i = 0; i < n; i++) {
dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width);
dc->font.ascent =
MAX(dc->font.ascent, xfonts[i]->ascent);
dc->font.descent =
MAX(dc->font.descent, xfonts[i]->descent);
dc->font.width =
MAX(dc->font.width, xfonts[i]->max_bounds.width);
}
} else if((dc->font.xft_font = XftFontOpenName(dc->dpy, DefaultScreen(dc->dpy), fontstr))) {
} else
if ((dc->font.xft_font =
XftFontOpenName(dc->dpy, DefaultScreen(dc->dpy), fontstr))) {
dc->font.ascent = dc->font.xft_font->ascent;
dc->font.descent = dc->font.xft_font->descent;
dc->font.width = dc->font.xft_font->max_advance_width;
@ -203,13 +224,13 @@ initfont(DC *dc, const char *fontstr) {
return;
}
void
mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
void mapdc(DC * dc, Window win, unsigned int w, unsigned int h)
{
XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
}
void
resizedc(DC *dc, unsigned int w, unsigned int h) {
void resizedc(DC * dc, unsigned int w, unsigned int h)
{
int screen = DefaultScreen(dc->dpy);
if (dc->canvas)
XFreePixmap(dc->dpy, dc->canvas);
@ -222,17 +243,21 @@ resizedc(DC *dc, unsigned int w, unsigned int h) {
XftDrawDestroy(dc->xftdraw);
}
if (dc->font.xft_font) {
dc->xftdraw = XftDrawCreate(dc->dpy, dc->canvas, DefaultVisual(dc->dpy,screen), DefaultColormap(dc->dpy,screen));
dc->xftdraw =
XftDrawCreate(dc->dpy, dc->canvas,
DefaultVisual(dc->dpy, screen),
DefaultColormap(dc->dpy, screen));
if (!(dc->xftdraw))
eprintf("error, cannot create xft drawable\n");
}
}
int
textnw(DC *dc, const char *text, size_t len) {
int textnw(DC * dc, const char *text, size_t len)
{
if (dc->font.xft_font) {
XGlyphInfo gi;
XftTextExtentsUtf8(dc->dpy, dc->font.xft_font, (const FcChar8*)text, len, &gi);
XftTextExtentsUtf8(dc->dpy, dc->font.xft_font,
(const FcChar8 *)text, len, &gi);
return gi.width;
} else if (dc->font.set) {
XRectangle r;
@ -242,7 +267,9 @@ textnw(DC *dc, const char *text, size_t len) {
return XTextWidth(dc->font.xfont, text, len);
}
int
textw(DC *dc, const char *text) {
int textw(DC * dc, const char *text)
{
return textnw(dc, text, strlen(text)) + dc->font.height;
}
/* vim: set ts=8 sw=8 tw=0: */

4
draw.h
View File

@ -59,7 +59,8 @@ typedef struct {
unsigned long BG;
} ColorSet;
void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color);
void drawrect(DC * dc, int x, int y, unsigned int w, unsigned int h, Bool fill,
unsigned long color);
void drawtext(DC * dc, const char *text, ColorSet * col);
void drawtextn(DC * dc, const char *text, size_t n, ColorSet * col);
void freecol(DC * dc, ColorSet * col);
@ -75,3 +76,4 @@ int textnw(DC *dc, const char *text, size_t len);
int textw(DC * dc, const char *text);
#endif
/* vim: set ts=8 sw=8 tw=0: */

741
dunst.c

File diff suppressed because it is too large Load Diff

11
dunst.h
View File

@ -31,20 +31,21 @@ typedef struct _rule_t {
struct _rule_t *next;
} rule_t;
typedef struct _msg_queue_t {
typedef struct _notification {
char *appname;
char *summary;
char *body;
char *icon;
char *msg;
const char *format;
struct _msg_queue_t *next;
time_t start;
time_t timestamp;
int timeout;
int urgency;
int redisplayed; /* has been displayed before? */
ColorSet *colors;
char *color_strings[2];
} msg_queue_t;
} notification;
typedef struct _dimension_t {
int x;
@ -54,3 +55,7 @@ typedef struct _dimension_t {
int mask;
} dimension_t;
#endif
/* vim: set ts=8 sw=8 tw=0: */
void init_notification(notification * n);
void map_win(void);

View File

@ -3,16 +3,15 @@
#include <dbus/dbus.h>
#include "dunst.h"
#include "list.h"
#define DBUS_POLL_TIMEOUT 200
#include "dunst_dbus.h"
DBusError dbus_err;
DBusConnection *dbus_conn;
dbus_uint32_t dbus_serial = 0;
static const char *introspect = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<node name=\"/org/freedesktop/Notifications\">"
" <interface name=\"org.freedesktop.Notifications\">"
" "
@ -51,11 +50,10 @@ static const char *introspect = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
" </interface>"
" "
" <interface name=\"org.xfce.Notifyd\">"
" <method name=\"Quit\"/>"
" </interface>"
"</node>";
" <method name=\"Quit\"/>" " </interface>" "</node>";
void dbus_introspect(DBusMessage * dmsg) {
void dbus_introspect(DBusMessage * dmsg)
{
DBusMessage *reply;
DBusMessageIter args;
@ -69,8 +67,8 @@ void dbus_introspect(DBusMessage * dmsg) {
}
void
initdbus(void) {
void initdbus(void)
{
int ret;
dbus_error_init(&dbus_err);
dbus_conn = dbus_bus_get(DBUS_BUS_SESSION, &dbus_err);
@ -89,7 +87,8 @@ initdbus(void) {
fprintf(stderr, "Name Error (%s)\n", dbus_err.message);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
fprintf(stderr, "There's already another notification-daemon running\n");
fprintf(stderr,
"There's already another notification-daemon running\n");
exit(EXIT_FAILURE);
}
@ -102,18 +101,11 @@ initdbus(void) {
}
}
void
dbus_poll(void) {
void dbus_poll(int timeout)
{
DBusMessage *dbus_msg;
/* make timeout smaller if we are displaying a message
* to improve responsivness for mouse clicks
*/
if(msgqueue == NULL) {
dbus_connection_read_write(dbus_conn, DBUS_POLL_TIMEOUT);
} else {
dbus_connection_read_write(dbus_conn, 100);
}
dbus_connection_read_write(dbus_conn, timeout);
dbus_msg = dbus_connection_pop_message(dbus_conn);
/* we don't have a new message */
@ -121,31 +113,36 @@ dbus_poll(void) {
return;
}
if (dbus_message_is_method_call(dbus_msg,"org.freedesktop.DBus.Introspectable", "Introspect")) {
if (dbus_message_is_method_call
(dbus_msg, "org.freedesktop.DBus.Introspectable", "Introspect")) {
dbus_introspect(dbus_msg);
}
if (dbus_message_is_method_call(dbus_msg,
"org.freedesktop.Notifications","Notify")) {
"org.freedesktop.Notifications",
"Notify")) {
notify(dbus_msg);
}
if (dbus_message_is_method_call(dbus_msg,
"org.freedesktop.Notifications", "GetCapabilities")) {
"org.freedesktop.Notifications",
"GetCapabilities")) {
getCapabilities(dbus_msg);
}
if (dbus_message_is_method_call(dbus_msg,
"org.freedesktop.Notifications", "GetServerInformation")) {
"org.freedesktop.Notifications",
"GetServerInformation")) {
getServerInformation(dbus_msg);
}
if (dbus_message_is_method_call(dbus_msg,
"org.freedesktop.Notifications", "CloseNotification")) {
"org.freedesktop.Notifications",
"CloseNotification")) {
closeNotification(dbus_msg);
}
dbus_message_unref(dbus_msg);
}
void
getCapabilities(DBusMessage *dmsg) {
void getCapabilities(DBusMessage * dmsg)
{
DBusMessage *reply;
DBusMessageIter args;
DBusMessageIter subargs;
@ -160,7 +157,8 @@ getCapabilities(DBusMessage *dmsg) {
dbus_message_iter_init_append(reply, &args);
if(!dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &subargs )
if (!dbus_message_iter_open_container
(&args, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &subargs)
|| !dbus_message_iter_append_basic(&subargs, DBUS_TYPE_STRING, caps)
|| !dbus_message_iter_close_container(&args, &subargs)
|| !dbus_connection_send(dbus_conn, reply, &dbus_serial)) {
@ -172,8 +170,8 @@ getCapabilities(DBusMessage *dmsg) {
dbus_message_unref(reply);
}
void
closeNotification(DBusMessage *dmsg) {
void closeNotification(DBusMessage * dmsg)
{
DBusMessage *reply;
reply = dbus_message_new_method_return(dmsg);
if (!reply) {
@ -183,31 +181,29 @@ closeNotification(DBusMessage *dmsg) {
dbus_connection_flush(dbus_conn);
}
void
getServerInformation(DBusMessage *dmsg) {
void getServerInformation(DBusMessage * dmsg)
{
DBusMessage *reply;
DBusMessageIter args;
char *param = "";
const char *info[4] = { "dunst", "dunst", "2011", "2011" };
if (!dbus_message_iter_init(dmsg, &args)) {
}
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) {
}
else {
} else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) {
} else {
dbus_message_iter_get_basic(&args, &param);
}
reply = dbus_message_new_method_return(dmsg);
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[0])
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[1])
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[2])
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &info[3])) {
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
&info[1])
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
&info[2])
|| !dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
&info[3])) {
fprintf(stderr, "Unable to fill arguments");
return;
}
@ -222,26 +218,22 @@ getServerInformation(DBusMessage *dmsg) {
dbus_message_unref(reply);
}
static void
_extract_basic(int type, DBusMessageIter *iter, void *target) {
static void _extract_basic(int type, DBusMessageIter * iter, void *target)
{
int iter_type = dbus_message_iter_get_arg_type(iter);
if (iter_type != type) {
dunst_printf(DEBUG, "Invalid dbus notification: expected type %d but got %d.\n",
type, iter_type);
} else {
if (iter_type == type) {
dbus_message_iter_get_basic(iter, target);
}
}
static void
_extract_hint(const char *name, const char *hint_name,
DBusMessageIter *hint, void *target) {
DBusMessageIter * hint, void *target)
{
DBusMessageIter hint_value;
if (!strcmp(hint_name, name)) {
dunst_printf(DEBUG, "%s found\n", name);
dbus_message_iter_next(hint);
dbus_message_iter_recurse(hint, &hint_value);
do {
@ -250,8 +242,8 @@ _extract_hint(const char *name, const char *hint_name,
}
}
void
notify(DBusMessage *dmsg) {
void notify(DBusMessage * dmsg)
{
DBusMessage *reply;
DBusMessageIter args;
DBusMessageIter hints;
@ -267,49 +259,41 @@ notify(DBusMessage *dmsg) {
const char *fgcolor = NULL;
const char *bgcolor = NULL;
int urgency = 1;
msg_queue_t *msg = malloc(sizeof(msg_queue_t));
notification *n = malloc(sizeof(notification));
dbus_uint32_t nid = 0;
dbus_int32_t expires = -1;
dbus_serial++;
dunst_printf(DEBUG, "new dbus message\n");
dbus_message_iter_init(dmsg, &args);
dunst_printf(DEBUG, "extracting appname\n");
_extract_basic(DBUS_TYPE_STRING, &args, &appname);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting nid\n");
_extract_basic(DBUS_TYPE_UINT32, &args, &nid);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting icon\n");
_extract_basic(DBUS_TYPE_STRING, &args, &icon);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting summary\n");
_extract_basic(DBUS_TYPE_STRING, &args, &summary);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting body\n");
_extract_basic(DBUS_TYPE_STRING, &args, &body);
dbus_message_iter_next(&args);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting hints\n");
dbus_message_iter_recurse(&args, &hints);
dbus_message_iter_next(&args);
dunst_printf(DEBUG, "extracting expires\n");
_extract_basic(DBUS_TYPE_INT32, &args, &expires);
dunst_printf(DEBUG, "extracting hints\n");
while (dbus_message_iter_get_arg_type(&hints) != DBUS_TYPE_INVALID) {
dbus_message_iter_recurse(&hints, &hint);
while (dbus_message_iter_get_arg_type(&hint) != DBUS_TYPE_INVALID) {
if(dbus_message_iter_get_arg_type(&hint) != DBUS_TYPE_STRING) {
while (dbus_message_iter_get_arg_type(&hint) !=
DBUS_TYPE_INVALID) {
if (dbus_message_iter_get_arg_type(&hint) !=
DBUS_TYPE_STRING) {
dbus_message_iter_next(&hint);
continue;
}
@ -322,7 +306,6 @@ notify(DBusMessage *dmsg) {
dbus_message_iter_next(&hints);
}
if (expires > 0) {
/* do some rounding */
expires = (expires + 500) / 1000;
@ -330,20 +313,19 @@ notify(DBusMessage *dmsg) {
expires = 1;
}
}
msg->appname = strdup(appname);
msg->summary = strdup(summary);
msg->body = strdup(body);
msg->icon = strdup(icon);
msg->timeout = expires;
msg->urgency = urgency;
n->appname = strdup(appname);
n->summary = strdup(summary);
n->body = strdup(body);
n->icon = strdup(icon);
n->timeout = expires;
n->urgency = urgency;
for (i = 0; i < ColLast; i++) {
msg->color_strings[i] = NULL;
n->color_strings[i] = NULL;
}
msg->color_strings[ColFG] = fgcolor == NULL ? NULL : strdup(fgcolor);
msg->color_strings[ColBG] = bgcolor == NULL ? NULL : strdup(bgcolor);
initmsg(msg);
msgqueue = add(msgqueue, msg);
drawmsg();
n->color_strings[ColFG] = fgcolor == NULL ? NULL : strdup(fgcolor);
n->color_strings[ColBG] = bgcolor == NULL ? NULL : strdup(bgcolor);
init_notification(n);
map_win();
reply = dbus_message_new_method_return(dmsg);
@ -353,3 +335,5 @@ notify(DBusMessage *dmsg) {
dbus_message_unref(reply);
}
/* vim: set ts=8 sw=8 tw=0: */

View File

@ -1,12 +1,17 @@
/* copyright 2012 Sascha Kruse and contributors (see LICENSE for licensing information) */
#ifndef _DUNST_DBUS_H
#define _DUNST_DBUS_H
#include <dbus/dbus.h>
void initdbus(void);
void dbus_poll(void);
void dbus_poll(int timeout);
void notify(DBusMessage * msg);
void getCapabilities(DBusMessage * dmsg);
void closeNotification(DBusMessage * dmsg);
void getServerInformation(DBusMessage * dmsg);
#include "dunst_dbus.c"
#endif
/* vim: set ts=8 sw=8 tw=0: */