import new drw and util from libsl.

This commit is contained in:
Markus Teich 2016-05-21 21:39:58 +02:00
parent 268d1968ea
commit 27a904c1dd
6 changed files with 238 additions and 240 deletions

View File

@ -8,8 +8,10 @@ static char *fontfallbacks[] = {
#define NUMFONTSCALES 42 #define NUMFONTSCALES 42
#define FONTSZ(x) ((int)(10.0 * powf(1.1288, (x)))) /* x in [0, NUMFONTSCALES-1] */ #define FONTSZ(x) ((int)(10.0 * powf(1.1288, (x)))) /* x in [0, NUMFONTSCALES-1] */
static const char *fgcol = "#000000"; static const char *colors[] = {
static const char *bgcol = "#FFFFFF"; "#000000", /* foreground color */
"#FFFFFF", /* background color */
};
static const float linespacing = 1.4; static const float linespacing = 1.4;

300
drw.c
View File

@ -11,15 +11,14 @@
#define UTF_INVALID 0xFFFD #define UTF_INVALID 0xFFFD
#define UTF_SIZ 4 #define UTF_SIZ 4
static void drw_xfont_free(Fnt *font);
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
static long static long
utf8decodebyte(const char c, size_t *i) { utf8decodebyte(const char c, size_t *i)
{
for (*i = 0; *i < (UTF_SIZ + 1); ++(*i)) for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
if (((unsigned char)c & utfmask[*i]) == utfbyte[*i]) if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
return (unsigned char)c & ~utfmask[*i]; return (unsigned char)c & ~utfmask[*i];
@ -27,7 +26,8 @@ utf8decodebyte(const char c, size_t *i) {
} }
static size_t static size_t
utf8validate(long *u, size_t i) { utf8validate(long *u, size_t i)
{
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID; *u = UTF_INVALID;
for (i = 1; *u > utfmax[i]; ++i) for (i = 1; *u > utfmax[i]; ++i)
@ -36,7 +36,8 @@ utf8validate(long *u, size_t i) {
} }
static size_t static size_t
utf8decode(const char *c, long *u, size_t clen) { utf8decode(const char *c, long *u, size_t clen)
{
size_t i, j, len, type; size_t i, j, len, type;
long udecoded; long udecoded;
@ -48,21 +49,22 @@ utf8decode(const char *c, long *u, size_t clen) {
return 1; return 1;
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) { for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type); udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
if(type != 0) if (type)
return j; return j;
} }
if (j < len) if (j < len)
return 0; return 0;
*u = udecoded; *u = udecoded;
utf8validate(u, len); utf8validate(u, len);
return len; return len;
} }
Drw * Drw *
drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) { drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
Drw *drw = (Drw *)calloc(1, sizeof(Drw)); {
if(!drw) Drw *drw = ecalloc(1, sizeof(Drw));
return NULL;
drw->dpy = dpy; drw->dpy = dpy;
drw->screen = screen; drw->screen = screen;
drw->root = root; drw->root = root;
@ -71,22 +73,26 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
drw->gc = XCreateGC(dpy, root, 0, NULL); drw->gc = XCreateGC(dpy, root, 0, NULL);
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
return drw; return drw;
} }
void void
drw_resize(Drw *drw, unsigned int w, unsigned int h) { drw_resize(Drw *drw, unsigned int w, unsigned int h)
{
if (!drw) if (!drw)
return; return;
drw->w = w; drw->w = w;
drw->h = h; drw->h = h;
if(drw->drawable != 0) if (drw->drawable)
XFreePixmap(drw->dpy, drw->drawable); XFreePixmap(drw->dpy, drw->drawable);
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen)); drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
} }
void void
drw_free(Drw *drw) { drw_free(Drw *drw)
{
XFreePixmap(drw->dpy, drw->drawable); XFreePixmap(drw->dpy, drw->drawable);
XFreeGC(drw->dpy, drw->gc); XFreeGC(drw->dpy, drw->gc);
free(drw); free(drw);
@ -96,52 +102,48 @@ drw_free(Drw *drw) {
* drw_fontset_create instead. * drw_fontset_create instead.
*/ */
static Fnt * static Fnt *
drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) { xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
{
Fnt *font; Fnt *font;
XftFont *xfont = NULL;
if (!(fontname || fontpattern)) FcPattern *pattern = NULL;
die("No font specified.\n");
if (!(font = (Fnt *)calloc(1, sizeof(Fnt))))
return NULL;
if (fontname) { if (fontname) {
/* Using the pattern found at font->xfont->pattern does not yield the /* Using the pattern found at font->xfont->pattern does not yield the
* same substitution results as using the pattern returned by * same substitution results as using the pattern returned by
* FcNameParse; using the latter results in the desired fallback * FcNameParse; using the latter results in the desired fallback
* behaviour whereas the former just results in * behaviour whereas the former just results in missing-character
* missing-character-rectangles being drawn, at least with some fonts. * rectangles being drawn, at least with some fonts. */
*/ if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) || fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
!(font->pattern = FcNameParse((const FcChar8 *) fontname))) {
if (font->xfont) {
XftFontClose(drw->dpy, font->xfont);
font->xfont = NULL;
}
fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
}
} else if (fontpattern) {
if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
fprintf(stderr, "error, cannot load font pattern.\n");
} else {
font->pattern = NULL;
}
}
if (!font->xfont) {
free(font);
return NULL; return NULL;
} }
if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
XftFontClose(drw->dpy, xfont);
return NULL;
}
} else if (fontpattern) {
if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
fprintf(stderr, "error, cannot load font from pattern.\n");
return NULL;
}
} else {
die("no font specified.\n");
}
font->ascent = font->xfont->ascent; font = ecalloc(1, sizeof(Fnt));
font->descent = font->xfont->descent; font->xfont = xfont;
font->h = font->ascent + font->descent; font->pattern = pattern;
font->h = xfont->ascent + xfont->descent;
font->dpy = drw->dpy; font->dpy = drw->dpy;
return font; return font;
} }
void static void
drw_xfont_free(Fnt *font) { xfont_free(Fnt *font)
{
if (!font) if (!font)
return; return;
if (font->pattern) if (font->pattern)
@ -151,64 +153,72 @@ drw_xfont_free(Fnt *font) {
} }
Fnt* Fnt*
drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) { drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
Fnt *ret = NULL; {
Fnt *cur = NULL; Fnt *cur, *ret = NULL;
ssize_t i; size_t i;
for (i = fontcount - 1; i >= 0; i--) {
if ((cur = drw_font_xcreate(drw, fonts[i], NULL))) { if (!drw || !fonts)
return NULL;
for (i = 1; i <= fontcount; i++) {
if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
cur->next = ret; cur->next = ret;
ret = cur; ret = cur;
} }
} }
return (drw->fonts = ret);
}
void
drw_fontset_free(Fnt *font)
{
if (font) {
drw_fontset_free(font->next);
xfont_free(font);
}
}
void
drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
{
if (!drw || !dest || !clrname)
return;
if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
DefaultColormap(drw->dpy, drw->screen),
clrname, dest))
die("error, cannot allocate color '%s'\n", clrname);
}
/* Wrapper to create color schemes. The caller has to call free(3) on the
* returned color scheme when done using it. */
Clr *
drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
{
size_t i;
Clr *ret;
/* need at least two colors for a scheme */
if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
return NULL;
for (i = 0; i < clrcount; i++)
drw_clr_create(drw, &ret[i], clrnames[i]);
return ret; return ret;
} }
void void
drw_fontset_free(Fnt *font) { drw_setfontset(Drw *drw, Fnt *set)
Fnt *nf = font; {
while ((font = nf)) {
nf = font->next;
drw_xfont_free(font);
}
}
Scm *
drw_scm_create(Drw *drw, const char *fgname, const char *bgname) {
Scm *scm;
Colormap cmap;
Visual *vis;
if (!drw || !(scm = (Scm *)calloc(1, sizeof(Scm))))
return NULL;
cmap = DefaultColormap(drw->dpy, drw->screen);
vis = DefaultVisual(drw->dpy, drw->screen);
if (!XftColorAllocName(drw->dpy, vis, cmap, fgname, &scm->fg.rgb))
die("error, cannot allocate color '%s'\n", fgname);
if (!XftColorAllocName(drw->dpy, vis, cmap, bgname, &scm->bg.rgb))
die("error, cannot allocate color '%s'\n", bgname);
scm->fg.pix = scm->fg.rgb.pixel;
scm->bg.pix = scm->bg.rgb.pixel;
return scm;
}
void
drw_scm_free(Scm *scm) {
if (scm)
free(scm);
}
void
drw_setfontset(Drw *drw, Fnt *set) {
if (drw) if (drw)
drw->fonts = set; drw->fonts = set;
} }
void void
drw_setscheme(Drw *drw, Scm *scm) { drw_setscheme(Drw *drw, Clr *scm)
if (drw && scm) {
if (drw)
drw->scheme = scm; drw->scheme = scm;
} }
@ -217,24 +227,23 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
{ {
if (!drw || !drw->scheme) if (!drw || !drw->scheme)
return; return;
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg.pix : drw->scheme->fg.pix); XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
if (filled) if (filled)
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
else else
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
} }
int int
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) { drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
{
char buf[1024]; char buf[1024];
int tx, ty, th; int ty;
unsigned int ew = 0; unsigned int ew;
Colormap cmap; XftDraw *d = NULL;
Visual *vis;
XftDraw *d;
Fnt *usedfont, *curfont, *nextfont; Fnt *usedfont, *curfont, *nextfont;
size_t i, len; size_t i, len;
int utf8strlen, utf8charlen, render; int utf8strlen, utf8charlen, render = x || y || w || h;
long utf8codepoint = 0; long utf8codepoint = 0;
const char *utf8str; const char *utf8str;
FcCharSet *fccharset; FcCharSet *fccharset;
@ -243,23 +252,19 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
XftResult result; XftResult result;
int charexists = 0; int charexists = 0;
if (!(render = x || y || w || h)) { if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
return 0;
if (!render) {
w = ~w; w = ~w;
} } else {
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
if (!drw || !drw->scheme) {
return 0;
} else if (render) {
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg.pix : drw->scheme->bg.pix);
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
} d = XftDrawCreate(drw->dpy, drw->drawable,
DefaultVisual(drw->dpy, drw->screen),
if (!text || !drw->fonts) { DefaultColormap(drw->dpy, drw->screen));
return 0; x += lpad;
} else if (render) { w -= lpad;
cmap = DefaultColormap(drw->dpy, drw->screen);
vis = DefaultVisual(drw->dpy, drw->screen);
d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap);
} }
usedfont = drw->fonts; usedfont = drw->fonts;
@ -282,32 +287,30 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
} }
} }
if (!charexists || nextfont) { if (!charexists || nextfont)
break; break;
} else { else
charexists = 0; charexists = 0;
} }
}
if (utf8strlen) { if (utf8strlen) {
drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL); drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
/* shorten text if necessary */ /* shorten text if necessary */
for(len = MIN(utf8strlen, (sizeof buf) - 1); len && (ew > w - drw->fonts->h || w < drw->fonts->h); len--) for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
drw_font_getexts(usedfont, utf8str, len, &ew, NULL); drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
if (len) { if (len) {
memcpy(buf, utf8str, len); memcpy(buf, utf8str, len);
buf[len] = '\0'; buf[len] = '\0';
if (len < utf8strlen) if (len < utf8strlen)
for(i = len; i && i > len - 3; buf[--i] = '.'); for (i = len; i && i > len - 3; buf[--i] = '.')
; /* NOP */
if (render) { if (render) {
th = usedfont->ascent + usedfont->descent; ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
ty = y + (h / 2) - (th / 2) + usedfont->ascent; XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
tx = x + (h / 2); usedfont->xfont, x, ty, (XftChar8 *)buf, len);
XftDrawStringUtf8(d, invert ? &drw->scheme->bg.rgb : &drw->scheme->fg.rgb, usedfont->xfont, tx, ty, (XftChar8 *)buf, len);
} }
x += ew; x += ew;
w -= ew; w -= ew;
} }
@ -320,18 +323,15 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
usedfont = nextfont; usedfont = nextfont;
} else { } else {
/* Regardless of whether or not a fallback font is found, the /* Regardless of whether or not a fallback font is found, the
* character must be drawn. * character must be drawn. */
*/
charexists = 1; charexists = 1;
fccharset = FcCharSetCreate(); fccharset = FcCharSetCreate();
FcCharSetAddChar(fccharset, utf8codepoint); FcCharSetAddChar(fccharset, utf8codepoint);
if (!drw->fonts->pattern) { if (!drw->fonts->pattern) {
/* Refer to the comment in drw_font_xcreate for more /* Refer to the comment in xfont_create for more information. */
* information. die("the first font in the cache must be loaded from a font string.\n");
*/
die("The first font in the cache must be loaded from a font string.\n");
} }
fcpattern = FcPatternDuplicate(drw->fonts->pattern); fcpattern = FcPatternDuplicate(drw->fonts->pattern);
@ -346,47 +346,50 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
FcPatternDestroy(fcpattern); FcPatternDestroy(fcpattern);
if (match) { if (match) {
usedfont = drw_font_xcreate(drw, NULL, match); usedfont = xfont_create(drw, NULL, match);
if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
for (curfont = drw->fonts; curfont->next; curfont = curfont->next) for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
; /* just find the end of the linked list */ ; /* NOP */
curfont->next = usedfont; curfont->next = usedfont;
} else { } else {
drw_xfont_free(usedfont); xfont_free(usedfont);
usedfont = drw->fonts; usedfont = drw->fonts;
} }
} }
} }
} }
if (d)
if (render) {
XftDrawDestroy(d); XftDrawDestroy(d);
}
return x; return x + (render ? w : 0);
} }
void void
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) { drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
{
if (!drw) if (!drw)
return; return;
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
XSync(drw->dpy, False); XSync(drw->dpy, False);
} }
unsigned int unsigned int
drw_fontset_getwidth(Drw *drw, const char *text) { drw_fontset_getwidth(Drw *drw, const char *text)
{
if (!drw || !drw->fonts || !text) if (!drw || !drw->fonts || !text)
return 0; return 0;
return drw_text(drw, 0, 0, 0, 0, text, 0); return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
} }
void void
drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) { drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
{
XGlyphInfo ext; XGlyphInfo ext;
if (!font || !text) if (!font || !text)
return; return;
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
if (w) if (w)
*w = ext.xOff; *w = ext.xOff;
@ -395,19 +398,24 @@ drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w,
} }
Cur * Cur *
drw_cur_create(Drw *drw, int shape) { drw_cur_create(Drw *drw, int shape)
{
Cur *cur; Cur *cur;
if(!drw || !(cur = (Cur *)calloc(1, sizeof(Cur)))) if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
return NULL; return NULL;
cur->cursor = XCreateFontCursor(drw->dpy, shape); cur->cursor = XCreateFontCursor(drw->dpy, shape);
return cur; return cur;
} }
void void
drw_cur_free(Drw *drw, Cur *cursor) { drw_cur_free(Drw *drw, Cur *cursor)
if(!drw || !cursor) {
if (!cursor)
return; return;
XFreeCursor(drw->dpy, cursor->cursor); XFreeCursor(drw->dpy, cursor->cursor);
free(cursor); free(cursor);
} }

