diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 165 |
1 files changed, 165 insertions, 0 deletions
| @@ -61,6 +61,94 @@ cleanup(void) { | |||
| 61 | free(seltag); | 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 | drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) { | ||
| 77 | int x, y, w, h; | ||
| 78 | static char buf[256]; | ||
| 79 | unsigned int len, olen; | ||
| 80 | XGCValues gcv; | ||
| 81 | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | ||
| 82 | |||
| 83 | XSetForeground(dpy, dc.gc, col[ColBG]); | ||
| 84 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
| 85 | if(!text) | ||
| 86 | return; | ||
| 87 | w = 0; | ||
| 88 | olen = len = strlen(text); | ||
| 89 | if(len >= sizeof buf) | ||
| 90 | len = sizeof buf - 1; | ||
| 91 | memcpy(buf, text, len); | ||
| 92 | buf[len] = 0; | ||
| 93 | h = dc.font.ascent + dc.font.descent; | ||
| 94 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | ||
| 95 | x = dc.x + (h / 2); | ||
| 96 | /* shorten text if necessary */ | ||
| 97 | while(len && (w = textnw(buf, len)) > dc.w - h) | ||
| 98 | buf[--len] = 0; | ||
| 99 | if(len < olen) { | ||
| 100 | if(len > 1) | ||
| 101 | buf[len - 1] = '.'; | ||
| 102 | if(len > 2) | ||
| 103 | buf[len - 2] = '.'; | ||
| 104 | if(len > 3) | ||
| 105 | buf[len - 3] = '.'; | ||
| 106 | } | ||
| 107 | if(w > dc.w) | ||
| 108 | return; /* too long */ | ||
| 109 | gcv.foreground = col[ColFG]; | ||
| 110 | if(dc.font.set) { | ||
| 111 | XChangeGC(dpy, dc.gc, GCForeground, &gcv); | ||
| 112 | XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | gcv.font = dc.font.xfont->fid; | ||
| 116 | XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); | ||
| 117 | XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); | ||
| 118 | } | ||
| 119 | x = (h + 2) / 4; | ||
| 120 | r.x = dc.x + 1; | ||
| 121 | r.y = dc.y + 1; | ||
| 122 | if(filledsquare) { | ||
| 123 | r.width = r.height = x + 1; | ||
| 124 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
| 125 | } | ||
| 126 | else if(emptysquare) { | ||
| 127 | r.width = r.height = x; | ||
| 128 | XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | static unsigned long | ||
| 133 | getcolor(const char *colstr) { | ||
| 134 | Colormap cmap = DefaultColormap(dpy, screen); | ||
| 135 | XColor color; | ||
| 136 | |||
| 137 | if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) | ||
| 138 | eprint("error, cannot allocate color '%s'\n", colstr); | ||
| 139 | return color.pixel; | ||
| 140 | } | ||
| 141 | |||
| 142 | static Bool | ||
| 143 | isoccupied(unsigned int t) { | ||
| 144 | Client *c; | ||
| 145 | |||
| 146 | for(c = clients; c; c = c->next) | ||
| 147 | if(c->tags[t]) | ||
| 148 | return True; | ||
| 149 | return False; | ||
| 150 | } | ||
| 151 | |||
| 64 | static void | 152 | static void |
| 65 | scan(void) { | 153 | scan(void) { |
| 66 | unsigned int i, num; | 154 | unsigned int i, num; |
| @@ -82,6 +170,47 @@ scan(void) { | |||
| 82 | } | 170 | } |
| 83 | 171 | ||
| 84 | static void | 172 | static void |
| 173 | setfont(const char *fontstr) { | ||
| 174 | char *def, **missing; | ||
| 175 | int i, n; | ||
| 176 | |||
| 177 | missing = NULL; | ||
| 178 | if(dc.font.set) | ||
| 179 | XFreeFontSet(dpy, dc.font.set); | ||
| 180 | dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | ||
| 181 | if(missing) { | ||
| 182 | while(n--) | ||
| 183 | fprintf(stderr, "missing fontset: %s\n", missing[n]); | ||
| 184 | XFreeStringList(missing); | ||
| 185 | } | ||
| 186 | if(dc.font.set) { | ||
| 187 | XFontSetExtents *font_extents; | ||
| 188 | XFontStruct **xfonts; | ||
| 189 | char **font_names; | ||
| 190 | dc.font.ascent = dc.font.descent = 0; | ||
| 191 | font_extents = XExtentsOfFontSet(dc.font.set); | ||
| 192 | n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); | ||
| 193 | for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { | ||
| 194 | if(dc.font.ascent < (*xfonts)->ascent) | ||
| 195 | dc.font.ascent = (*xfonts)->ascent; | ||
| 196 | if(dc.font.descent < (*xfonts)->descent) | ||
| 197 | dc.font.descent = (*xfonts)->descent; | ||
| 198 | xfonts++; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | if(dc.font.xfont) | ||
| 203 | XFreeFont(dpy, dc.font.xfont); | ||
| 204 | dc.font.xfont = NULL; | ||
| 205 | if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))) | ||
| 206 | eprint("error, cannot load font: '%s'\n", fontstr); | ||
| 207 | dc.font.ascent = dc.font.xfont->ascent; | ||
| 208 | dc.font.descent = dc.font.xfont->descent; | ||
| 209 | } | ||
| 210 | dc.font.height = dc.font.ascent + dc.font.descent; | ||
| 211 | } | ||
| 212 | |||
| 213 | static void | ||
| 85 | setup(void) { | 214 | setup(void) { |
| 86 | int i, j; | 215 | int i, j; |
| 87 | unsigned int mask; | 216 | unsigned int mask; |
| @@ -171,6 +300,37 @@ xerrorstart(Display *dsply, XErrorEvent *ee) { | |||
| 171 | /* extern */ | 300 | /* extern */ |
| 172 | 301 | ||
| 173 | void | 302 | void |
| 303 | drawstatus(void) { | ||
| 304 | int i, x; | ||
| 305 | |||
| 306 | dc.x = dc.y = 0; | ||
| 307 | for(i = 0; i < ntags; i++) { | ||
| 308 | dc.w = textw(tags[i]); | ||
| 309 | if(seltag[i]) | ||
| 310 | drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i)); | ||
| 311 | else | ||
| 312 | drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i)); | ||
| 313 | dc.x += dc.w; | ||
| 314 | } | ||
| 315 | dc.w = bmw; | ||
| 316 | drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False); | ||
| 317 | x = dc.x + dc.w; | ||
| 318 | dc.w = textw(stext); | ||
| 319 | dc.x = sw - dc.w; | ||
| 320 | if(dc.x < x) { | ||
| 321 | dc.x = x; | ||
| 322 | dc.w = sw - x; | ||
| 323 | } | ||
| 324 | drawtext(stext, dc.norm, False, False); | ||
| 325 | if((dc.w = dc.x - x) > bh) { | ||
| 326 | dc.x = x; | ||
| 327 | drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False); | ||
| 328 | } | ||
| 329 | XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); | ||
| 330 | XSync(dpy, False); | ||
| 331 | } | ||
| 332 | |||
| 333 | void | ||
| 174 | sendevent(Window w, Atom a, long value) { | 334 | sendevent(Window w, Atom a, long value) { |
| 175 | XEvent e; | 335 | XEvent e; |
| 176 | 336 | ||
| @@ -184,6 +344,11 @@ sendevent(Window w, Atom a, long value) { | |||
| 184 | XSync(dpy, False); | 344 | XSync(dpy, False); |
| 185 | } | 345 | } |
| 186 | 346 | ||
| 347 | unsigned int | ||
| 348 | textw(const char *text) { | ||
| 349 | return textnw(text, strlen(text)) + dc.font.height; | ||
| 350 | } | ||
| 351 | |||
| 187 | void | 352 | void |
| 188 | quit(Arg *arg) { | 353 | quit(Arg *arg) { |
| 189 | readin = running = False; | 354 | readin = running = False; |
