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 DEFAULTFN "fixed"
14 static Bool loadfont(DC *dc, const char *fontstr);
17 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
18 XSetForeground(dc->dpy, dc->gc, color);
20 XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h);
22 XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1);
26 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
28 size_t mn, n = strlen(text);
30 /* shorten text if necessary */
31 for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--)
34 memcpy(buf, text, mn);
36 for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
38 drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
39 drawtextn(dc, buf, mn, col);
43 drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
44 int x = dc->x + dc->font.height/2;
45 int y = dc->y + dc->font.ascent+1;
47 XSetForeground(dc->dpy, dc->gc, FG(dc, col));
49 XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
51 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
52 XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
57 eprintf(const char *fmt, ...) {
61 vfprintf(stderr, fmt, ap);
64 if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') {
74 XFreeFontSet(dc->dpy, dc->font.set);
76 XFreeFont(dc->dpy, dc->font.xfont);
78 XFreePixmap(dc->dpy, dc->canvas);
79 XFreeGC(dc->dpy, dc->gc);
80 XCloseDisplay(dc->dpy);
85 getcolor(DC *dc, const char *colstr) {
86 Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
89 if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
90 eprintf("cannot allocate color '%s'\n", colstr);
98 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
99 fputs("no locale support\n", stderr);
100 if(!(dc = calloc(1, sizeof *dc)))
101 eprintf("cannot malloc %u bytes:", sizeof *dc);
102 if(!(dc->dpy = XOpenDisplay(NULL)))
103 eprintf("cannot open display\n");
105 dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
106 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
111 initfont(DC *dc, const char *fontstr) {
112 if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) {
114 fprintf(stderr, "cannot load font '%s'\n", fontstr);
115 if(fontstr == NULL || !loadfont(dc, DEFAULTFN))
116 eprintf("cannot load font '%s'\n", DEFAULTFN);
118 dc->font.height = dc->font.ascent + dc->font.descent;
122 loadfont(DC *dc, const char *fontstr) {
123 char *def, **missing, **names;
125 XFontStruct **xfonts;
129 if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
130 n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
131 for(i = 0; i < n; i++) {
132 dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
133 dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
134 dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width);
137 else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
138 dc->font.ascent = dc->font.xfont->ascent;
139 dc->font.descent = dc->font.xfont->descent;
140 dc->font.width = dc->font.xfont->max_bounds.width;
143 XFreeStringList(missing);
144 return dc->font.set || dc->font.xfont;
148 mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
149 XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
153 resizedc(DC *dc, unsigned int w, unsigned int h) {
155 XFreePixmap(dc->dpy, dc->canvas);
159 dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
160 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
164 textnw(DC *dc, const char *text, size_t len) {
168 XmbTextExtents(dc->font.set, text, len, NULL, &r);
171 return XTextWidth(dc->font.xfont, text, len);
175 textw(DC *dc, const char *text) {
176 return textnw(dc, text, strlen(text)) + dc->font.height;