28
drw.h
View File

@ -1,27 +1,19 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#define DRW_FONT_CACHE_SIZE 32
typedef struct { typedef struct {
Cursor cursor; Cursor cursor;
} Cur; } Cur;
typedef struct Fnt Fnt; typedef struct Fnt {
struct Fnt {
Display *dpy; Display *dpy;
int ascent;
int descent;
unsigned int h; unsigned int h;
XftFont *xfont; XftFont *xfont;
FcPattern *pattern; FcPattern *pattern;
Fnt *next; struct Fnt *next;
}; } Fnt;
typedef struct { enum { ColFg, ColBg }; /* Clr scheme index */
struct { typedef XftColor Clr;
unsigned long pix;
XftColor rgb;
} fg, bg;
} Scm;
typedef struct { typedef struct {
unsigned int w, h; unsigned int w, h;
@ -30,7 +22,7 @@ typedef struct {
Window root; Window root;
Drawable drawable; Drawable drawable;
GC gc; GC gc;
Scm *scheme; Clr *scheme;
Fnt *fonts; Fnt *fonts;
} Drw; } Drw;
@ -46,8 +38,8 @@ unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
/* Colorscheme abstraction */ /* Colorscheme abstraction */
Scm *drw_scm_create(Drw *drw, const char *fgname, const char *bgname); void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
void drw_scm_free(Scm *scm); Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
/* Cursor abstraction */ /* Cursor abstraction */
Cur *drw_cur_create(Drw *drw, int shape); Cur *drw_cur_create(Drw *drw, int shape);
@ -55,11 +47,11 @@ void drw_cur_free(Drw *drw, Cur *cursor);
/* Drawing context manipulation */ /* Drawing context manipulation */
void drw_setfontset(Drw *drw, Fnt *set); void drw_setfontset(Drw *drw, Fnt *set);
void drw_setscheme(Drw *drw, Scm *scm); void drw_setscheme(Drw *drw, Clr *scm);
/* Drawing functions */ /* Drawing functions */
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert); void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert);
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert); int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert);
/* Map functions */ /* Map functions */
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h); void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);

