Skip to content

Commit

Permalink
add support for specifying color for each item #1
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbush committed Sep 27, 2020
1 parent 0cead57 commit 1a887c1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
9 changes: 9 additions & 0 deletions xnotify.1
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ The following names are currently accepted for the NAME:VALUE pairs:
.TP
.B IMG:
Specify the path of a image to be displayed on the notification.
.TP
.B BG:
Specify the color of the notification background.
.TP
.B FG:
Specify the color of the notification text (i.e., its foreground).
.TP
.B BRD:
Specify the color of the notification border.
.PP
Clicking on a notification removes it from the screen.
.SH ENVIRONMENT
Expand Down
74 changes: 62 additions & 12 deletions xnotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,14 @@ getoptions(int argc, char *argv[])
}

/* get XftColor from color string */
static void
static int
ealloccolor(const char *s, XftColor *color)
{
if (!XftColorAllocName(dpy, visual, colormap, s, color))
errx(1, "could not allocate color: %s", s);
if (!XftColorAllocName(dpy, visual, colormap, s, color)) {
warnx("could not allocate color: %s", s);
return -1;
}
return 0;
}

/* get number from *s into n, return 1 if error, update s to end of number */
Expand Down Expand Up @@ -651,7 +654,7 @@ drawitem(struct Item *item)
draw = XftDrawCreate(dpy, item->pixmap, visual, colormap);

/* draw background */
XSetForeground(dpy, dc.gc, dc.background.pixel);
XSetForeground(dpy, dc.gc, item->background.pixel);
XFillRectangle(dpy, item->pixmap, dc.gc, 0, 0, item->w, item->h);

/* draw image */
Expand Down Expand Up @@ -683,7 +686,7 @@ drawitem(struct Item *item)
y = (item->h - config.leading_pixels) / 2 - titlefnt.texth;
else
y = (item->h - titlefnt.texth) / 2;
drawtext(&titlefnt, draw, &dc.foreground, x, y, item->title);
drawtext(&titlefnt, draw, &item->foreground, x, y, item->title);

/* draw text body */
if (item->body) {
Expand All @@ -703,9 +706,12 @@ drawitem(struct Item *item)
x = MAX(x, xaligned);
break;
}
drawtext(&bodyfnt, draw, &dc.foreground, x, y, item->body);
drawtext(&bodyfnt, draw, &item->foreground, x, y, item->body);
}

/* change border color */
XSetWindowBorder(dpy, item->win, item->border.pixel);

XftDrawDestroy(draw);
}

Expand All @@ -718,7 +724,7 @@ resettime(struct Item *item)

/* add item notification item and set its window and contents */
static void
additem(const char *title, const char *body, const char *file)
additem(const char *title, const char *body, const char *file, const char *background, const char *foreground, const char *border)
{
struct Item *item;
int titlew, bodyw;
Expand All @@ -737,6 +743,14 @@ additem(const char *title, const char *body, const char *file)
item->prev = tail;
tail = item;

/* allocate colors */
if (!background || ealloccolor(background, &item->background) == -1)
item->background = dc.background;
if (!foreground || ealloccolor(foreground, &item->foreground) == -1)
item->foreground = dc.foreground;
if (!border || ealloccolor(border, &item->border) == -1)
item->border = dc.border;

/* compute notification height */
if (item->body)
h = titlefnt.texth + bodyfnt.texth + config.padding_pixels + config.leading_pixels;
Expand Down Expand Up @@ -792,20 +806,56 @@ delitem(struct Item *item)
change = 1;
}

/* check the type of option given to a notification item */
static enum ItemOption
optiontype(const char *s)
{
if (strncmp(s, "IMG:", 4) == 0)
return IMG;
if (strncmp(s, "BG:", 3) == 0)
return BG;
if (strncmp(s, "FG:", 3) == 0)
return FG;
if (strncmp(s, "BRD:", 4) == 0)
return BRD;
return UNKNOWN;
}

/* read stdin */
static void
parseinput(char *s)
{
char *title, *body, *file;
enum ItemOption option;
char *title, *body, *file, *fg, *bg, *brd;

/* get the title */
title = strtok(s, "\t\n");

/* get the filename */
file = NULL;
if (title != NULL && strncmp(title, "IMG:", 4) == 0) {
file = title + 4;
title = strtok(NULL, "\t\n");
fg = bg = brd = NULL;
while (title && (option = optiontype(title)) != UNKNOWN) {
switch (option) {
case IMG:
file = title + 4;
title = strtok(NULL, "\t\n");
break;
case BG:
bg = title + 3;
title = strtok(NULL, "\t\n");
break;
case FG:
fg = title + 3;
title = strtok(NULL, "\t\n");
break;
case BRD:
brd = title + 4;
title = strtok(NULL, "\t\n");
break;
default:
break;
}

}

/* get the body */
Expand All @@ -817,7 +867,7 @@ parseinput(char *s)
if (!title)
return;

additem(title, body, file);
additem(title, body, file, bg, fg, brd);
}

/* read x events */
Expand Down
5 changes: 5 additions & 0 deletions xnotify.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enum ItemOption {IMG, BG, FG, BRD, UNKNOWN};
enum {DownWards, UpWards};
enum {LeftAlignment, CenterAlignment, RightAlignment};
enum {
Expand Down Expand Up @@ -76,6 +77,10 @@ struct Item {

int w, h;

XftColor background;
XftColor foreground;
XftColor border;

Imlib_Image image;
Drawable pixmap;
Window win;
Expand Down

0 comments on commit 1a887c1

Please sign in to comment.