mirror of
git://git.suckless.org/dmenu
synced 2025-04-20 03:24:46 +01:00
Compare commits
No commits in common. "master" and "5.1" have entirely different histories.
10
Makefile
10
Makefile
@ -6,7 +6,13 @@ include config.mk
|
|||||||
SRC = drw.c dmenu.c stest.c util.c
|
SRC = drw.c dmenu.c stest.c util.c
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
all: dmenu stest
|
all: options dmenu stest
|
||||||
|
|
||||||
|
options:
|
||||||
|
@echo dmenu build options:
|
||||||
|
@echo "CFLAGS = $(CFLAGS)"
|
||||||
|
@echo "LDFLAGS = $(LDFLAGS)"
|
||||||
|
@echo "CC = $(CC)"
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) -c $(CFLAGS) $<
|
$(CC) -c $(CFLAGS) $<
|
||||||
@ -55,4 +61,4 @@ uninstall:
|
|||||||
$(DESTDIR)$(MANPREFIX)/man1/dmenu.1\
|
$(DESTDIR)$(MANPREFIX)/man1/dmenu.1\
|
||||||
$(DESTDIR)$(MANPREFIX)/man1/stest.1
|
$(DESTDIR)$(MANPREFIX)/man1/stest.1
|
||||||
|
|
||||||
.PHONY: all clean dist install uninstall
|
.PHONY: all options clean dist install uninstall
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# dmenu version
|
# dmenu version
|
||||||
VERSION = 5.3
|
VERSION = 5.1
|
||||||
|
|
||||||
# paths
|
# paths
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
@ -17,7 +17,6 @@ FREETYPELIBS = -lfontconfig -lXft
|
|||||||
FREETYPEINC = /usr/include/freetype2
|
FREETYPEINC = /usr/include/freetype2
|
||||||
# OpenBSD (uncomment)
|
# OpenBSD (uncomment)
|
||||||
#FREETYPEINC = $(X11INC)/freetype2
|
#FREETYPEINC = $(X11INC)/freetype2
|
||||||
#MANPREFIX = ${PREFIX}/man
|
|
||||||
|
|
||||||
# includes and libs
|
# includes and libs
|
||||||
INCS = -I$(X11INC) -I$(FREETYPEINC)
|
INCS = -I$(X11INC) -I$(FREETYPEINC)
|
||||||
|
82
dmenu.c
82
dmenu.c
@ -22,6 +22,7 @@
|
|||||||
/* macros */
|
/* macros */
|
||||||
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
||||||
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
||||||
|
#define LENGTH(X) (sizeof X / sizeof X[0])
|
||||||
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
|
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
|
||||||
|
|
||||||
/* enums */
|
/* enums */
|
||||||
@ -57,13 +58,6 @@ static Clr *scheme[SchemeLast];
|
|||||||
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
|
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
|
||||||
static char *(*fstrstr)(const char *, const char *) = strstr;
|
static char *(*fstrstr)(const char *, const char *) = strstr;
|
||||||
|
|
||||||
static unsigned int
|
|
||||||
textw_clamp(const char *str, unsigned int n)
|
|
||||||
{
|
|
||||||
unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad;
|
|
||||||
return MIN(w, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
appenditem(struct item *item, struct item **list, struct item **last)
|
appenditem(struct item *item, struct item **list, struct item **last)
|
||||||
{
|
{
|
||||||
@ -88,10 +82,10 @@ calcoffsets(void)
|
|||||||
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
|
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
|
||||||
/* calculate which items will begin the next page and previous page */
|
/* calculate which items will begin the next page and previous page */
|
||||||
for (i = 0, next = curr; next; next = next->right)
|
for (i = 0, next = curr; next; next = next->right)
|
||||||
if ((i += (lines > 0) ? bh : textw_clamp(next->text, n)) > n)
|
if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n)
|
||||||
break;
|
break;
|
||||||
for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
|
for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
|
||||||
if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n)
|
if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,12 +94,9 @@ cleanup(void)
|
|||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
XUngrabKeyboard(dpy, CurrentTime);
|
XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
||||||
for (i = 0; i < SchemeLast; i++)
|
for (i = 0; i < SchemeLast; i++)
|
||||||
free(scheme[i]);
|
free(scheme[i]);
|
||||||
for (i = 0; items && items[i].text; ++i)
|
|
||||||
free(items[i].text);
|
|
||||||
free(items);
|
|
||||||
drw_free(drw);
|
drw_free(drw);
|
||||||
XSync(dpy, False);
|
XSync(dpy, False);
|
||||||
XCloseDisplay(dpy);
|
XCloseDisplay(dpy);
|
||||||
@ -181,7 +172,7 @@ drawmenu(void)
|
|||||||
}
|
}
|
||||||
x += w;
|
x += w;
|
||||||
for (item = curr; item != next; item = item->right)
|
for (item = curr; item != next; item = item->right)
|
||||||
x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">")));
|
x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
|
||||||
if (next) {
|
if (next) {
|
||||||
w = TEXTW(">");
|
w = TEXTW(">");
|
||||||
drw_setscheme(drw, scheme[SchemeNorm]);
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
||||||
@ -241,7 +232,7 @@ match(void)
|
|||||||
/* separate input text into tokens to be matched individually */
|
/* separate input text into tokens to be matched individually */
|
||||||
for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
|
for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
|
||||||
if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
|
if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
|
||||||
die("cannot realloc %zu bytes:", tokn * sizeof *tokv);
|
die("cannot realloc %u bytes:", tokn * sizeof *tokv);
|
||||||
len = tokc ? strlen(tokv[0]) : 0;
|
len = tokc ? strlen(tokv[0]) : 0;
|
||||||
|
|
||||||
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
|
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
|
||||||
@ -323,19 +314,19 @@ movewordedge(int dir)
|
|||||||
static void
|
static void
|
||||||
keypress(XKeyEvent *ev)
|
keypress(XKeyEvent *ev)
|
||||||
{
|
{
|
||||||
char buf[64];
|
char buf[32];
|
||||||
int len;
|
int len;
|
||||||
KeySym ksym = NoSymbol;
|
KeySym ksym;
|
||||||
Status status;
|
Status status;
|
||||||
|
|
||||||
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
|
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
default: /* XLookupNone, XBufferOverflow */
|
default: /* XLookupNone, XBufferOverflow */
|
||||||
return;
|
return;
|
||||||
case XLookupChars: /* composed string from input method */
|
case XLookupChars:
|
||||||
goto insert;
|
goto insert;
|
||||||
case XLookupKeySym:
|
case XLookupKeySym:
|
||||||
case XLookupBoth: /* a KeySym and a string are returned: use keysym */
|
case XLookupBoth:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -414,7 +405,7 @@ keypress(XKeyEvent *ev)
|
|||||||
switch(ksym) {
|
switch(ksym) {
|
||||||
default:
|
default:
|
||||||
insert:
|
insert:
|
||||||
if (!iscntrl((unsigned char)*buf))
|
if (!iscntrl(*buf))
|
||||||
insert(buf, len);
|
insert(buf, len);
|
||||||
break;
|
break;
|
||||||
case XK_Delete:
|
case XK_Delete:
|
||||||
@ -516,9 +507,9 @@ insert:
|
|||||||
case XK_Tab:
|
case XK_Tab:
|
||||||
if (!sel)
|
if (!sel)
|
||||||
return;
|
return;
|
||||||
cursor = strnlen(sel->text, sizeof text - 1);
|
strncpy(text, sel->text, sizeof text - 1);
|
||||||
memcpy(text, sel->text, cursor);
|
text[sizeof text - 1] = '\0';
|
||||||
text[cursor] = '\0';
|
cursor = strlen(text);
|
||||||
match();
|
match();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -548,27 +539,29 @@ paste(void)
|
|||||||
static void
|
static void
|
||||||
readstdin(void)
|
readstdin(void)
|
||||||
{
|
{
|
||||||
char *line = NULL;
|
char buf[sizeof text], *p;
|
||||||
size_t i, itemsiz = 0, linesiz = 0;
|
size_t i, imax = 0, size = 0;
|
||||||
ssize_t len;
|
unsigned int tmpmax = 0;
|
||||||
|
|
||||||
/* read each line from stdin and add it to the item list */
|
/* read each line from stdin and add it to the item list */
|
||||||
for (i = 0; (len = getline(&line, &linesiz, stdin)) != -1; i++) {
|
for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
|
||||||
if (i + 1 >= itemsiz) {
|
if (i + 1 >= size / sizeof *items)
|
||||||
itemsiz += 256;
|
if (!(items = realloc(items, (size += BUFSIZ))))
|
||||||
if (!(items = realloc(items, itemsiz * sizeof(*items))))
|
die("cannot realloc %u bytes:", size);
|
||||||
die("cannot realloc %zu bytes:", itemsiz * sizeof(*items));
|
if ((p = strchr(buf, '\n')))
|
||||||
}
|
*p = '\0';
|
||||||
if (line[len - 1] == '\n')
|
if (!(items[i].text = strdup(buf)))
|
||||||
line[len - 1] = '\0';
|
die("cannot strdup %u bytes:", strlen(buf) + 1);
|
||||||
if (!(items[i].text = strdup(line)))
|
|
||||||
die("strdup:");
|
|
||||||
|
|
||||||
items[i].out = 0;
|
items[i].out = 0;
|
||||||
|
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
|
||||||
|
if (tmpmax > inputw) {
|
||||||
|
inputw = tmpmax;
|
||||||
|
imax = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
free(line);
|
|
||||||
if (items)
|
if (items)
|
||||||
items[i].text = NULL;
|
items[i].text = NULL;
|
||||||
|
inputw = items ? TEXTW(items[imax].text) : 0;
|
||||||
lines = MIN(lines, i);
|
lines = MIN(lines, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -659,7 +652,7 @@ setup(void)
|
|||||||
/* no focused window is on screen, so use pointer location instead */
|
/* no focused window is on screen, so use pointer location instead */
|
||||||
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
|
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
if (INTERSECT(x, y, 1, 1, info[i]) != 0)
|
if (INTERSECT(x, y, 1, 1, info[i]))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
x = info[i].x_org;
|
x = info[i].x_org;
|
||||||
@ -677,18 +670,19 @@ setup(void)
|
|||||||
mw = wa.width;
|
mw = wa.width;
|
||||||
}
|
}
|
||||||
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
|
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
|
||||||
inputw = mw / 3; /* input width: ~33% of monitor width */
|
inputw = MIN(inputw, mw/3);
|
||||||
match();
|
match();
|
||||||
|
|
||||||
/* create menu window */
|
/* create menu window */
|
||||||
swa.override_redirect = True;
|
swa.override_redirect = True;
|
||||||
swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
|
swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
|
||||||
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
||||||
win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
|
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
|
||||||
CopyFromParent, CopyFromParent, CopyFromParent,
|
CopyFromParent, CopyFromParent, CopyFromParent,
|
||||||
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
||||||
XSetClassHint(dpy, win, &ch);
|
XSetClassHint(dpy, win, &ch);
|
||||||
|
|
||||||
|
|
||||||
/* input methods */
|
/* input methods */
|
||||||
if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
|
if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
|
||||||
die("XOpenIM failed: could not open input device");
|
die("XOpenIM failed: could not open input device");
|
||||||
@ -698,7 +692,6 @@ setup(void)
|
|||||||
|
|
||||||
XMapRaised(dpy, win);
|
XMapRaised(dpy, win);
|
||||||
if (embed) {
|
if (embed) {
|
||||||
XReparentWindow(dpy, win, parentwin, x, y);
|
|
||||||
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
|
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
|
||||||
if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
|
if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
|
||||||
for (i = 0; i < du && dws[i] != win; ++i)
|
for (i = 0; i < du && dws[i] != win; ++i)
|
||||||
@ -714,8 +707,9 @@ setup(void)
|
|||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
|
fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
|
||||||
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
|
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
178
drw.c
178
drw.c
@ -9,40 +9,54 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#define UTF_INVALID 0xFFFD
|
#define UTF_INVALID 0xFFFD
|
||||||
|
#define UTF_SIZ 4
|
||||||
|
|
||||||
static int
|
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
|
||||||
utf8decode(const char *s_in, long *u, int *err)
|
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 utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
|
||||||
|
|
||||||
|
static long
|
||||||
|
utf8decodebyte(const char c, size_t *i)
|
||||||
{
|
{
|
||||||
static const unsigned char lens[] = {
|
for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
|
||||||
/* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
|
||||||
/* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0, /* invalid */
|
return (unsigned char)c & ~utfmask[*i];
|
||||||
/* 110XX */ 2, 2, 2, 2,
|
return 0;
|
||||||
/* 1110X */ 3, 3,
|
}
|
||||||
/* 11110 */ 4,
|
|
||||||
/* 11111 */ 0, /* invalid */
|
|
||||||
};
|
|
||||||
static const unsigned char leading_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
|
|
||||||
static const unsigned int overlong[] = { 0x0, 0x80, 0x0800, 0x10000 };
|
|
||||||
|
|
||||||
const unsigned char *s = (const unsigned char *)s_in;
|
static size_t
|
||||||
int len = lens[*s >> 3];
|
utf8validate(long *u, size_t i)
|
||||||
|
{
|
||||||
|
if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
|
||||||
*u = UTF_INVALID;
|
*u = UTF_INVALID;
|
||||||
*err = 1;
|
for (i = 1; *u > utfmax[i]; ++i)
|
||||||
if (len == 0)
|
;
|
||||||
return 1;
|
|
||||||
|
|
||||||
long cp = s[0] & leading_mask[len - 1];
|
|
||||||
for (int i = 1; i < len; ++i) {
|
|
||||||
if (s[i] == '\0' || (s[i] & 0xC0) != 0x80)
|
|
||||||
return i;
|
return i;
|
||||||
cp = (cp << 6) | (s[i] & 0x3F);
|
}
|
||||||
}
|
|
||||||
/* out of range, surrogate, overlong encoding */
|
static size_t
|
||||||
if (cp > 0x10FFFF || (cp >> 11) == 0x1B || cp < overlong[len - 1])
|
utf8decode(const char *c, long *u, size_t clen)
|
||||||
return len;
|
{
|
||||||
|
size_t i, j, len, type;
|
||||||
|
long udecoded;
|
||||||
|
|
||||||
|
*u = UTF_INVALID;
|
||||||
|
if (!clen)
|
||||||
|
return 0;
|
||||||
|
udecoded = utf8decodebyte(c[0], &len);
|
||||||
|
if (!BETWEEN(len, 1, UTF_SIZ))
|
||||||
|
return 1;
|
||||||
|
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
|
||||||
|
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
|
||||||
|
if (type)
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
if (j < len)
|
||||||
|
return 0;
|
||||||
|
*u = udecoded;
|
||||||
|
utf8validate(u, len);
|
||||||
|
|
||||||
*err = 0;
|
|
||||||
*u = cp;
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +133,19 @@ xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
|
|||||||
die("no font specified.");
|
die("no font specified.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Do not allow using color fonts. This is a workaround for a BadLength
|
||||||
|
* error from Xft with color glyphs. Modelled on the Xterm workaround. See
|
||||||
|
* https://bugzilla.redhat.com/show_bug.cgi?id=1498269
|
||||||
|
* https://lists.suckless.org/dev/1701/30932.html
|
||||||
|
* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
|
||||||
|
* and lots more all over the internet.
|
||||||
|
*/
|
||||||
|
FcBool iscol;
|
||||||
|
if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
|
||||||
|
XftFontClose(drw->dpy, xfont);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
font = ecalloc(1, sizeof(Fnt));
|
font = ecalloc(1, sizeof(Fnt));
|
||||||
font->xfont = xfont;
|
font->xfont = xfont;
|
||||||
font->pattern = pattern;
|
font->pattern = pattern;
|
||||||
@ -224,32 +251,29 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
|
|||||||
int
|
int
|
||||||
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, 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)
|
||||||
{
|
{
|
||||||
int ty, ellipsis_x = 0;
|
char buf[1024];
|
||||||
unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
|
int ty;
|
||||||
|
unsigned int ew;
|
||||||
XftDraw *d = NULL;
|
XftDraw *d = NULL;
|
||||||
Fnt *usedfont, *curfont, *nextfont;
|
Fnt *usedfont, *curfont, *nextfont;
|
||||||
int utf8strlen, utf8charlen, utf8err, render = x || y || w || h;
|
size_t i, len;
|
||||||
|
int utf8strlen, utf8charlen, render = x || y || w || h;
|
||||||
long utf8codepoint = 0;
|
long utf8codepoint = 0;
|
||||||
const char *utf8str;
|
const char *utf8str;
|
||||||
FcCharSet *fccharset;
|
FcCharSet *fccharset;
|
||||||
FcPattern *fcpattern;
|
FcPattern *fcpattern;
|
||||||
FcPattern *match;
|
FcPattern *match;
|
||||||
XftResult result;
|
XftResult result;
|
||||||
int charexists = 0, overflow = 0;
|
int charexists = 0;
|
||||||
/* keep track of a couple codepoints for which we have no match. */
|
|
||||||
static unsigned int nomatches[128], ellipsis_width, invalid_width;
|
|
||||||
static const char invalid[] = "<EFBFBD>";
|
|
||||||
|
|
||||||
if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
|
if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!render) {
|
if (!render) {
|
||||||
w = invert ? invert : ~invert;
|
w = ~w;
|
||||||
} else {
|
} else {
|
||||||
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
||||||
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
||||||
if (w < lpad)
|
|
||||||
return x + w;
|
|
||||||
d = XftDrawCreate(drw->dpy, drw->drawable,
|
d = XftDrawCreate(drw->dpy, drw->drawable,
|
||||||
DefaultVisual(drw->dpy, drw->screen),
|
DefaultVisual(drw->dpy, drw->screen),
|
||||||
DefaultColormap(drw->dpy, drw->screen));
|
DefaultColormap(drw->dpy, drw->screen));
|
||||||
@ -258,40 +282,18 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||||||
}
|
}
|
||||||
|
|
||||||
usedfont = drw->fonts;
|
usedfont = drw->fonts;
|
||||||
if (!ellipsis_width && render)
|
|
||||||
ellipsis_width = drw_fontset_getwidth(drw, "...");
|
|
||||||
if (!invalid_width && render)
|
|
||||||
invalid_width = drw_fontset_getwidth(drw, invalid);
|
|
||||||
while (1) {
|
while (1) {
|
||||||
ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
|
utf8strlen = 0;
|
||||||
utf8str = text;
|
utf8str = text;
|
||||||
nextfont = NULL;
|
nextfont = NULL;
|
||||||
while (*text) {
|
while (*text) {
|
||||||
utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
|
utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
|
||||||
for (curfont = drw->fonts; curfont; curfont = curfont->next) {
|
for (curfont = drw->fonts; curfont; curfont = curfont->next) {
|
||||||
charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
|
charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
|
||||||
if (charexists) {
|
if (charexists) {
|
||||||
drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
|
if (curfont == usedfont) {
|
||||||
if (ew + ellipsis_width <= w) {
|
utf8strlen += utf8charlen;
|
||||||
/* keep track where the ellipsis still fits */
|
|
||||||
ellipsis_x = x + ew;
|
|
||||||
ellipsis_w = w - ew;
|
|
||||||
ellipsis_len = utf8strlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ew + tmpw > w) {
|
|
||||||
overflow = 1;
|
|
||||||
/* called from drw_fontset_getwidth_clamp():
|
|
||||||
* it wants the width AFTER the overflow
|
|
||||||
*/
|
|
||||||
if (!render)
|
|
||||||
x += tmpw;
|
|
||||||
else
|
|
||||||
utf8strlen = ellipsis_len;
|
|
||||||
} else if (curfont == usedfont) {
|
|
||||||
text += utf8charlen;
|
text += utf8charlen;
|
||||||
utf8strlen += utf8err ? 0 : utf8charlen;
|
|
||||||
ew += utf8err ? 0 : tmpw;
|
|
||||||
} else {
|
} else {
|
||||||
nextfont = curfont;
|
nextfont = curfont;
|
||||||
}
|
}
|
||||||
@ -299,31 +301,36 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overflow || !charexists || nextfont || utf8err)
|
if (!charexists || nextfont)
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
charexists = 0;
|
charexists = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (utf8strlen) {
|
if (utf8strlen) {
|
||||||
|
drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
|
||||||
|
/* shorten text if necessary */
|
||||||
|
for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
|
||||||
|
drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
|
||||||
|
|
||||||
|
if (len) {
|
||||||
|
memcpy(buf, utf8str, len);
|
||||||
|
buf[len] = '\0';
|
||||||
|
if (len < utf8strlen)
|
||||||
|
for (i = len; i && i > len - 3; buf[--i] = '.')
|
||||||
|
; /* NOP */
|
||||||
|
|
||||||
if (render) {
|
if (render) {
|
||||||
ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
|
ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
|
||||||
XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
|
XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
|
||||||
usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
|
usedfont->xfont, x, ty, (XftChar8 *)buf, len);
|
||||||
}
|
}
|
||||||
x += ew;
|
x += ew;
|
||||||
w -= ew;
|
w -= ew;
|
||||||
}
|
}
|
||||||
if (utf8err && (!render || invalid_width < w)) {
|
|
||||||
if (render)
|
|
||||||
drw_text(drw, x, y, w, h, 0, invalid, invert);
|
|
||||||
x += invalid_width;
|
|
||||||
w -= invalid_width;
|
|
||||||
}
|
}
|
||||||
if (render && overflow)
|
|
||||||
drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
|
|
||||||
|
|
||||||
if (!*text || overflow) {
|
if (!*text) {
|
||||||
break;
|
break;
|
||||||
} else if (nextfont) {
|
} else if (nextfont) {
|
||||||
charexists = 0;
|
charexists = 0;
|
||||||
@ -333,15 +340,6 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||||||
* character must be drawn. */
|
* character must be drawn. */
|
||||||
charexists = 1;
|
charexists = 1;
|
||||||
|
|
||||||
hash = (unsigned int)utf8codepoint;
|
|
||||||
hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
|
|
||||||
hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
|
|
||||||
h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
|
|
||||||
h1 = (hash >> 17) % LENGTH(nomatches);
|
|
||||||
/* avoid expensive XftFontMatch call when we know we won't find a match */
|
|
||||||
if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
|
|
||||||
goto no_match;
|
|
||||||
|
|
||||||
fccharset = FcCharSetCreate();
|
fccharset = FcCharSetCreate();
|
||||||
FcCharSetAddChar(fccharset, utf8codepoint);
|
FcCharSetAddChar(fccharset, utf8codepoint);
|
||||||
|
|
||||||
@ -353,6 +351,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||||||
fcpattern = FcPatternDuplicate(drw->fonts->pattern);
|
fcpattern = FcPatternDuplicate(drw->fonts->pattern);
|
||||||
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
|
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
|
||||||
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
|
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
|
||||||
|
FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
|
||||||
|
|
||||||
FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
|
FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
|
||||||
FcDefaultSubstitute(fcpattern);
|
FcDefaultSubstitute(fcpattern);
|
||||||
@ -369,8 +368,6 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
|||||||
curfont->next = usedfont;
|
curfont->next = usedfont;
|
||||||
} else {
|
} else {
|
||||||
xfont_free(usedfont);
|
xfont_free(usedfont);
|
||||||
nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
|
|
||||||
no_match:
|
|
||||||
usedfont = drw->fonts;
|
usedfont = drw->fonts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -400,15 +397,6 @@ drw_fontset_getwidth(Drw *drw, const char *text)
|
|||||||
return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
|
return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
|
||||||
drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
|
|
||||||
{
|
|
||||||
unsigned int tmp = 0;
|
|
||||||
if (drw && drw->fonts && text && n)
|
|
||||||
tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
|
|
||||||
return MIN(n, tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
1
drw.h
1
drw.h
@ -35,7 +35,6 @@ void drw_free(Drw *drw);
|
|||||||
Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount);
|
Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount);
|
||||||
void drw_fontset_free(Fnt* set);
|
void drw_fontset_free(Fnt* set);
|
||||||
unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
|
unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
|
||||||
unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n);
|
|
||||||
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 */
|
||||||
|
38
util.c
38
util.c
@ -1,5 +1,4 @@
|
|||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <errno.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -7,25 +6,6 @@
|
|||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
void
|
|
||||||
die(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
int saved_errno;
|
|
||||||
|
|
||||||
saved_errno = errno;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
vfprintf(stderr, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (fmt[0] && fmt[strlen(fmt)-1] == ':')
|
|
||||||
fprintf(stderr, " %s", strerror(saved_errno));
|
|
||||||
fputc('\n', stderr);
|
|
||||||
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
ecalloc(size_t nmemb, size_t size)
|
ecalloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
@ -35,3 +15,21 @@ ecalloc(size_t nmemb, size_t size)
|
|||||||
die("calloc:");
|
die("calloc:");
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
fputc('\n', stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
1
util.h
1
util.h
@ -3,7 +3,6 @@
|
|||||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||||
#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))
|
||||||
#define LENGTH(X) (sizeof (X) / sizeof (X)[0])
|
|
||||||
|
|
||||||
void die(const char *fmt, ...);
|
void die(const char *fmt, ...);
|
||||||
void *ecalloc(size_t nmemb, size_t size);
|
void *ecalloc(size_t nmemb, size_t size);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user