85
sent.c
View File

@ -20,6 +20,7 @@
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include "arg.h" #include "arg.h"
#include "util.h"
#include "drw.h" #include "drw.h"
char *argv0; char *argv0;
@ -96,7 +97,6 @@ static void ffdraw(Image *img);
static void getfontsize(Slide *s, unsigned int *width, unsigned int *height); static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
static void cleanup(); static void cleanup();
static void die(const char *, ...);
static void load(FILE *fp); static void load(FILE *fp);
static void advance(const Arg *arg); static void advance(const Arg *arg);
static void quit(const Arg *arg); static void quit(const Arg *arg);
@ -123,7 +123,7 @@ static int idx = 0;
static int slidecount = 0; static int slidecount = 0;
static XWindow xw; static XWindow xw;
static Drw *d = NULL; static Drw *d = NULL;
static Scm *sc; static Clr *sc;
static Fnt *fonts[NUMFONTSCALES]; static Fnt *fonts[NUMFONTSCALES];
static int running = 1; static int running = 1;
@ -141,18 +141,18 @@ filter(int fd, const char *cmd)
int fds[2]; int fds[2];
if (pipe(fds) < 0) if (pipe(fds) < 0)
die("Unable to create pipe:"); die("sent: Unable to create pipe:");
switch (fork()) { switch (fork()) {
case -1: case -1:
die("Unable to fork:"); die("sent: Unable to fork:");
case 0: case 0:
dup2(fd, 0); dup2(fd, 0);
dup2(fds[1], 1); dup2(fds[1], 1);
close(fds[0]); close(fds[0]);
close(fds[1]); close(fds[1]);
execlp("sh", "sh", "-c", cmd, (char *)0); execlp("sh", "sh", "-c", cmd, (char *)0);
die("execlp sh -c '%s':", cmd); die("sent: execlp sh -c '%s':", cmd);
} }
close(fds[1]); close(fds[1]);
return fds[0]; return fds[0];
@ -182,13 +182,13 @@ ffopen(char *filename)
return NULL; return NULL;
if ((fd = open(filename, O_RDONLY)) < 0) { if ((fd = open(filename, O_RDONLY)) < 0) {
die("Unable to open file %s:", filename); die("sent: Unable to open file %s:", filename);
} }
tmpfd = fd; tmpfd = fd;
fd = filter(fd, bin); fd = filter(fd, bin);
if (fd < 0) if (fd < 0)
die("Unable to filter %s:", filename); die("sent: Unable to filter %s:", filename);
close(tmpfd); close(tmpfd);
if (read(fd, hdr, 16) != 16) if (read(fd, hdr, 16) != 16)
@ -231,26 +231,26 @@ ffread(Image *img)
free(img->buf); free(img->buf);
/* internally the image is stored in 888 format */ /* internally the image is stored in 888 format */
if (!(img->buf = malloc(3 * img->bufwidth * img->bufheight))) if (!(img->buf = malloc(3 * img->bufwidth * img->bufheight)))
die("Unable to malloc buffer for image."); die("sent: Unable to malloc buffer for image.\n");
/* scratch buffer to read row by row */ /* scratch buffer to read row by row */
rowlen = img->bufwidth * 2 * strlen("RGBA"); rowlen = img->bufwidth * 2 * strlen("RGBA");
row = malloc(rowlen); row = malloc(rowlen);
if (!row) { if (!row) {
die("Unable to malloc buffer for image row."); die("sent: Unable to malloc buffer for image row.\n");
} }
/* extract window background color channels for transparency */ /* extract window background color channels for transparency */
bg_r = (sc->bg.pix >> 16) % 256; bg_r = (sc[ColBg].pixel >> 16) % 256;
bg_g = (sc->bg.pix >> 8) % 256; bg_g = (sc[ColBg].pixel >> 8) % 256;
bg_b = (sc->bg.pix >> 0) % 256; bg_b = (sc[ColBg].pixel >> 0) % 256;
for (off = 0, y = 0; y < img->bufheight; y++) { for (off = 0, y = 0; y < img->bufheight; y++) {
nbytes = 0; nbytes = 0;
while (nbytes < rowlen) { while (nbytes < rowlen) {
count = read(img->fd, (char *)row + nbytes, rowlen - nbytes); count = read(img->fd, (char *)row + nbytes, rowlen - nbytes);
if (count < 0) if (count < 0)
die("Unable to read from pipe:"); die("sent: Unable to read from pipe:");
nbytes += count; nbytes += count;
} }
for (x = 0; x < rowlen / 2; x += 4) { for (x = 0; x < rowlen / 2; x += 4) {
@ -284,17 +284,17 @@ ffprepare(Image *img)
height = img->bufheight * xw.uw / img->bufwidth; height = img->bufheight * xw.uw / img->bufwidth;
if (depth < 24) if (depth < 24)
die("Display depths <24 not supported."); die("sent: Display depths <24 not supported.\n");
if (!(img->ximg = XCreateImage(xw.dpy, CopyFromParent, depth, ZPixmap, 0, if (!(img->ximg = XCreateImage(xw.dpy, CopyFromParent, depth, ZPixmap, 0,
NULL, width, height, 32, 0))) NULL, width, height, 32, 0)))
die("Unable to create XImage."); die("sent: Unable to create XImage.\n");
if (!(img->ximg->data = malloc(img->ximg->bytes_per_line * height))) if (!(img->ximg->data = malloc(img->ximg->bytes_per_line * height)))
die("Unable to alloc data section for XImage."); die("sent: Unable to alloc data section for XImage.\n");
if (!XInitImage(img->ximg)) if (!XInitImage(img->ximg))
die("Unable to init XImage."); die("sent: Unable to init XImage.\n");
ffscale(img); ffscale(img);
img->state |= SCALED; img->state |= SCALED;
@ -364,7 +364,6 @@ getfontsize(Slide *s, unsigned int *width, unsigned int *height)
*width = curw; *width = curw;
} }
*height = fonts[j]->h * lfac; *height = fonts[j]->h * lfac;
*width += fonts[j]->h;
} }
void void
@ -374,7 +373,7 @@ cleanup()
for (i = 0; i < NUMFONTSCALES; i++) for (i = 0; i < NUMFONTSCALES; i++)
drw_fontset_free(fonts[i]); drw_fontset_free(fonts[i]);
drw_scm_free(sc); free(sc);
drw_free(d); drw_free(d);
XDestroyWindow(xw.dpy, xw.win); XDestroyWindow(xw.dpy, xw.win);
@ -393,27 +392,6 @@ cleanup()
} }
} }
void
die(const char *fmt, ...)
{
va_list ap;
fputs("sent: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
void void
load(FILE *fp) load(FILE *fp)
{ {
@ -433,7 +411,7 @@ load(FILE *fp)
if ((slidecount+1) * sizeof(*slides) >= size) if ((slidecount+1) * sizeof(*slides) >= size)
if (!(slides = realloc(slides, (size += BUFSIZ)))) if (!(slides = realloc(slides, (size += BUFSIZ))))
die("Unable to realloc %u bytes:", size); die("sent: Unable to realloc %u bytes:", size);
/* read one slide */ /* read one slide */
maxlines = 0; maxlines = 0;
@ -446,12 +424,12 @@ load(FILE *fp)
if (s->linecount >= maxlines) { if (s->linecount >= maxlines) {
maxlines = 2 * s->linecount + 1; maxlines = 2 * s->linecount + 1;
if (!(s->lines = realloc(s->lines, maxlines * sizeof(s->lines[0])))) if (!(s->lines = realloc(s->lines, maxlines * sizeof(s->lines[0]))))
die("Unable to realloc %u bytes:", maxlines * sizeof(s->lines[0])); die("sent: Unable to realloc %u bytes:", maxlines * sizeof(s->lines[0]));
} }
blen = strlen(buf); blen = strlen(buf);
if (!(s->lines[s->linecount] = strdup(buf))) if (!(s->lines[s->linecount] = strdup(buf)))
die("Unable to strdup:"); die("sent: Unable to strdup:");
if (s->lines[s->linecount][blen-1] == '\n') if (s->lines[s->linecount][blen-1] == '\n')
s->lines[s->linecount][blen-1] = '\0'; s->lines[s->linecount][blen-1] = '\0';
@ -543,6 +521,7 @@ xdraw()
(xw.h - height) / 2 + i * linespacing * d->fonts->h, (xw.h - height) / 2 + i * linespacing * d->fonts->h,
width, width,
d->fonts->h, d->fonts->h,
0,
slides[idx].lines[i], slides[idx].lines[i],
0); 0);
drw_map(d, xw.win, 0, 0, xw.w, xw.h); drw_map(d, xw.win, 0, 0, xw.w, xw.h);
@ -564,7 +543,7 @@ xhints()
XSizeHints *sizeh = NULL; XSizeHints *sizeh = NULL;
if (!(sizeh = XAllocSizeHints())) if (!(sizeh = XAllocSizeHints()))
die("Unable to alloc size hints."); die("sent: Unable to alloc size hints.\n");
sizeh->flags = PSize; sizeh->flags = PSize;
sizeh->height = xw.h; sizeh->height = xw.h;
@ -580,7 +559,7 @@ xinit()
XTextProperty prop; XTextProperty prop;
if (!(xw.dpy = XOpenDisplay(NULL))) if (!(xw.dpy = XOpenDisplay(NULL)))
die("Unable to open display."); die("sent: Unable to open display.\n");
xw.scr = XDefaultScreen(xw.dpy); xw.scr = XDefaultScreen(xw.dpy);
xw.vis = XDefaultVisual(xw.dpy, xw.scr); xw.vis = XDefaultVisual(xw.dpy, xw.scr);
resize(DisplayWidth(xw.dpy, xw.scr), DisplayHeight(xw.dpy, xw.scr)); resize(DisplayWidth(xw.dpy, xw.scr), DisplayHeight(xw.dpy, xw.scr));
@ -599,10 +578,10 @@ xinit()
XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1); XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
if (!(d = drw_create(xw.dpy, xw.scr, xw.win, xw.w, xw.h))) if (!(d = drw_create(xw.dpy, xw.scr, xw.win, xw.w, xw.h)))
die("Unable to create drawing context."); die("sent: Unable to create drawing context.\n");
sc = drw_scm_create(d, fgcol, bgcol); sc = drw_scm_create(d, colors, 2);
drw_setscheme(d, sc); drw_setscheme(d, sc);
XSetWindowBackground(xw.dpy, xw.win, sc->bg.pix); XSetWindowBackground(xw.dpy, xw.win, sc[ColBg].pixel);
xloadfonts(); xloadfonts();
@ -623,16 +602,16 @@ xloadfonts()
for (j = 0; j < LEN(fontfallbacks); j++) { for (j = 0; j < LEN(fontfallbacks); j++) {
if (!(fstrs[j] = malloc(MAXFONTSTRLEN))) if (!(fstrs[j] = malloc(MAXFONTSTRLEN)))
die("Unable to malloc fstrs."); die("sent: Unable to malloc fstrs.\n");
} }
for (i = 0; i < NUMFONTSCALES; i++) { for (i = 0; i < NUMFONTSCALES; i++) {
for (j = 0; j < LEN(fontfallbacks); j++) { for (j = 0; j < LEN(fontfallbacks); j++) {
if (MAXFONTSTRLEN < snprintf(fstrs[j], MAXFONTSTRLEN, "%s:size=%d", fontfallbacks[j], FONTSZ(i))) if (MAXFONTSTRLEN < snprintf(fstrs[j], MAXFONTSTRLEN, "%s:size=%d", fontfallbacks[j], FONTSZ(i)))
die("Font string too long."); die("sent: Font string too long.\n");
} }
if (!(fonts[i] = drw_fontset_create(d, (const char**)fstrs, LEN(fstrs)))) if (!(fonts[i] = drw_fontset_create(d, (const char**)fstrs, LEN(fstrs))))
die("Unable to load any font for size %d.", FONTSZ(i)); die("sent: Unable to load any font for size %d.\n", FONTSZ(i));
} }
for (j = 0; j < LEN(fontfallbacks); j++) for (j = 0; j < LEN(fontfallbacks); j++)
@ -689,7 +668,7 @@ void
usage() usage()
{ {
die("sent " VERSION " (c) 2014-2015 markus.teich@stusta.mhn.de\n" \ die("sent " VERSION " (c) 2014-2015 markus.teich@stusta.mhn.de\n" \
"usage: sent [FILE]", argv0); "usage: sent [FILE]\n", argv0);
} }
int int
@ -706,7 +685,7 @@ main(int argc, char *argv[])
if (!argv[0] || !strcmp(argv[0], "-")) if (!argv[0] || !strcmp(argv[0], "-"))
fp = stdin; fp = stdin;
else if (!(fp = fopen(argv[0], "r"))) else if (!(fp = fopen(argv[0], "r")))
die("Unable to open '%s' for reading:", argv[0]); die("sent: Unable to open '%s' for reading:", argv[0]);
load(fp); load(fp);
fclose(fp); fclose(fp);

30
util.c
View File

@ -2,16 +2,32 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "util.h" #include "util.h"
void void *
die(const char *errstr, ...) { ecalloc(size_t nmemb, size_t size)
va_list ap; {
void *p;
va_start(ap, errstr); if (!(p = calloc(nmemb, size)))
vfprintf(stderr, errstr, ap); perror(NULL);
va_end(ap); return p;
exit(1);
} }
void
die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
}
exit(1);
}

3
util.h
View File

@ -4,4 +4,5 @@
#define MIN(A, B) ((A) < (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B))
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
void die(const char *errstr, ...); void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);