1 /* See LICENSE file for copyright and license details. */
10 #define MAX(a, b) ((a) > (b) ? (a) : (b))
11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG])
13 #define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG])
14 #define DEFFONT "fixed"
16 static Bool loadfont(DC *dc, const char *fontstr);
19 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
20 XRectangle r = { dc->x + x, dc->y + y, w, h };
26 XSetForeground(dc->dpy, dc->gc, color);
27 (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
32 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
36 /* shorten text if necessary */
38 for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
41 memcpy(buf, text, mn);
43 for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
45 drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
46 drawtextn(dc, buf, mn, col);
50 drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
53 x = dc->x + dc->font.height/2;
54 y = dc->y + dc->font.ascent+1;
56 XSetForeground(dc->dpy, dc->gc, FG(dc, col));
58 XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
60 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
61 XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
66 eprintf(const char *fmt, ...) {
69 fprintf(stderr, "%s: ", progname);
71 vfprintf(stderr, fmt, ap);
79 XFreeFontSet(dc->dpy, dc->font.set);
81 XFreeFont(dc->dpy, dc->font.xfont);
83 XFreePixmap(dc->dpy, dc->canvas);
84 XFreeGC(dc->dpy, dc->gc);
85 XCloseDisplay(dc->dpy);
90 getcolor(DC *dc, const char *colstr) {
91 Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
94 if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
95 eprintf("cannot allocate color '%s'\n", colstr);
103 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
104 weprintf("no locale support\n");
105 if(!(dc = malloc(sizeof *dc)))
106 eprintf("cannot malloc %u bytes\n", sizeof *dc);
107 if(!(dc->dpy = XOpenDisplay(NULL)))
108 eprintf("cannot open display\n");
110 dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
111 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
112 dc->font.xfont = NULL;
119 initfont(DC *dc, const char *fontstr) {
120 if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
122 weprintf("cannot load font '%s'\n", fontstr);
123 if(fontstr == NULL || !loadfont(dc, DEFFONT))
124 eprintf("cannot load font '%s'\n", DEFFONT);
126 dc->font.height = dc->font.ascent + dc->font.descent;
130 loadfont(DC *dc, const char *fontstr) {
131 char *def, **missing;
136 if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
138 XFontStruct **xfonts;
140 n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
141 for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
142 dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
143 dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
146 else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
147 dc->font.ascent = dc->font.xfont->ascent;
148 dc->font.descent = dc->font.xfont->descent;
151 XFreeStringList(missing);
152 return (dc->font.set || dc->font.xfont);
156 mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
157 XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
161 resizedc(DC *dc, unsigned int w, unsigned int h) {
163 XFreePixmap(dc->dpy, dc->canvas);
164 dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
165 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
173 textnw(DC *dc, const char *text, size_t len) {
177 XmbTextExtents(dc->font.set, text, len, NULL, &r);
180 return XTextWidth(dc->font.xfont, text, len);
184 textw(DC *dc, const char *text) {
185 return textnw(dc, text, strlen(text)) + dc->font.height;
189 weprintf(const char *fmt, ...) {
192 fprintf(stderr, "%s: warning: ", progname);
194 vfprintf(stderr, fmt, ap);