renamed draw into drw

keyboard
Anselm R Garbe 12 years ago
parent aafeaf7317
commit f2544a3318
  1. 2
      Makefile
  2. 202
      draw.c
  3. 64
      draw.h
  4. 202
      drw.c
  5. 61
      drw.h
  6. 2
      dwm.c

@ -3,7 +3,7 @@
include config.mk
SRC = util.c draw.c dwm.c
SRC = drw.c dwm.c util.c
OBJ = ${SRC:.c=.o}
all: options dwm

202
draw.c

@ -1,202 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include "draw.h"
#include "util.h"
Draw *
draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
Draw *draw = (Draw *)calloc(1, sizeof(Draw));
draw->dpy = dpy;
draw->screen = screen;
draw->win = win;
draw->w = w;
draw->h = h;
draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
draw->gc = XCreateGC(dpy, win, 0, NULL);
XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
return draw;
}
void
draw_resize(Draw *draw, unsigned int w, unsigned int h) {
if(!draw)
return;
draw->w = w;
draw->h = h;
XFreePixmap(draw->dpy, draw->drawable);
draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
}
void
draw_free(Draw *draw) {
XFreePixmap(draw->dpy, draw->drawable);
XFreeGC(draw->dpy, draw->gc);
free(draw);
}
Fnt *
draw_font_create(Draw *draw, const char *fontname) {
Fnt *font;
char *def, **missing;
int n;
if(!draw)
return NULL;
font = (Fnt *)calloc(1, sizeof(Fnt));
font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
if(missing) {
while(n--)
fprintf(stderr, "draw: 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 {
if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
&& !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
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
draw_font_free(Draw *draw, Fnt *font) {
if(!draw || !font)
return;
if(font->set)
XFreeFontSet(draw->dpy, font->set);
else
XFreeFont(draw->dpy, font->xfont);
free(font);
}
Col *
draw_col_create(Draw *draw, const char *colname) {
Col *col = (Col *)calloc(1, sizeof(Col));
Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
XColor color;
if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
die("error, cannot allocate color '%s'\n", colname);
col->rgb = color.pixel;
return col;
}
void
draw_col_free(Draw *draw, Col *col) {
if(!col)
return;
free(col);
}
void
draw_setfont(Draw *draw, Fnt *font) {
if(!draw)
return;
draw->font = font;
}
void
draw_setfg(Draw *draw, Col *col) {
if(!draw)
return;
draw->fg = col;
}
void
draw_setbg(Draw *draw, Col *col) {
if(!draw)
return;
draw->bg = col;
}
void
draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
int dx;
if(!draw || !draw->font || !draw->fg || !draw->bg)
return;
XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
dx = (draw->font->ascent + draw->font->descent + 2) / 4;
if(filled)
XFillRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx+1, dx+1);
else if(empty)
XDrawRectangle(draw->dpy, draw->drawable, draw->gc, x+1, y+1, dx, dx);
}
void
draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
char buf[256];
int i, tx, ty, len, olen;
TextExtents tex;
if(!draw || !draw->fg || !draw->bg)
return;
XSetForeground(draw->dpy, draw->gc, invert ? draw->fg->rgb : draw->bg->rgb);
XFillRectangle(draw->dpy, draw->drawable, draw->gc, x, y, w, h);
if(!text || !draw->font)
return;
olen = strlen(text);
draw_getextents(draw, text, olen, &tex);
ty = y + (h / 2) - tex.yOff;
tx = x + tex.xOff;
/* shorten text if necessary */
for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
draw_getextents(draw, text, len, &tex);
if(!len)
return;
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
XSetForeground(draw->dpy, draw->gc, invert ? draw->bg->rgb : draw->fg->rgb);
if(draw->font->set)
XmbDrawString(draw->dpy, draw->drawable, draw->font->set, draw->gc, tx, ty, buf, len);
else
XDrawString(draw->dpy, draw->drawable, draw->gc, tx, ty, buf, len);
}
void
draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
if(!draw)
return;
XCopyArea(draw->dpy, draw->drawable, draw->win, draw->gc, x, y, w, h, x, y);
XSync(draw->dpy, False);
}
void
draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents) {
XRectangle r;
if(!draw || !draw->font || !text)
return;
if(draw->font->set) {
XmbTextExtents(draw->font->set, text, len, NULL, &r);
extents->xOff = r.x;
extents->yOff = r.y;
extents->w = r.width;
extents->h = r.height;
}
else {
extents->h = draw->font->ascent + draw->font->descent;
extents->w = XTextWidth(draw->font->xfont, text, len);
extents->xOff = extents->h / 2;
extents->yOff = (extents->h / 2) + draw->font->ascent;
}
}

