| @@ -41,13 +41,7 @@ static Cursor chand; |
| 41 | 41 | | static Cursor cwatch; |
| 42 | 42 | | static GC gc; |
| 43 | 43 | | |
| 44 | | − | static struct { |
| 45 | | − | int ascent; |
| 46 | | − | int descent; |
| 47 | | − | XFontStruct *xfont; |
| 48 | | − | XFontSet set; |
| 49 | | − | } font; |
| 50 | | − | |
| 44 | + | static XftFont *font; |
| 51 | 45 | | static int fontheight; |
| 52 | 46 | | static int barheight; |
| 53 | 47 | | |
| @@ -56,50 +50,21 @@ Atom atoms[ATOM_COUNT]; |
| 56 | 50 | | static Bool fs_support; |
| 57 | 51 | | static Bool fs_warned; |
| 58 | 52 | | |
| 59 | | − | void win_init_font(Display *dpy, const char *fontstr) |
| 53 | + | void win_init_font(const win_env_t *e, const char *fontstr) |
| 60 | 54 | | { |
| 61 | | − | int n; |
| 62 | | − | char *def, **missing; |
| 63 | | − | |
| 64 | | − | font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); |
| 65 | | − | if (missing) |
| 66 | | − | XFreeStringList(missing); |
| 67 | | − | if (font.set) { |
| 68 | | − | XFontStruct **xfonts; |
| 69 | | − | char **font_names; |
| 70 | | − | |
| 71 | | − | font.ascent = font.descent = 0; |
| 72 | | − | XExtentsOfFontSet(font.set); |
| 73 | | − | n = XFontsOfFontSet(font.set, &xfonts, &font_names); |
| 74 | | − | while (n--) { |
| 75 | | − | font.ascent = MAX(font.ascent, (*xfonts)->ascent); |
| 76 | | − | font.descent = MAX(font.descent,(*xfonts)->descent); |
| 77 | | − | xfonts++; |
| 78 | | − | } |
| 79 | | − | } else { |
| 80 | | − | if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL && |
| 81 | | − | (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL) |
| 82 | | − | { |
| 83 | | − | error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr); |
| 84 | | − | } |
| 85 | | − | font.ascent = font.xfont->ascent; |
| 86 | | − | font.descent = font.xfont->descent; |
| 87 | | − | } |
| 88 | | − | fontheight = font.ascent + font.descent; |
| 55 | + | if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL) |
| 56 | + | error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr); |
| 57 | + | fontheight = font->ascent + font->descent; |
| 89 | 58 | | barheight = fontheight + 2 * V_TEXT_PAD; |
| 90 | 59 | | } |
| 91 | 60 | | |
| 92 | | − | unsigned long win_alloc_color(win_t *win, const char *name) |
| 61 | + | void win_alloc_color(const win_env_t *e, const char *name, XftColor *col) |
| 93 | 62 | | { |
| 94 | | − | XColor col; |
| 95 | | − | |
| 96 | | − | if (XAllocNamedColor(win->env.dpy, |
| 97 | | − | DefaultColormap(win->env.dpy, win->env.scr), |
| 98 | | − | name, &col, &col) == 0) |
| 63 | + | if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr), |
| 64 | + | DefaultColormap(e->dpy, e->scr), name, col)) |
| 99 | 65 | | { |
| 100 | 66 | | error(EXIT_FAILURE, 0, "Error allocating color '%s'", name); |
| 101 | 67 | | } |
| 102 | | − | return col.pixel; |
| 103 | 68 | | } |
| 104 | 69 | | |
| 105 | 70 | | void win_check_wm_support(Display *dpy, Window root) |
| @@ -155,13 +120,13 @@ void win_init(win_t *win) |
| 155 | 120 | | if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) |
| 156 | 121 | | error(0, 0, "No locale support"); |
| 157 | 122 | | |
| 158 | | − | win_init_font(e->dpy, BAR_FONT); |
| 123 | + | win_init_font(e, BAR_FONT); |
| 159 | 124 | | |
| 160 | | − | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); |
| 161 | | − | win->fscol = win_alloc_color(win, WIN_FS_COLOR); |
| 162 | | − | win->selcol = win_alloc_color(win, SEL_COLOR); |
| 163 | | − | win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR); |
| 164 | | − | win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR); |
| 125 | + | win_alloc_color(e, WIN_BG_COLOR, &win->bgcol); |
| 126 | + | win_alloc_color(e, WIN_FS_COLOR, &win->fscol); |
| 127 | + | win_alloc_color(e, SEL_COLOR, &win->selcol); |
| 128 | + | win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol); |
| 129 | + | win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol); |
| 165 | 130 | | |
| 166 | 131 | | win->bar.l.size = BAR_L_LEN; |
| 167 | 132 | | win->bar.r.size = BAR_R_LEN; |
| @@ -295,7 +260,7 @@ void win_open(win_t *win) |
| 295 | 260 | | win->buf.h = e->scrh; |
| 296 | 261 | | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, |
| 297 | 262 | | win->buf.w, win->buf.h, e->depth); |
| 298 | | − | XSetForeground(e->dpy, gc, fullscreen ? win->fscol : win->bgcol); |
| 263 | + | XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel); |
| 299 | 264 | | XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); |
| 300 | 265 | | XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm); |
| 301 | 266 | | |
| @@ -387,7 +352,7 @@ void win_clear(win_t *win) |
| 387 | 352 | | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, |
| 388 | 353 | | win->buf.w, win->buf.h, e->depth); |
| 389 | 354 | | } |
| 390 | | − | XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol); |
| 355 | + | XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel); |
| 391 | 356 | | XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); |
| 392 | 357 | | } |
| 393 | 358 | | |
| @@ -398,33 +363,33 @@ void win_draw_bar(win_t *win) |
| 398 | 363 | | const char *dots = "..."; |
| 399 | 364 | | win_env_t *e; |
| 400 | 365 | | win_bar_t *l, *r; |
| 366 | + | XftDraw *d; |
| 401 | 367 | | |
| 402 | 368 | | if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL) |
| 403 | 369 | | return; |
| 404 | 370 | | |
| 405 | 371 | | e = &win->env; |
| 406 | | − | y = win->h + font.ascent + V_TEXT_PAD; |
| 372 | + | y = win->h + font->ascent + V_TEXT_PAD; |
| 407 | 373 | | w = win->w; |
| 374 | + | d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr), |
| 375 | + | DefaultColormap(e->dpy, e->scr)); |
| 408 | 376 | | |
| 409 | | − | XSetForeground(e->dpy, gc, win->bar.bgcol); |
| 377 | + | XSetForeground(e->dpy, gc, win->bar.bgcol.pixel); |
| 410 | 378 | | XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h); |
| 411 | 379 | | |
| 412 | | − | XSetForeground(e->dpy, gc, win->bar.fgcol); |
| 413 | | − | XSetBackground(e->dpy, gc, win->bar.bgcol); |
| 380 | + | XSetForeground(e->dpy, gc, win->bar.fgcol.pixel); |
| 381 | + | XSetBackground(e->dpy, gc, win->bar.bgcol.pixel); |
| 414 | 382 | | |
| 415 | 383 | | if ((len = strlen(r->buf)) > 0) { |
| 416 | | − | if ((tw = win_textwidth(r->buf, len, true)) > w) |
| 384 | + | if ((tw = win_textwidth(e, r->buf, len, true)) > w) |
| 417 | 385 | | return; |
| 418 | 386 | | x = win->w - tw + H_TEXT_PAD; |
| 419 | 387 | | w -= tw; |
| 420 | | − | if (font.set) |
| 421 | | − | XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, r->buf, len); |
| 422 | | − | else |
| 423 | | − | XDrawString(e->dpy, win->buf.pm, gc, x, y, r->buf, len); |
| 388 | + | XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)r->buf, len); |
| 424 | 389 | | } |
| 425 | 390 | | if ((len = strlen(l->buf)) > 0) { |
| 426 | 391 | | olen = len; |
| 427 | | − | while (len > 0 && (tw = win_textwidth(l->buf, len, true)) > w) |
| 392 | + | while (len > 0 && (tw = win_textwidth(e, l->buf, len, true)) > w) |
| 428 | 393 | | len--; |
| 429 | 394 | | if (len > 0) { |
| 430 | 395 | | if (len != olen) { |
| @@ -435,14 +400,12 @@ void win_draw_bar(win_t *win) |
| 435 | 400 | | memcpy(l->buf + len - w, dots, w); |
| 436 | 401 | | } |
| 437 | 402 | | x = H_TEXT_PAD; |
| 438 | | − | if (font.set) |
| 439 | | − | XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, l->buf, len); |
| 440 | | − | else |
| 441 | | − | XDrawString(e->dpy, win->buf.pm, gc, x, y, l->buf, len); |
| 403 | + | XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)l->buf, len); |
| 442 | 404 | | if (len != olen) |
| 443 | 405 | | memcpy(l->buf + len - w, rest, w); |
| 444 | 406 | | } |
| 445 | 407 | | } |
| 408 | + | XftDrawDestroy(d); |
| 446 | 409 | | } |
| 447 | 410 | | |
| 448 | 411 | | void win_draw(win_t *win) |
| @@ -470,17 +433,12 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw, |
| 470 | 433 | | XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); |
| 471 | 434 | | } |
| 472 | 435 | | |
| 473 | | − | int win_textwidth(const char *text, unsigned int len, bool with_padding) |
| 436 | + | int win_textwidth(const win_env_t *e, const char *text, unsigned int len, bool with_padding) |
| 474 | 437 | | { |
| 475 | | − | XRectangle r; |
| 476 | | − | int padding = with_padding ? 2 * H_TEXT_PAD : 0; |
| 438 | + | XGlyphInfo ext; |
| 477 | 439 | | |
| 478 | | − | if (font.set) { |
| 479 | | − | XmbTextExtents(font.set, text, len, NULL, &r); |
| 480 | | − | return r.width + padding; |
| 481 | | − | } else { |
| 482 | | − | return XTextWidth(font.xfont, text, len) + padding; |
| 483 | | − | } |
| 440 | + | XftTextExtentsUtf8(e->dpy, font, (XftChar8*)text, len, &ext); |
| 441 | + | return ext.xOff + (with_padding ? 2 * H_TEXT_PAD : 0); |
| 484 | 442 | | } |
| 485 | 443 | | |
| 486 | 444 | | void win_set_title(win_t *win, const char *title) |