-
Notifications
You must be signed in to change notification settings - Fork 12
/
xnotify.c
1505 lines (1387 loc) · 33.2 KB
/
xnotify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/extensions/Xinerama.h>
#include <X11/extensions/Xrender.h>
#include <Imlib2.h>
#include "ctrlfnt.h"
#define APP_CLASS "XNotify"
#define APP_NAME "xnotify"
#define DEFWIDTH 350 /* default width of a notification */
#define MAXLINES 128 /* maximum number of unwrapped lines */
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)>(y)?(x):(y))
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define RED(v) ((((v) & 0xFF0000) >> 8) | (((v) & 0xFF0000) >> 16))
#define GREEN(v) ((((v) & 0x00FF00) ) | (((v) & 0x00FF00) >> 8))
#define BLUE(v) ((((v) & 0x0000FF) << 8) | (((v) & 0x0000FF) ))
#define ATOMS \
X(UTF8_STRING) \
X(_NET_WM_NAME) \
X(_NET_WM_WINDOW_TYPE) \
X(_NET_WM_WINDOW_TYPE_NOTIFICATION) \
X(_NET_WM_STATE) \
X(_NET_WM_STATE_ABOVE)
#define RESOURCES \
/* ENUM CLASS NAME DEFAULT */\
X(RES_ALIGNMENT, "Alignment", "alignment", LeftAlignment )\
X(RES_BACKGROUND, "Background", "background", 0x000000 )\
X(RES_BORDERCLR, "BorderColor", "borderColor", 0x3465A4 )\
X(RES_BORDERWID, "BorderWidth", "borderWidth", 1 )\
X(RES_FACENAME, "FaceName", "faceName", 0 )\
X(RES_FOREGROUND, "Foreground", "foreground", 0xFFFFFF )\
X(RES_GAP, "Gap", "gap", 7 )\
X(RES_GEOMETRY, "Geometry", "geometry", 0 )\
X(RES_GRAVITY, "Gravity", "gravity", NorthEastGravity)\
X(RES_IMAGEWID, "ImageWidth", "imageWidth", 80 )\
X(RES_LEADING, "Leading", "leading", 5 )\
X(RES_MAXHEIGHT, "MaxHeight", "maxHeight", 300 )\
X(RES_OPACITY, "Opacity", "opacity", 0xFFFF )\
X(RES_PADDING, "Padding", "padding", 10 )\
X(RES_SHRINK, "Shrink", "shrink", 0 )\
enum ItemOption {IMG, BG, FG, BRD, TAG, CMD, SEC, BAR, UNKNOWN};
enum {DownWards, UpWards};
enum {LeftAlignment, CenterAlignment, RightAlignment};
enum {
SIGNAL_NONE = 0,
SIGNAL_KILL = 1,
SIGNAL_CMD = 2,
SIGNAL_KILLALL = 3,
};
enum Atom {
#define X(atom) atom,
ATOMS
NATOMS
#undef X
};
enum Resource {
#define X(resource, class, name, value) resource,
RESOURCES
NRESOURCES
#undef X
};
typedef struct {
XrmClass class;
XrmName name;
} Resource;
/* monitor geometry structure */
struct Monitor {
int num;
int x, y, w, h;
};
/* notification item specification structure */
struct Itemspec {
char *firstline;
char *otherlines;
char *file;
char *background;
char *foreground;
char *border;
char *tag;
char *cmd;
int bar;
int sec;
};
/* notification item structure */
struct Item {
struct Item *prev, *next;
int nlines;
char *line[MAXLINES];
char *tag;
char *cmd;
time_t time;
int sec;
int w, h;
int imgw;
int textw;
int bar;
XRenderColor background;
XRenderColor foreground;
XRenderColor borderclr;
Imlib_Image image;
Window win;
};
/* notification queue structure */
struct Queue {
/* queue pointers */
struct Item *head, *tail;
/* general geometry for the notification queue */
int x, y; /* position of the first notification */
int w, h; /* width and height of individual notifications */
/* whether the queue changed */
bool change;
};
/* ellipsis size and font structure */
struct Ellipsis {
char *s;
size_t len; /* length of s */
int width; /* size of ellipsis string */
};
/* X stuff */
static int alignment;
static int seconds = 10;
static int saveargc;
static char **saveargv;
static Display *dpy;
static Colormap colormap;
static Visual *visual;
static Window root;
static int screen;
static int depth;
static int xfd;
static struct Queue queue; /* queue of notifications and their geometry */
static struct Monitor mon;
static Atom atoms[NATOMS];
static Resource application, resources[NRESOURCES];
static XRenderColor background, foreground, borderclr;
static struct Ellipsis ellipsis;
static CtrlFontSet *fontset = NULL;
static int fonth;
static XRenderPictFormat *xformat, *alphaformat;
static int gravity; /* NorthEastGravity, NorthGravity, etc */
static int direction; /* DownWards or UpWards */
static int gap_pixels, border_pixels, leading_pixels, padding_pixels;
static int max_height, image_pixels;
static unsigned short opacity;
static bool shrink;
static unsigned int actionbutton = Button3;
/* flags */
static bool oflag; /* whether only one notification must exist at a time */
static bool wflag; /* whether to let window manager manage notifications */
static bool rflag; /* whether to watch for notifications in the root window */
volatile sig_atomic_t sigflag;
void
usage(void)
{
(void)fprintf(stderr, "usage: xnotify [-ow] [-G gravity] [-b button] [-g geometry]\n");
(void)fprintf(stderr, " [-h height] [-m monitor] [-s seconds]\n");
exit(1);
}
static void *
emalloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
err(1, "malloc");
return p;
}
static char *
gettextprop(Window window, Atom prop)
{
char *text;
unsigned char *p;
unsigned long len;
unsigned long dl; /* dummy variable */
int format, status;
Atom type;
text = NULL;
status = XGetWindowProperty(
dpy,
window,
prop,
0L,
0x1FFFFFFF,
False,
AnyPropertyType,
&type, &format,
&len, &dl,
&p
);
if (status != Success || len == 0 || p == NULL) {
goto done;
}
if ((text = emalloc(len + 1)) == NULL) {
goto done;
}
memcpy(text, p, len);
text[len] = '\0';
done:
XFree(p);
return text;
}
static void
parsegravityspec(int *gravity, int *direction, const char *gravityspec)
{
if (gravityspec == NULL || strcmp(gravityspec, "N") == 0) {
*gravity = NorthGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "NW") == 0) {
*gravity = NorthWestGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "NE") == 0) {
*gravity = NorthEastGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "W") == 0) {
*gravity = WestGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "C") == 0) {
*gravity = CenterGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "E") == 0) {
*gravity = EastGravity;
*direction = DownWards;
} else if (strcmp(gravityspec, "SW") == 0) {
*gravity = SouthWestGravity;
*direction = UpWards;
} else if (strcmp(gravityspec, "S") == 0) {
*gravity = SouthGravity;
*direction = UpWards;
} else if (strcmp(gravityspec, "SE") == 0) {
*gravity = SouthEastGravity;
*direction = UpWards;
} else {
warnx("Unknown gravity %s", gravityspec);
}
}
static void
parseoptions(int argc, char *argv[], const char **geomspec)
{
unsigned long n;
int ch;
while ((ch = getopt(argc, argv, "G:b:g:h:m:ors:w")) != -1) {
switch (ch) {
case 'G':
parsegravityspec(&gravity, &direction, optarg);
break;
case 'b':
if (*(optarg+1) != '\0')
break;
switch (*optarg) {
case '1':
actionbutton = Button1;
break;
case '2':
actionbutton = Button2;
break;
case '3':
actionbutton = Button3;
break;
default:
break;
}
break;
case 'g':
*geomspec = optarg;
break;
case 'h':
if ((n = strtoul(optarg, NULL, 10)) < INT_MAX)
max_height = n;
break;
case 'm':
mon.num = atoi(optarg);
break;
case 'o':
oflag = true;
break;
case 'r':
rflag = true;
break;
case 's':
if ((n = strtoul(optarg, NULL, 10)) < INT_MAX)
seconds = n;
break;
case 'w':
wflag = 1;
break;
default:
usage();
break;
}
}
if (argc - optind != 0) {
usage();
}
}
static void
setcolor(XRenderColor *color, const char *value)
{
XColor xcolor;
if (value == NULL)
return;
if (!XParseColor(dpy, colormap, value, &xcolor)) {
warnx("%s: unknown color name", value);
return;
}
color->red = (xcolor.flags & DoRed) ? xcolor.red : 0x0000;
color->green = (xcolor.flags & DoGreen) ? xcolor.green : 0x0000;
color->blue = (xcolor.flags & DoBlue) ? xcolor.blue : 0x0000;
color->alpha = 0xFFFF;
}
static int
getnum(const char **s, int *n)
{
int retval;
long num;
char *endp;
num = strtol(*s, &endp, 10);
retval = (num > INT_MAX || num < 0 || endp == *s);
*s = endp;
*n = num;
return retval;
}
static void
sigusr1handler(int sig)
{
(void)sig;
sigflag = SIGNAL_KILL;
}
static void
sigusr2handler(int sig)
{
(void)sig;
sigflag = SIGNAL_CMD;
}
static void
sighuphandler(int sig)
{
(void)sig;
sigflag = SIGNAL_KILLALL;
}
static void
initsignal(void)
{
struct sigaction sa;
sa.sa_handler = sigusr1handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR1, &sa, NULL) == -1)
err(1, "sigaction");
sa.sa_handler = sigusr2handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR2, &sa, NULL) == -1)
err(1, "sigaction");
sa.sa_handler = sighuphandler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGHUP, &sa, NULL) == -1)
err(1, "sigaction");
}
static void
initmonitor(void)
{
XineramaScreenInfo *info = NULL;
int nmons;
mon.x = mon.y = 0;
mon.w = DisplayWidth(dpy, screen);
mon.h = DisplayHeight(dpy, screen);
if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) {
int selmon;
selmon = (mon.num >= 0 && mon.num < nmons) ? mon.num : 0;
mon.x = info[selmon].x_org;
mon.y = info[selmon].y_org;
mon.w = info[selmon].width;
mon.h = info[selmon].height;
XFree(info);
}
}
static void
parsegeometry(struct Queue *queue, const char *geometry)
{
unsigned int width, height;
int flags, x, y;
queue->x = 0;
queue->y = 0;
queue->w = 0;
queue->h = 0;
flags = XParseGeometry(geometry, &x, &y, &width, &height);
if (flags & WidthValue)
queue->w = width;
if (flags & HeightValue)
queue->h = height;
if (flags & XValue)
queue->x = x;
if (flags & YValue)
queue->y = y;
}
static void
setqueue(const char *geomspec)
{
int minw;
queue.head = NULL;
queue.tail = NULL;
queue.change = false;
if (geomspec != NULL)
parsegeometry(&queue, geomspec);
minw = ellipsis.width + image_pixels + padding_pixels * 3 + 1;
if (queue.w < minw)
queue.w = MAX(DEFWIDTH, minw);
}
static struct Item *
getitem(Window win)
{
struct Item *item;
for (item = queue.head; item; item = item->next)
if (item->win == win)
return item;
return NULL;
}
static Imlib_Image
loadimage(const char *file)
{
Imlib_Image image;
Imlib_Load_Error errcode;
const char *errstr;
image = imlib_load_image_with_error_return(file, &errcode);
if (*file == '\0') {
warnx("could not load image (file name is blank)");
return NULL;
} else if (image == NULL) {
switch (errcode) {
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
errstr = "file does not exist";
break;
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
errstr = "file is directory";
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
errstr = "permission denied";
break;
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
errstr = "unknown file format";
break;
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
errstr = "path too long";
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
errstr = "improper path";
break;
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
errstr = "too many symbolic links";
break;
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
errstr = "out of memory";
break;
case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
errstr = "out of file descriptors";
break;
default:
errstr = "unknown error";
break;
}
warnx("could not load image (%s): %s", errstr, file);
return NULL;
}
imlib_context_set_image(image);
imlib_image_set_changes_on_disk();
return image;
}
static void
createwindow(struct Item *item)
{
item->win = XCreateWindow(
dpy, root,
0, 0, /* placed at (0,0) for now; will be moved later */
item->w,
1, /* sized with 1px for now; will be resized later */
0,
depth,
InputOutput,
visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel |
CWSaveUnder | CWEventMask | CWColormap,
&(XSetWindowAttributes){
.override_redirect = wflag ? False : True,
.background_pixel = 0,
.border_pixel =0,
.colormap = colormap,
.save_under = True,
.event_mask = ButtonPressMask | PointerMotionMask,
}
);
XmbSetWMProperties(
dpy, item->win,
APP_CLASS, /* title name */
APP_CLASS, /* icon name */
saveargv,
saveargc,
NULL,
NULL,
&(XClassHint){
.res_class = APP_CLASS,
.res_name = APP_NAME,
}
);
XChangeProperty(
dpy,
item->win,
atoms[_NET_WM_NAME],
atoms[UTF8_STRING],
8,
PropModeReplace,
(unsigned char *)APP_CLASS,
strlen(APP_CLASS)
);
XChangeProperty(
dpy,
item->win,
atoms[_NET_WM_WINDOW_TYPE],
XA_ATOM,
32,
PropModeReplace,
(unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_NOTIFICATION],
1
);
XChangeProperty(
dpy,
item->win,
atoms[_NET_WM_STATE],
XA_ATOM,
32,
PropModeReplace,
(unsigned char *)&atoms[_NET_WM_STATE_ABOVE],
1
);
}
static void
drawitem(struct Item *item)
{
Pixmap pixmap, fg, alpha;
Picture picture;
Imlib_Image image = NULL;
const char *text;
size_t len, j;
int xaligned;
int bar, i, x, y, newh;
int texth, imgh, imgw;
int origimgw, origimgh;
pixmap = XCreatePixmap(dpy, item->win, item->w, max_height, depth);
picture = XRenderCreatePicture(dpy, pixmap, xformat, 0, NULL);
XRenderFillRectangle(
dpy,
PictOpSrc,
picture,
&item->background,
0, 0,
item->w,
max_height
);
/* draw opacity */
alpha = XRenderCreateSolidFill(dpy, &(XRenderColor){
.red = 0, .green = 0, .blue = 0,
.alpha = opacity
});
XRenderComposite(
dpy,
PictOpSrc,
picture,
alpha,
picture,
0, 0,
0, 0,
0, 0,
item->w,
max_height
);
/* draw image */
y = padding_pixels;
fg = XRenderCreateSolidFill(dpy, &item->foreground);
imgw = imgh = 0;
if (item->image && item->imgw > 0) {
imlib_context_set_image(item->image);
imlib_context_set_drawable(pixmap);
origimgw = imlib_image_get_width();
origimgh = imlib_image_get_height();
if (imgw > imgh) {
imgw = item->imgw;
imgh = (origimgh * item->imgw) / origimgw;
} else {
imgw = (origimgw * item->imgw) / origimgh;
imgh = item->imgw;
}
image = imlib_create_cropped_scaled_image(0, 0, origimgw, origimgh, imgw, imgh);
imlib_free_image();
imlib_context_set_image(image);
imlib_render_image_on_drawable(
padding_pixels + (item->imgw - imgw) / 2,
padding_pixels + (item->imgw - imgh) / 2
);
imlib_free_image();
}
/* draw text */
texth = 0;
for (i = 0; item->textw > 0 && i < item->nlines; i++) {
text = item->line[i];
x = padding_pixels;
x += (image && item->imgw > 0 ? item->imgw + padding_pixels : 0);
while (texth <= max_height) {
for (len = j = 0; text[len] != '\0'; len = j, j += strcspn(text + j, " \t")) {
j += strspn(text + j, " \t");
xaligned = ctrlfnt_width(fontset, text, j);
if (xaligned > item->textw)
break;
}
if (len < 1)
break;
if (xaligned <= 0)
break;
switch (alignment) {
case LeftAlignment:
break;
case CenterAlignment:
xaligned = (item->textw - xaligned) / 2;
x = MAX(x, xaligned);
break;
case RightAlignment:
xaligned = item->textw - xaligned;
x = MAX(x, xaligned);
break;
default:
break;
}
ctrlfnt_draw(
fontset,
picture,
fg,
(XRectangle){
.x = x,
.y = y + texth,
.width = item->textw,
.height = fonth,
},
text, len
);
texth += fonth + leading_pixels;
text += len;
}
}
if (texth > leading_pixels)
texth -= leading_pixels;
x = padding_pixels;
x += (image && item->imgw > 0 ? item->imgw + padding_pixels : 0);
/* draw bar */
if (item->bar > 0) {
bar = (item->textw * item->bar) / 100;
bar = MIN(bar, item->textw);
XRenderFillRectangle(
dpy,
PictOpSrc,
picture,
&item->foreground,
x, y + texth,
bar,
fonth
);
texth += fonth;
}
/* resize notification window based on its contents */
newh = MAX(imgh, texth) + 2 * padding_pixels;
item->h = MAX(item->h, newh);
XResizeWindow(dpy, item->win, item->w, item->h);
/* change border color */
XSetWindowBackgroundPixmap(dpy, item->win, pixmap);
XClearWindow(dpy, item->win);
XFreePixmap(dpy, pixmap);
XRenderFreePicture(dpy, picture);
XRenderFreePicture(dpy, fg);
XRenderFreePicture(dpy, alpha);
}
static void
resettime(struct Item *item)
{
item->time = time(NULL);
}
static char *
estrdup(const char *s)
{
char *t;
if ((t = strdup(s)) == NULL)
err(1, "strdup");
return t;
}
static void
additem(struct Itemspec *itemspec)
{
const char *text;
struct Item *item;
int w, i;
if ((item = malloc(sizeof *item)) == NULL)
err(1, "malloc");
item->next = NULL;
item->image = (itemspec->file) ? loadimage(itemspec->file) : NULL;
item->tag = (itemspec->tag) ? estrdup(itemspec->tag) : NULL;
item->cmd = (itemspec->cmd) ? estrdup(itemspec->cmd) : NULL;
item->sec = itemspec->sec;
item->bar = itemspec->bar;
if (!queue.head)
queue.head = item;
else
queue.tail->next = item;
item->prev = queue.tail;
queue.tail = item;
/* allocate texts */
item->line[0] = (itemspec->firstline) ? estrdup(itemspec->firstline) : NULL;
text = strtok(itemspec->otherlines, "\t\n");
for (i = 1; i < MAXLINES && text != NULL; i++) {
item->line[i] = estrdup(text);
text = strtok(NULL, "\t\n");
}
item->nlines = i;
/* allocate colors */
item->background = background;
item->foreground = foreground;
item->borderclr = borderclr;
setcolor(&item->background, itemspec->background);
setcolor(&item->foreground, itemspec->foreground);
setcolor(&item->borderclr, itemspec->border);
/* compute notification width and height */
item->imgw = image_pixels;
item->h = queue.h;
for (i = 0; i < item->nlines; i++) {
text = item->line[i];
w = ctrlfnt_width(fontset, text, strlen(text));
}
if (shrink) {
if (item->image) {
item->textw = queue.w - image_pixels - padding_pixels * 3;
item->textw = MIN(w, item->textw);
item->w = item->textw + image_pixels + padding_pixels * 3;
} else {
item->textw = queue.w - padding_pixels * 2;
item->textw = MIN(w, item->textw);
item->w = item->textw + padding_pixels * 2;
}
} else {
item->w = queue.w;
if (item->image) {
item->textw = queue.w - image_pixels - padding_pixels * (2 + (item->line[0] ? 1 : 0));
if (!image_pixels) {
item->textw = MIN(item->textw, w);
}
item->imgw = queue.w - item->textw - padding_pixels * (2 + (item->line[0] ? 1 : 0));
} else {
item->textw = queue.w - padding_pixels * 2;
}
}
/* call functions that set the item */
createwindow(item);
resettime(item);
drawitem(item);
/* a new item was added to the queue, so the queue changed */
queue.change = true;
}
static void
delitem(struct Item *item)
{
int i;
for (i = 0; i < item->nlines; i++)
free(item->line[i]);
XDestroyWindow(dpy, item->win);
if (item->prev)
item->prev->next = item->next;
else
queue.head = item->next;
if (item->next)
item->next->prev = item->prev;
else
queue.tail = item->prev;
free(item);
queue.change = true;
}
static void
cmditem(struct Item *item)
{
if (item->cmd != NULL)
printf("%s\n", item->cmd);
fflush(stdout);
}
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;
if (strncmp(s, "TAG:", 4) == 0)
return TAG;
if (strncmp(s, "CMD:", 4) == 0)
return CMD;
if (strncmp(s, "SEC:", 4) == 0)
return SEC;
if (strncmp(s, "BAR:", 4) == 0)
return BAR;
return UNKNOWN;
}
static bool
parseline(struct Itemspec *itemspec, char *s)
{
enum ItemOption option;
const char *t;
int n;
/* get the filename */
itemspec->file = NULL;
itemspec->foreground = NULL;
itemspec->background = NULL;
itemspec->border = NULL;
itemspec->tag = NULL;
itemspec->cmd = NULL;
itemspec->bar = -1;
itemspec->sec = seconds;
itemspec->firstline = strtok(s, "\t\n");
while (itemspec->firstline && (option = optiontype(itemspec->firstline)) != UNKNOWN) {
switch (option) {
case IMG:
itemspec->file = itemspec->firstline + 4;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case BG:
itemspec->background = itemspec->firstline + 3;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case FG:
itemspec->foreground = itemspec->firstline + 3;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case BRD:
itemspec->border = itemspec->firstline + 4;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case TAG:
itemspec->tag = itemspec->firstline + 4;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case CMD:
itemspec->cmd = itemspec->firstline + 4;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case BAR:
t = itemspec->firstline + 4;
if (!getnum(&t, &n) && n >= 0 && n <= 100)
itemspec->bar = n;
else
itemspec->bar = -1;
itemspec->firstline = strtok(NULL, "\t\n");
break;
case SEC:
t = itemspec->firstline + 4;
if (!getnum(&t, &n))
itemspec->sec = n;
itemspec->firstline = strtok(NULL, "\t\n");
break;
default:
break;
}
}
/* get the body */
itemspec->otherlines = strtok(NULL, "\n");
if (itemspec->otherlines)
while (*itemspec->otherlines == '\t')
itemspec->otherlines++;
if (!itemspec->firstline && !itemspec->file)
return false;
return true;
}
static void
timeitems(void)
{
struct Item *item;
struct Item *tmp;
item = queue.head;
while (item) {
tmp = item;
item = item->next;
if (tmp->sec && time(NULL) - tmp->time >= tmp->sec) {
delitem(tmp);
}
}
}
static void
moveitems(void)
{
struct Item *item;
int x, y;