@ -1,64 +0,0 @@
/* See LICENSE file for copyright and license details. */
struct _XCol {
unsigned long rgb;
};
typedef struct _XCol Col;
struct _XFont {
int ascent;
int descent;
unsigned int h;
XFontSet set;
XFontStruct *xfont;
};
typedef struct _XFont Fnt;
typedef struct _XDraw Draw;
struct _XDraw {
unsigned int w, h;
Display *dpy;
int screen;
Window win;
Drawable drawable;
GC gc;
Col *fg;
Col *bg;
Fnt *font;
};
typedef struct {
unsigned int w;
unsigned int h;
int xOff;
int yOff;
} TextExtents;
/* Drawable abstraction */
Draw *draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
void draw_resize(Draw *draw, unsigned int w, unsigned int h);
void draw_free(Draw *draw);
/* Fnt abstraction */
Fnt *draw_font_create(Draw *draw, const char *fontname);
void draw_font_free(Draw *draw, Fnt *font);
/* Colour abstraction */
Col *draw_col_create(Draw *draw, const char *colname);
void draw_col_free(Draw *draw, Col *col);
/* Drawing context manipulation */
void draw_setfont(Draw *draw, Fnt *font);
void draw_setfg(Draw *draw, Col *col);
void draw_setbg(Draw *draw, Col *col);
/* Drawing functions */
void draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
void draw_text(Draw *draw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
/* Map functions */
void draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h);
/* Text functions */
void draw_getextents(Draw *draw, const char *text, unsigned int len, TextExtents *extents);

202
drw.c

