dwm/drw.c

227 lines
5.3 KiB
C
Raw Normal View History

2013-04-18 02:21:47 +07:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include "drw.h"
#include "util.h"
Drw *
2013-06-16 20:20:29 +07:00
drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
2013-04-18 02:21:47 +07:00
Drw *drw = (Drw *)calloc(1, sizeof(Drw));
2013-06-16 20:20:29 +07:00
if(!drw)
return NULL;
2013-04-18 02:21:47 +07:00
drw->dpy = dpy;
drw->screen = screen;
2013-06-16 20:20:29 +07:00
drw->root = root;
2013-04-18 02:21:47 +07:00
drw->w = w;
drw->h = h;
2013-06-16 20:20:29 +07:00
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
drw->gc = XCreateGC(dpy, root, 0, NULL);
2013-04-18 02:21:47 +07:00
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
return drw;
}
void
drw_resize(Drw *drw, unsigned int w, unsigned int h) {
if(!drw)
return;
drw->w = w;
drw->h = h;
2013-06-16 20:20:29 +07:00
if(drw->drawable != 0)
XFreePixmap(drw->dpy, drw->drawable);
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
2013-04-18 02:21:47 +07:00
}
void
drw_free(Drw *drw) {
2013-06-16 20:20:29 +07:00
XFreePixmap(drw->dpy, drw->drawable);
2013-04-18 02:21:47 +07:00
XFreeGC(drw->dpy, drw->gc);
free(drw);
}
Fnt *
2013-06-16 20:20:29 +07:00
drw_font_create(Display *dpy, const char *fontname) {
2013-04-18 02:21:47 +07:00
Fnt *font;
char *def, **missing;
int n;
font = (Fnt *)calloc(1, sizeof(Fnt));
2013-06-16 20:20:29 +07:00
if(!font)
return NULL;
font->set = XCreateFontSet(dpy, fontname, &missing, &n, &def);
2013-04-18 02:21:47 +07:00
if(missing) {
while(n--)
fprintf(stderr, "drw: missing fontset: %s\n", missing[n]);
XFreeStringList(missing);
}
if(font->set) {
XFontStruct **xfonts;
char **font_names;
XExtentsOfFontSet(font->set);
n = XFontsOfFontSet(font->set, &xfonts, &font_names);
while(n--) {
font->ascent = MAX(font->ascent, (*xfonts)->ascent);
font->descent = MAX(font->descent,(*xfonts)->descent);
xfonts++;
}
}
else {
2013-06-16 20:20:29 +07:00
if(!(font->xfont = XLoadQueryFont(dpy, fontname))
&& !(font->xfont = XLoadQueryFont(dpy, "fixed")))
2013-04-18 02:21:47 +07:00
die("error, cannot load font: '%s'\n", fontname);
font->ascent = font->xfont->ascent;
font->descent = font->xfont->descent;
}
font->h = font->ascent + font->descent;
return font;
}
void
2013-06-16 20:20:29 +07:00
drw_font_free(Display *dpy, Fnt *font) {
if(!font)
2013-04-18 02:21:47 +07:00
return;
if(font->set)
2013-06-16 20:20:29 +07:00
XFreeFontSet(dpy, font->set);
2013-04-18 02:21:47 +07:00
else
2013-06-16 20:20:29 +07:00
XFreeFont(dpy, font->xfont);
2013-04-18 02:21:47 +07:00
free(font);
}
Clr *
drw_clr_create(Drw *drw, const char *clrname) {
2013-06-16 20:20:29 +07:00
Clr *clr;
Colormap cmap;
2013-04-18 02:21:47 +07:00
XColor color;
2013-06-16 20:20:29 +07:00
if(!drw)
return NULL;
clr = (Clr *)calloc(1, sizeof(Clr));
if(!clr)
return NULL;
cmap = DefaultColormap(drw->dpy, drw->screen);
2013-04-18 02:21:47 +07:00
if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
die("error, cannot allocate color '%s'\n", clrname);
clr->rgb = color.pixel;
return clr;
}
void
2013-06-16 20:20:29 +07:00
drw_clr_free(Clr *clr) {
if(clr)
free(clr);
2013-04-18 02:21:47 +07:00
}
void
drw_setfont(Drw *drw, Fnt *font) {
2013-06-16 20:20:29 +07:00
if(drw)
drw->font = font;
2013-04-18 02:21:47 +07:00
}
void
drw_setscheme(Drw *drw, ClrScheme *scheme) {
if(drw && scheme)
drw->scheme = scheme;
2013-04-18 02:21:47 +07:00
}
void
2013-06-16 20:20:29 +07:00
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
2013-04-18 02:21:47 +07:00
int dx;
if(!drw || !drw->font || !drw->scheme)
2013-04-18 02:21:47 +07:00
return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
2013-04-18 02:21:47 +07:00
dx = (drw->font->ascent + drw->font->descent + 2) / 4;
if(filled)
2013-06-16 20:20:29 +07:00
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx+1, dx+1);
2013-04-18 02:21:47 +07:00
else if(empty)
2013-06-16 20:20:29 +07:00
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x+1, y+1, dx, dx);
2013-04-18 02:21:47 +07:00
}
void
2013-06-16 20:20:29 +07:00
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
2013-04-18 02:21:47 +07:00
char buf[256];
2013-06-16 20:20:29 +07:00
int i, tx, ty, th, len, olen;
2013-04-18 02:21:47 +07:00
Extnts tex;
if(!drw || !drw->scheme)
2013-04-18 02:21:47 +07:00
return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->rgb : drw->scheme->bg->rgb);
2013-06-16 20:20:29 +07:00
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
2013-04-18 02:21:47 +07:00
if(!text || !drw->font)
return;
olen = strlen(text);
2013-06-16 20:20:29 +07:00
drw_font_getexts(drw->font, text, olen, &tex);
th = drw->font->ascent + drw->font->descent;
ty = y + (h / 2) - (th / 2) + drw->font->ascent;
tx = x + (h / 2);
2013-04-18 02:21:47 +07:00
/* shorten text if necessary */
for(len = MIN(olen, sizeof buf); len && (tex.w > w - tex.h || w < tex.h); len--)
2013-06-16 20:20:29 +07:00
drw_font_getexts(drw->font, text, len, &tex);
2013-04-18 02:21:47 +07:00
if(!len)
return;
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->rgb : drw->scheme->fg->rgb);
2013-04-18 02:21:47 +07:00
if(drw->font->set)
2013-06-16 20:20:29 +07:00
XmbDrawString(drw->dpy, drw->drawable, drw->font->set, drw->gc, tx, ty, buf, len);
2013-04-18 02:21:47 +07:00
else
2013-06-16 20:20:29 +07:00
XDrawString(drw->dpy, drw->drawable, drw->gc, tx, ty, buf, len);
2013-04-18 02:21:47 +07:00
}
void
2013-06-16 20:20:29 +07:00
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
2013-04-18 02:21:47 +07:00
if(!drw)
return;
2013-06-16 20:20:29 +07:00
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
2013-04-18 02:21:47 +07:00
XSync(drw->dpy, False);
}
void
2013-06-16 20:20:29 +07:00
drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
2013-04-18 02:21:47 +07:00
XRectangle r;
2013-06-16 20:20:29 +07:00
if(!font || !text)
2013-04-18 02:21:47 +07:00
return;
2013-06-16 20:20:29 +07:00
if(font->set) {
XmbTextExtents(font->set, text, len, NULL, &r);
2013-04-18 02:21:47 +07:00
tex->w = r.width;
tex->h = r.height;
}
else {
2013-06-16 20:20:29 +07:00
tex->h = font->ascent + font->descent;
tex->w = XTextWidth(font->xfont, text, len);
2013-04-18 02:21:47 +07:00
}
}
2013-06-16 20:20:29 +07:00
unsigned int
drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
Extnts tex;
if(!font)
return -1;
drw_font_getexts(font, text, len, &tex);
return tex.w;
}
Cur *
drw_cur_create(Drw *drw, int shape) {
Cur *cur = (Cur *)calloc(1, sizeof(Cur));
if(!drw || !cur)
return NULL;
cur->cursor = XCreateFontCursor(drw->dpy, shape);
return cur;
}
void
drw_cur_free(Drw *drw, Cur *cursor) {
if(!drw || !cursor)
return;
XFreeCursor(drw->dpy, cursor->cursor);
free(cursor);
}