Xinqi Bao's Git
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 DEFFONT "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 XRectangle r
= { dc
->x
+ x
, dc
->y
+ y
, w
, h
};
24 XSetForeground(dc
->dpy
, dc
->gc
, color
);
25 (fill
? XFillRectangles
: XDrawRectangles
)(dc
->dpy
, dc
->canvas
, dc
->gc
, &r
, 1);
30 drawtext(DC
*dc
, const char *text
, unsigned long col
[ColLast
]) {
34 /* shorten text if necessary */
36 for(mn
= MIN(n
, sizeof buf
); textnw(dc
, text
, mn
) > dc
->w
- dc
->font
.height
/2; mn
--)
39 memcpy(buf
, text
, mn
);
41 for(n
= MAX(mn
-3, 0); n
< mn
; buf
[n
++] = '.');
43 drawrect(dc
, 0, 0, dc
->w
, dc
->h
, True
, BG(dc
, col
));
44 drawtextn(dc
, buf
, mn
, col
);
48 drawtextn(DC
*dc
, const char *text
, size_t n
, unsigned long col
[ColLast
]) {
51 x
= dc
->x
+ dc
->font
.height
/2;
52 y
= dc
->y
+ dc
->font
.ascent
+1;
54 XSetForeground(dc
->dpy
, dc
->gc
, FG(dc
, col
));
56 XmbDrawString(dc
->dpy
, dc
->canvas
, dc
->font
.set
, dc
->gc
, x
, y
, text
, n
);
58 XSetFont(dc
->dpy
, dc
->gc
, dc
->font
.xfont
->fid
);
59 XDrawString(dc
->dpy
, dc
->canvas
, dc
->gc
, x
, y
, text
, n
);
64 eprintf(const char *fmt
, ...) {
67 fprintf(stderr
, "%s: ", progname
);
69 vfprintf(stderr
, fmt
, ap
);
72 if(fmt
[strlen(fmt
)-1] == ':') {
82 XFreeFontSet(dc
->dpy
, dc
->font
.set
);
84 XFreeFont(dc
->dpy
, dc
->font
.xfont
);
86 XFreePixmap(dc
->dpy
, dc
->canvas
);
87 XFreeGC(dc
->dpy
, dc
->gc
);
88 XCloseDisplay(dc
->dpy
);
93 getcolor(DC
*dc
, const char *colstr
) {
94 Colormap cmap
= DefaultColormap(dc
->dpy
, DefaultScreen(dc
->dpy
));
97 if(!XAllocNamedColor(dc
->dpy
, cmap
, colstr
, &color
, &color
))
98 eprintf("cannot allocate color '%s'\n", colstr
);
106 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
107 weprintf("no locale support\n");
108 if(!(dc
= calloc(1, sizeof *dc
)))
109 eprintf("cannot malloc %u bytes:", sizeof *dc
);
110 if(!(dc
->dpy
= XOpenDisplay(NULL
)))
111 eprintf("cannot open display\n");
113 dc
->gc
= XCreateGC(dc
->dpy
, DefaultRootWindow(dc
->dpy
), 0, NULL
);
114 XSetLineAttributes(dc
->dpy
, dc
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
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: ", progname
);
194 vfprintf(stderr
, fmt
, ap
);