initial commit - libsl - draw back-ends for dwm, dmenu, etc (HTM) git clone git://git.codemadness.org/libsl (DIR) Log (DIR) Files (DIR) Refs (DIR) LICENSE --- (DIR) commit bfdec1e2d45497b6d53e366aa31e063214829023 (HTM) Author: Anselm R Garbe <anselm@garbe.us> Date: Mon, 10 Dec 2012 10:04:58 +0100 initial commit Diffstat: A arg.h | 41 +++++++++++++++++++++++++++++++ A drw.c | 202 +++++++++++++++++++++++++++++++ A drw.h | 61 +++++++++++++++++++++++++++++++ A util.c | 17 +++++++++++++++++ A util.h | 6 ++++++ 5 files changed, 327 insertions(+), 0 deletions(-) --- (DIR) diff --git a/arg.h b/arg.h @@ -0,0 +1,41 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef __ARG_H__ +#define __ARG_H__ + +extern char *argv0; + +#define USED(x) ((void)(x)) + +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char _argc;\ + char **_argv;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (argv[0]++, _argv = argv; argv[0][0];\ + argv[0]++) {\ + if (_argv != argv)\ + break;\ + _argc = argv[0][0];\ + switch (_argc) + +#define ARGEND }\ + USED(_argc);\ + }\ + USED(argv);\ + USED(argc); + +#define EARGF(x) ((argv[1] == NULL)? ((x), abort(), (char *)0) :\ + (argc--, argv++, argv[0])) + +#endif + (DIR) diff --git a/drw.c b/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; + } +} (DIR) diff --git a/drw.h b/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); + (DIR) diff --git a/util.c b/util.c @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "util.h" + +void +die(const char *errstr, ...) { + va_list ap; + + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); + exit(EXIT_FAILURE); +} + (DIR) diff --git a/util.h b/util.h @@ -0,0 +1,6 @@ +/* See LICENSE file for copyright and license details. */ + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) + +void die(const char *errstr, ...);