diff options
| -rw-r--r-- | draw.c | 176 | ||||
| -rw-r--r-- | draw.h | 35 |
2 files changed, 0 insertions, 211 deletions
| @@ -1,176 +0,0 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | ||
| 2 | #include <locale.h> | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <X11/Xlib.h> | ||
| 8 | #include "draw.h" | ||
| 9 | |||
| 10 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
| 11 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
| 12 | #define DEFAULTFN "fixed" | ||
| 13 | |||
| 14 | static Bool loadfont(DC *dc, const char *fontstr); | ||
| 15 | |||
| 16 | void | ||
| 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); | ||
| 19 | if(fill) | ||
| 20 | XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); | ||
| 21 | else | ||
| 22 | XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1); | ||
| 23 | } | ||
| 24 | |||
| 25 | void | ||
| 26 | drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { | ||
| 27 | char buf[BUFSIZ]; | ||
| 28 | size_t mn, n = strlen(text); | ||
| 29 | |||
| 30 | /* shorten text if necessary */ | ||
| 31 | for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) | ||
| 32 | if(mn == 0) | ||
| 33 | return; | ||
| 34 | memcpy(buf, text, mn); | ||
| 35 | if(mn < n) | ||
| 36 | for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.'); | ||
| 37 | |||
| 38 | drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col)); | ||
| 39 | drawtextn(dc, buf, mn, col); | ||
| 40 | } | ||
| 41 | |||
| 42 | void | ||
| 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; | ||
| 46 | |||
| 47 | XSetForeground(dc->dpy, dc->gc, FG(dc, col)); | ||
| 48 | if(dc->font.set) | ||
| 49 | XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); | ||
| 50 | else { | ||
| 51 | XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); | ||
| 52 | XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | void | ||
| 57 | eprintf(const char *fmt, ...) { | ||
| 58 | va_list ap; | ||
| 59 | |||
| 60 | va_start(ap, fmt); | ||
| 61 | vfprintf(stderr, fmt, ap); | ||
| 62 | va_end(ap); | ||
| 63 | |||
| 64 | if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { | ||
| 65 | fputc(' ', stderr); | ||
| 66 | perror(NULL); | ||
| 67 | } | ||
| 68 | exit(EXIT_FAILURE); | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | freedc(DC *dc) { | ||
| 73 | if(dc->font.set) | ||
| 74 | XFreeFontSet(dc->dpy, dc->font.set); | ||
| 75 | if(dc->font.xfont) | ||
| 76 | XFreeFont(dc->dpy, dc->font.xfont); | ||
| 77 | if(dc->canvas) | ||
| 78 | XFreePixmap(dc->dpy, dc->canvas); | ||
| 79 | XFreeGC(dc->dpy, dc->gc); | ||
| 80 | XCloseDisplay(dc->dpy); | ||
| 81 | free(dc); | ||
| 82 | } | ||
| 83 | |||
| 84 | unsigned long | ||
| 85 | getcolor(DC *dc, const char *colstr) { | ||
| 86 | Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); | ||
| 87 | XColor color; | ||
| 88 | |||
| 89 | if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) | ||
| 90 | eprintf("cannot allocate color '%s'\n", colstr); | ||
| 91 | return color.pixel; | ||
| 92 | } | ||
| 93 | |||
| 94 | DC * | ||
| 95 | initdc(void) { | ||
| 96 | DC *dc; | ||
| 97 | |||
| 98 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) | ||
| 99 | fprintf(stderr, "no locale support\n"); | ||
| 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"); | ||
| 104 | |||
| 105 | dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); | ||
| 106 | XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); | ||
| 107 | return dc; | ||
| 108 | } | ||
| 109 | |||
| 110 | void | ||
| 111 | initfont(DC *dc, const char *fontstr) { | ||
| 112 | if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { | ||
| 113 | if(fontstr != NULL) | ||
| 114 | fprintf(stderr, "cannot load font '%s'\n", fontstr); | ||
| 115 | if(fontstr == NULL || !loadfont(dc, DEFAULTFN)) | ||
| 116 | eprintf("cannot load font '%s'\n", DEFAULTFN); | ||
| 117 | } | ||
| 118 | dc->font.height = dc->font.ascent + dc->font.descent; | ||
| 119 | } | ||
| 120 | |||
| 121 | Bool | ||
| 122 | loadfont(DC *dc, const char *fontstr) { | ||
| 123 | char *def, **missing, **names; | ||
| 124 | int i, n = 1; | ||
| 125 | XFontStruct **xfonts; | ||
| 126 | |||
| 127 | if(!*fontstr) | ||
| 128 | return False; | ||
| 129 | if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) | ||
| 130 | n = XFontsOfFontSet(dc->font.set, &xfonts, &names); | ||
| 131 | else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) | ||
| 132 | xfonts = &dc->font.xfont; | ||
| 133 | else | ||
| 134 | n = 0; | ||
| 135 | |||
| 136 | for(i = 0; i < n; i++) { | ||
| 137 | dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent); | ||
| 138 | dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent); | ||
| 139 | dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width); | ||
| 140 | } | ||
| 141 | if(missing) | ||
| 142 | XFreeStringList(missing); | ||
| 143 | return (dc->font.set || dc->font.xfont); | ||
| 144 | } | ||
| 145 | |||
| 146 | void | ||
| 147 | mapdc(DC *dc, Window win, unsigned int w, unsigned int h) { | ||
| 148 | XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0); | ||
| 149 | } | ||
| 150 | |||
| 151 | void | ||
| 152 | resizedc(DC *dc, unsigned int w, unsigned int h) { | ||
| 153 | if(dc->canvas) | ||
| 154 | XFreePixmap(dc->dpy, dc->canvas); | ||
| 155 | |||
| 156 | dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h, | ||
| 157 | DefaultDepth(dc->dpy, DefaultScreen(dc->dpy))); | ||
| 158 | dc->w = w; | ||
| 159 | dc->h = h; | ||
| 160 | } | ||
| 161 | |||
| 162 | int | ||
| 163 | textnw(DC *dc, const char *text, size_t len) { | ||
| 164 | if(dc->font.set) { | ||
| 165 | XRectangle r; | ||
| 166 | |||
| 167 | XmbTextExtents(dc->font.set, text, len, NULL, &r); | ||
| 168 | return r.width; | ||
| 169 | } | ||
| 170 | return XTextWidth(dc->font.xfont, text, len); | ||
| 171 | } | ||
| 172 | |||
| 173 | int | ||
| 174 | textw(DC *dc, const char *text) { | ||
| 175 | return textnw(dc, text, strlen(text)) + dc->font.height; | ||
| 176 | } | ||
| @@ -1,35 +0,0 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | ||
| 2 | |||
| 3 | #define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG]) | ||
| 4 | #define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG]) | ||
| 5 | |||
| 6 | enum { ColBG, ColFG, ColBorder, ColLast }; | ||
| 7 | |||
| 8 | typedef struct { | ||
| 9 | int x, y, w, h; | ||
| 10 | Bool invert; | ||
| 11 | Display *dpy; | ||
| 12 | GC gc; | ||
| 13 | Pixmap canvas; | ||
| 14 | struct { | ||
| 15 | int ascent; | ||
| 16 | int descent; | ||
| 17 | int height; | ||
| 18 | int width; | ||
| 19 | XFontSet set; | ||
| 20 | XFontStruct *xfont; | ||
| 21 | } font; | ||
| 22 | } DC; /* draw context */ | ||
| 23 | |||
| 24 | void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color); | ||
| 25 | void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); | ||
| 26 | void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]); | ||
| 27 | void eprintf(const char *fmt, ...); | ||
| 28 | void freedc(DC *dc); | ||
| 29 | unsigned long getcolor(DC *dc, const char *colstr); | ||
| 30 | DC *initdc(void); | ||
| 31 | void initfont(DC *dc, const char *fontstr); | ||
| 32 | void mapdc(DC *dc, Window win, unsigned int w, unsigned int h); | ||
| 33 | void resizedc(DC *dc, unsigned int w, unsigned int h); | ||
| 34 | int textnw(DC *dc, const char *text, size_t len); | ||
| 35 | int textw(DC *dc, const char *text); | ||