@ -0,0 +1,202 @@
/* 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 *
drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
Drw *drw = (Drw *)calloc(1, sizeof(Drw));
drw->dpy = dpy;
drw->screen = screen;
drw->win = win;
drw->w = w;
drw->h = h;
drw->drwable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
drw->gc = XCreateGC(dpy, win, 0, NULL);
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;
XFreePixmap(drw->dpy, drw->drwable);
drw->drwable = XCreatePixmap(drw->dpy, drw->win, w, h, DefaultDepth(drw->dpy, drw->screen));
}
void
drw_free(Drw *drw) {
XFreePixmap(drw->dpy, drw->drwable);
XFreeGC(drw->dpy, drw->gc);
free(drw);
}
Fnt *
drw_font_create(Drw *drw, const char *fontname) {
Fnt *font;
char *def, **missing;
int n;
if(!drw)
return NULL;
font = (Fnt *)calloc(1, sizeof(Fnt));
font->set = XCreateFontSet(drw->dpy, fontname, &missing, &n, &def);
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 {
if(!(font->xfont = XLoadQueryFont(drw->dpy, fontname))
&& !(font->xfont = XLoadQueryFont(drw->dpy, "fixed")))
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
drw_font_free(Drw *drw, Fnt *font) {
if(!drw || !font)
return;
if(font->set)
XFreeFontSet(drw->dpy, font->set);
else
XFreeFont(drw->dpy, font->xfont);
free(font);
}
Clr *
drw_clr_create(Drw *drw, const char *clrname) {
Clr *clr = (Clr *)calloc(1, sizeof(Clr));
Colormap cmap = DefaultColormap(drw->dpy, drw->screen);
XColor color;
if(!XAllocNamedColor(drw->dpy, cmap, clrname, &color, &color))
die("error, cannot allocate color '%s'\n", clrname);
clr->rgb = color.pixel;
return clr;
}
void
drw_clr_free(Drw *drw, Clr *clr) {
if(!clr)
return;
free(clr);
}
void
drw_setfont(Drw *drw, Fnt *font) {
if(!drw)
return;
drw->font = font;
}
void
drw_setfg(Drw *drw, Clr *clr) {
if(!drw)
return;
drw->fg = clr;
}
void
drw_setbg(Drw *drw, Clr *clr) {
if(!drw)
return;
drw->bg = clr;
}
void
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert) {
int dx;
if(!drw || !drw->font || !drw->fg || !drw->bg)
return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->bg->rgb : drw->fg->rgb);
dx = (drw->font->ascent + drw->font->descent + 2) / 4;
if(filled)
XFillRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx+1, dx+1);
else if(empty)
XDrawRectangle(drw->dpy, drw->drwable, drw->gc, x+1, y+1, dx, dx);
}
void
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert) {
char buf[256];
int i, tx, ty, len, olen;
Extnts tex;
if(!drw || !drw->fg || !drw->bg)
return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->fg->rgb : drw->bg->rgb);
XFillRectangle(drw->dpy, drw->drwable, drw->gc, x, y, w, h);
if(!text || !drw->font)
return;
olen = strlen(text);
drw_getexts(drw, text, olen, &tex);
ty = y + (h / 2) - tex.yOff;
tx = x + tex.xOff;
/* shorten text if necessary */
for(len = MIN(olen, sizeof buf); len && tex.w > w - tex.h; len--)
drw_getexts(drw, text, len, &tex);
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->bg->rgb : drw->fg->rgb);
if(drw->font->set)
XmbDrawString(drw->dpy, drw->drwable, drw->font->set, drw->gc, tx, ty, buf, len);
else
XDrawString(drw->dpy, drw->drwable, drw->gc, tx, ty, buf, len);
}
void
drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h) {
if(!drw)
return;
XCopyArea(drw->dpy, drw->drwable, drw->win, drw->gc, x, y, w, h, x, y);
XSync(drw->dpy, False);
}
void
drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *tex) {
XRectangle r;
if(!drw || !drw->font || !text)
return;
if(drw->font->set) {
XmbTextExtents(drw->font->set, text, len, NULL, &r);
tex->xOff = r.x;
tex->yOff = r.y;
tex->w = r.width;
tex->h = r.height;
}
else {
tex->h = drw->font->ascent + drw->font->descent;
tex->w = XTextWidth(drw->font->xfont, text, len);
tex->xOff = tex->h / 2;
tex->yOff = (tex->h / 2) + drw->font->ascent;
}
}

61
drw.h

@ -0,0 +1,61 @@
/* See LICENSE file for copyright and license details. */
typedef struct {
unsigned long rgb;
} Clr;
typedef struct {
int ascent;
int descent;
unsigned int h;
XFontSet set;
XFontStruct *xfont;
} Fnt;
typedef struct {
unsigned int w, h;
Display *dpy;
int screen;
Window win;
Drawable drwable;
GC gc;
Clr *fg;
Clr *bg;
Fnt *font;
} Drw;
typedef struct {
unsigned int w;
unsigned int h;
int xOff;
int yOff;
} Extnts;
/* Drawable abstraction */
Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
void drw_resize(Drw *drw, unsigned int w, unsigned int h);
void drw_free(Drw *drw);
/* Fnt abstraction */
Fnt *drw_font_create(Drw *drw, const char *fontname);
void drw_font_free(Drw *drw, Fnt *font);
/* Clrour abstraction */
Clr *drw_clr_create(Drw *drw, const char *clrname);
void drw_clr_free(Drw *drw, Clr *clr);
/* Drawing context manipulation */
void drw_setfont(Drw *drw, Fnt *font);
void drw_setfg(Drw *drw, Clr *clr);
void drw_setbg(Drw *drw, Clr *clr);
/* Drawing functions */
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, Bool filled, Bool empty, Bool invert);
void drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, Bool invert);
/* Map functions */
void drw_map(Drw *drw, int x, int y, unsigned int w, unsigned int h);
/* Text functions */
void drw_getexts(Drw *drw, const char *text, unsigned int len, Extnts *extnts);

@ -40,7 +40,7 @@
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include "draw.h"
#include "drw.h"
#include "util.h"
/* macros */

Loading…
Cancel
Save