added draw.c again (except getcolor and setfont which are helpers in main.c)
e7508783
4 file(s) · +144 −133
| 3 | 3 | ||
| 4 | 4 | include config.mk |
|
| 5 | 5 | ||
| 6 | - | SRC = client.c event.c layout.c main.c tag.c util.c |
|
| 6 | + | SRC = client.c draw.c event.c layout.c main.c tag.c util.c |
|
| 7 | 7 | OBJ = ${SRC:.c=.o} |
|
| 8 | 8 | ||
| 9 | 9 | all: options dwm |
| 1 | + | /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com> |
|
| 2 | + | * See LICENSE file for license details. |
|
| 3 | + | */ |
|
| 4 | + | #include "dwm.h" |
|
| 5 | + | #include <string.h> |
|
| 6 | + | ||
| 7 | + | /* static */ |
|
| 8 | + | ||
| 9 | + | static unsigned int |
|
| 10 | + | textnw(const char *text, unsigned int len) { |
|
| 11 | + | XRectangle r; |
|
| 12 | + | ||
| 13 | + | if(dc.font.set) { |
|
| 14 | + | XmbTextExtents(dc.font.set, text, len, NULL, &r); |
|
| 15 | + | return r.width; |
|
| 16 | + | } |
|
| 17 | + | return XTextWidth(dc.font.xfont, text, len); |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | static void |
|
| 21 | + | drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) { |
|
| 22 | + | int x; |
|
| 23 | + | XGCValues gcv; |
|
| 24 | + | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 25 | + | ||
| 26 | + | gcv.foreground = col[ColFG]; |
|
| 27 | + | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 28 | + | x = (dc.font.ascent + dc.font.descent + 2) / 4; |
|
| 29 | + | r.x = dc.x + 1; |
|
| 30 | + | r.y = dc.y + 1; |
|
| 31 | + | if(filled) { |
|
| 32 | + | r.width = r.height = x + 1; |
|
| 33 | + | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 34 | + | } |
|
| 35 | + | else if(empty) { |
|
| 36 | + | r.width = r.height = x; |
|
| 37 | + | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 38 | + | } |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | static Bool |
|
| 42 | + | isoccupied(unsigned int t) { |
|
| 43 | + | Client *c; |
|
| 44 | + | ||
| 45 | + | for(c = clients; c; c = c->next) |
|
| 46 | + | if(c->tags[t]) |
|
| 47 | + | return True; |
|
| 48 | + | return False; |
|
| 49 | + | } |
|
| 50 | + | ||
| 51 | + | /* extern */ |
|
| 52 | + | ||
| 53 | + | void |
|
| 54 | + | drawstatus(void) { |
|
| 55 | + | int i, x; |
|
| 56 | + | ||
| 57 | + | dc.x = dc.y = 0; |
|
| 58 | + | for(i = 0; i < ntags; i++) { |
|
| 59 | + | dc.w = textw(tags[i]); |
|
| 60 | + | if(seltag[i]) { |
|
| 61 | + | drawtext(tags[i], dc.sel); |
|
| 62 | + | drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel); |
|
| 63 | + | } |
|
| 64 | + | else { |
|
| 65 | + | drawtext(tags[i], dc.norm); |
|
| 66 | + | drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm); |
|
| 67 | + | } |
|
| 68 | + | dc.x += dc.w; |
|
| 69 | + | } |
|
| 70 | + | dc.w = blw; |
|
| 71 | + | drawtext(lt->symbol, dc.norm); |
|
| 72 | + | x = dc.x + dc.w; |
|
| 73 | + | dc.w = textw(stext); |
|
| 74 | + | dc.x = sw - dc.w; |
|
| 75 | + | if(dc.x < x) { |
|
| 76 | + | dc.x = x; |
|
| 77 | + | dc.w = sw - x; |
|
| 78 | + | } |
|
| 79 | + | drawtext(stext, dc.norm); |
|
| 80 | + | if((dc.w = dc.x - x) > bh) { |
|
| 81 | + | dc.x = x; |
|
| 82 | + | drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm); |
|
| 83 | + | } |
|
| 84 | + | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); |
|
| 85 | + | XSync(dpy, False); |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | void |
|
| 89 | + | drawtext(const char *text, unsigned long col[ColLast]) { |
|
| 90 | + | int x, y, w, h; |
|
| 91 | + | static char buf[256]; |
|
| 92 | + | unsigned int len, olen; |
|
| 93 | + | XGCValues gcv; |
|
| 94 | + | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 95 | + | ||
| 96 | + | XSetForeground(dpy, dc.gc, col[ColBG]); |
|
| 97 | + | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 98 | + | if(!text) |
|
| 99 | + | return; |
|
| 100 | + | w = 0; |
|
| 101 | + | olen = len = strlen(text); |
|
| 102 | + | if(len >= sizeof buf) |
|
| 103 | + | len = sizeof buf - 1; |
|
| 104 | + | memcpy(buf, text, len); |
|
| 105 | + | buf[len] = 0; |
|
| 106 | + | h = dc.font.ascent + dc.font.descent; |
|
| 107 | + | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 108 | + | x = dc.x + (h / 2); |
|
| 109 | + | /* shorten text if necessary */ |
|
| 110 | + | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 111 | + | buf[--len] = 0; |
|
| 112 | + | if(len < olen) { |
|
| 113 | + | if(len > 1) |
|
| 114 | + | buf[len - 1] = '.'; |
|
| 115 | + | if(len > 2) |
|
| 116 | + | buf[len - 2] = '.'; |
|
| 117 | + | if(len > 3) |
|
| 118 | + | buf[len - 3] = '.'; |
|
| 119 | + | } |
|
| 120 | + | if(w > dc.w) |
|
| 121 | + | return; /* too long */ |
|
| 122 | + | gcv.foreground = col[ColFG]; |
|
| 123 | + | if(dc.font.set) { |
|
| 124 | + | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 125 | + | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
|
| 126 | + | } |
|
| 127 | + | else { |
|
| 128 | + | gcv.font = dc.font.xfont->fid; |
|
| 129 | + | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); |
|
| 130 | + | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 131 | + | } |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | unsigned int |
|
| 135 | + | textw(const char *text) { |
|
| 136 | + | return textnw(text, strlen(text)) + dc.font.height; |
|
| 137 | + | } |
| 117 | 117 | extern void unmanage(Client *c); /* destroy c */ |
|
| 118 | 118 | extern void zoom(Arg *arg); /* zooms the focused client to master area, arg is ignored */ |
|
| 119 | 119 | ||
| 120 | + | /* draw.c */ |
|
| 121 | + | extern void drawstatus(void); /* draw the bar */ |
|
| 122 | + | extern void drawtext(const char *text, |
|
| 123 | + | unsigned long col[ColLast]); /* draw text */ |
|
| 124 | + | extern unsigned int textw(const char *text); /* return the width of text in px*/ |
|
| 125 | + | ||
| 120 | 126 | /* event.c */ |
|
| 121 | 127 | extern void grabkeys(void); /* grab all keys defined in config.h */ |
|
| 122 | 128 | ||
| 130 | 136 | extern void versatile(void); /* arranges all windows versatile */ |
|
| 131 | 137 | ||
| 132 | 138 | /* main.c */ |
|
| 133 | - | extern void drawstatus(void); /* draw the bar */ |
|
| 134 | - | extern void drawtext(const char *text, |
|
| 135 | - | unsigned long col[ColLast]); /* draw text */ |
|
| 136 | - | extern unsigned int textw(const char *text); /* return the width of text in px*/ |
|
| 137 | 139 | extern void quit(Arg *arg); /* quit dwm nicely */ |
|
| 138 | 140 | extern void sendevent(Window w, Atom a, long value); /* send synthetic event to w */ |
|
| 139 | 141 | extern int xerror(Display *dsply, XErrorEvent *ee); /* dwm's X error handler */ |
|
| 61 | 61 | free(seltag); |
|
| 62 | 62 | } |
|
| 63 | 63 | ||
| 64 | - | static unsigned int |
|
| 65 | - | textnw(const char *text, unsigned int len) { |
|
| 66 | - | XRectangle r; |
|
| 67 | - | ||
| 68 | - | if(dc.font.set) { |
|
| 69 | - | XmbTextExtents(dc.font.set, text, len, NULL, &r); |
|
| 70 | - | return r.width; |
|
| 71 | - | } |
|
| 72 | - | return XTextWidth(dc.font.xfont, text, len); |
|
| 73 | - | } |
|
| 74 | - | ||
| 75 | - | static void |
|
| 76 | - | drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) { |
|
| 77 | - | int x; |
|
| 78 | - | XGCValues gcv; |
|
| 79 | - | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 80 | - | ||
| 81 | - | gcv.foreground = col[ColFG]; |
|
| 82 | - | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 83 | - | x = (dc.font.ascent + dc.font.descent + 2) / 4; |
|
| 84 | - | r.x = dc.x + 1; |
|
| 85 | - | r.y = dc.y + 1; |
|
| 86 | - | if(filled) { |
|
| 87 | - | r.width = r.height = x + 1; |
|
| 88 | - | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 89 | - | } |
|
| 90 | - | else if(empty) { |
|
| 91 | - | r.width = r.height = x; |
|
| 92 | - | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 93 | - | } |
|
| 94 | - | } |
|
| 95 | - | ||
| 96 | 64 | static unsigned long |
|
| 97 | 65 | getcolor(const char *colstr) { |
|
| 98 | 66 | Colormap cmap = DefaultColormap(dpy, screen); |
|
| 101 | 69 | if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) |
|
| 102 | 70 | eprint("error, cannot allocate color '%s'\n", colstr); |
|
| 103 | 71 | return color.pixel; |
|
| 104 | - | } |
|
| 105 | - | ||
| 106 | - | static Bool |
|
| 107 | - | isoccupied(unsigned int t) { |
|
| 108 | - | Client *c; |
|
| 109 | - | ||
| 110 | - | for(c = clients; c; c = c->next) |
|
| 111 | - | if(c->tags[t]) |
|
| 112 | - | return True; |
|
| 113 | - | return False; |
|
| 114 | 72 | } |
|
| 115 | 73 | ||
| 116 | 74 | static void |
|
| 264 | 222 | /* extern */ |
|
| 265 | 223 | ||
| 266 | 224 | void |
|
| 267 | - | drawstatus(void) { |
|
| 268 | - | int i, x; |
|
| 269 | - | ||
| 270 | - | dc.x = dc.y = 0; |
|
| 271 | - | for(i = 0; i < ntags; i++) { |
|
| 272 | - | dc.w = textw(tags[i]); |
|
| 273 | - | if(seltag[i]) { |
|
| 274 | - | drawtext(tags[i], dc.sel); |
|
| 275 | - | drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel); |
|
| 276 | - | } |
|
| 277 | - | else { |
|
| 278 | - | drawtext(tags[i], dc.norm); |
|
| 279 | - | drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm); |
|
| 280 | - | } |
|
| 281 | - | dc.x += dc.w; |
|
| 282 | - | } |
|
| 283 | - | dc.w = blw; |
|
| 284 | - | drawtext(lt->symbol, dc.norm); |
|
| 285 | - | x = dc.x + dc.w; |
|
| 286 | - | dc.w = textw(stext); |
|
| 287 | - | dc.x = sw - dc.w; |
|
| 288 | - | if(dc.x < x) { |
|
| 289 | - | dc.x = x; |
|
| 290 | - | dc.w = sw - x; |
|
| 291 | - | } |
|
| 292 | - | drawtext(stext, dc.norm); |
|
| 293 | - | if((dc.w = dc.x - x) > bh) { |
|
| 294 | - | dc.x = x; |
|
| 295 | - | drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm); |
|
| 296 | - | } |
|
| 297 | - | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); |
|
| 298 | - | XSync(dpy, False); |
|
| 299 | - | } |
|
| 300 | - | ||
| 301 | - | void |
|
| 302 | - | drawtext(const char *text, unsigned long col[ColLast]) { |
|
| 303 | - | int x, y, w, h; |
|
| 304 | - | static char buf[256]; |
|
| 305 | - | unsigned int len, olen; |
|
| 306 | - | XGCValues gcv; |
|
| 307 | - | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 308 | - | ||
| 309 | - | XSetForeground(dpy, dc.gc, col[ColBG]); |
|
| 310 | - | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 311 | - | if(!text) |
|
| 312 | - | return; |
|
| 313 | - | w = 0; |
|
| 314 | - | olen = len = strlen(text); |
|
| 315 | - | if(len >= sizeof buf) |
|
| 316 | - | len = sizeof buf - 1; |
|
| 317 | - | memcpy(buf, text, len); |
|
| 318 | - | buf[len] = 0; |
|
| 319 | - | h = dc.font.ascent + dc.font.descent; |
|
| 320 | - | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 321 | - | x = dc.x + (h / 2); |
|
| 322 | - | /* shorten text if necessary */ |
|
| 323 | - | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 324 | - | buf[--len] = 0; |
|
| 325 | - | if(len < olen) { |
|
| 326 | - | if(len > 1) |
|
| 327 | - | buf[len - 1] = '.'; |
|
| 328 | - | if(len > 2) |
|
| 329 | - | buf[len - 2] = '.'; |
|
| 330 | - | if(len > 3) |
|
| 331 | - | buf[len - 3] = '.'; |
|
| 332 | - | } |
|
| 333 | - | if(w > dc.w) |
|
| 334 | - | return; /* too long */ |
|
| 335 | - | gcv.foreground = col[ColFG]; |
|
| 336 | - | if(dc.font.set) { |
|
| 337 | - | XChangeGC(dpy, dc.gc, GCForeground, &gcv); |
|
| 338 | - | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); |
|
| 339 | - | } |
|
| 340 | - | else { |
|
| 341 | - | gcv.font = dc.font.xfont->fid; |
|
| 342 | - | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); |
|
| 343 | - | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 344 | - | } |
|
| 345 | - | } |
|
| 346 | - | ||
| 347 | - | void |
|
| 348 | 225 | sendevent(Window w, Atom a, long value) { |
|
| 349 | 226 | XEvent e; |
|
| 350 | 227 | ||
| 356 | 233 | e.xclient.data.l[1] = CurrentTime; |
|
| 357 | 234 | XSendEvent(dpy, w, False, NoEventMask, &e); |
|
| 358 | 235 | XSync(dpy, False); |
|
| 359 | - | } |
|
| 360 | - | ||
| 361 | - | unsigned int |
|
| 362 | - | textw(const char *text) { |
|
| 363 | - | return textnw(text, strlen(text)) + dc.font.height; |
|
| 364 | 236 | } |
|
| 365 | 237 | ||
| 366 | 238 | void |
|