Compare commits
39 Commits
Author | SHA1 | Date |
---|---|---|
|
8df553e004 | |
|
7ab0cb5ef0 | |
|
0fe460dbd4 | |
|
dfbbf7f6e1 | |
|
ba1a347dca | |
|
bcbc1ef5c4 | |
|
689d9bfcf6 | |
|
e42c036634 | |
|
1d2b462acf | |
|
7ec32fe494 | |
|
fce06f437d | |
|
1e8c5b68f4 | |
|
528d39b011 | |
|
32db2b1251 | |
|
e35976f4a5 | |
|
28fb3e2812 | |
|
fe5d5c6709 | |
|
e1e1de7b3b | |
|
33685b06e9 | |
|
e4827b0c40 | |
|
e73651f12a | |
|
31fa07b984 | |
|
6818e07291 | |
|
b43ec0577f | |
|
22511c41d5 | |
|
77526f756e | |
|
7269c5355d | |
|
6be057f060 | |
|
41fdabbf7c | |
|
3a505cebe8 | |
|
308fe78b83 | |
|
c4b656e0da | |
|
3e39c526d2 | |
|
a9a3836861 | |
|
eb96af27f4 | |
|
d78ff08d99 | |
|
cd2133a5f6 | |
|
c585e8e498 | |
|
523aa08f51 |
2
LICENSE
2
LICENSE
|
@ -8,7 +8,7 @@ MIT/X Consortium License
|
||||||
© 2009 Markus Schnalke <meillo@marmaro.de>
|
© 2009 Markus Schnalke <meillo@marmaro.de>
|
||||||
© 2009 Evan Gates <evan.gates@gmail.com>
|
© 2009 Evan Gates <evan.gates@gmail.com>
|
||||||
© 2010-2012 Connor Lane Smith <cls@lubutu.com>
|
© 2010-2012 Connor Lane Smith <cls@lubutu.com>
|
||||||
© 2014-2020 Hiltjo Posthuma <hiltjo@codemadness.org>
|
© 2014-2022 Hiltjo Posthuma <hiltjo@codemadness.org>
|
||||||
© 2015-2019 Quentin Rameau <quinq@fifth.space>
|
© 2015-2019 Quentin Rameau <quinq@fifth.space>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
|
10
Makefile
10
Makefile
|
@ -6,13 +6,7 @@ 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: options dmenu stest
|
all: 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) $<
|
||||||
|
@ -61,4 +55,4 @@ uninstall:
|
||||||
$(DESTDIR)$(MANPREFIX)/man1/dmenu.1\
|
$(DESTDIR)$(MANPREFIX)/man1/dmenu.1\
|
||||||
$(DESTDIR)$(MANPREFIX)/man1/stest.1
|
$(DESTDIR)$(MANPREFIX)/man1/stest.1
|
||||||
|
|
||||||
.PHONY: all options clean dist install uninstall
|
.PHONY: all clean dist install uninstall
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# dmenu version
|
# dmenu version
|
||||||
VERSION = 5.0
|
VERSION = 5.2
|
||||||
|
|
||||||
# paths
|
# paths
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
|
@ -17,6 +17,7 @@ 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)
|
||||||
|
|
107
dmenu.c
107
dmenu.c
|
@ -22,7 +22,6 @@
|
||||||
/* 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 */
|
||||||
|
@ -58,6 +57,13 @@ 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)
|
||||||
{
|
{
|
||||||
|
@ -82,10 +88,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 : MIN(TEXTW(next->text), n)) > n)
|
if ((i += (lines > 0) ? bh : textw_clamp(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 : MIN(TEXTW(prev->left->text), n)) > n)
|
if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,19 +103,29 @@ cleanup(void)
|
||||||
XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
cistrstr(const char *s, const char *sub)
|
cistrstr(const char *h, const char *n)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t i;
|
||||||
|
|
||||||
for (len = strlen(sub); *s; s++)
|
if (!n[0])
|
||||||
if (!strncasecmp(s, sub, len))
|
return (char *)h;
|
||||||
return (char *)s;
|
|
||||||
|
for (; *h; ++h) {
|
||||||
|
for (i = 0; n[i] && tolower((unsigned char)n[i]) ==
|
||||||
|
tolower((unsigned char)h[i]); ++i)
|
||||||
|
;
|
||||||
|
if (n[i] == '\0')
|
||||||
|
return (char *)h;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +181,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, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
|
x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">")));
|
||||||
if (next) {
|
if (next) {
|
||||||
w = TEXTW(">");
|
w = TEXTW(">");
|
||||||
drw_setscheme(drw, scheme[SchemeNorm]);
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
||||||
|
@ -225,7 +241,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 %u bytes:", tokn * sizeof *tokv);
|
die("cannot realloc %zu 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;
|
||||||
|
@ -307,19 +323,19 @@ movewordedge(int dir)
|
||||||
static void
|
static void
|
||||||
keypress(XKeyEvent *ev)
|
keypress(XKeyEvent *ev)
|
||||||
{
|
{
|
||||||
char buf[32];
|
char buf[64];
|
||||||
int len;
|
int len;
|
||||||
KeySym ksym;
|
KeySym ksym = NoSymbol;
|
||||||
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:
|
case XLookupChars: /* composed string from input method */
|
||||||
goto insert;
|
goto insert;
|
||||||
case XLookupKeySym:
|
case XLookupKeySym:
|
||||||
case XLookupBoth:
|
case XLookupBoth: /* a KeySym and a string are returned: use keysym */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,9 +376,11 @@ keypress(XKeyEvent *ev)
|
||||||
utf8, utf8, win, CurrentTime);
|
utf8, utf8, win, CurrentTime);
|
||||||
return;
|
return;
|
||||||
case XK_Left:
|
case XK_Left:
|
||||||
|
case XK_KP_Left:
|
||||||
movewordedge(-1);
|
movewordedge(-1);
|
||||||
goto draw;
|
goto draw;
|
||||||
case XK_Right:
|
case XK_Right:
|
||||||
|
case XK_KP_Right:
|
||||||
movewordedge(+1);
|
movewordedge(+1);
|
||||||
goto draw;
|
goto draw;
|
||||||
case XK_Return:
|
case XK_Return:
|
||||||
|
@ -396,10 +414,11 @@ keypress(XKeyEvent *ev)
|
||||||
switch(ksym) {
|
switch(ksym) {
|
||||||
default:
|
default:
|
||||||
insert:
|
insert:
|
||||||
if (!iscntrl(*buf))
|
if (!iscntrl((unsigned char)*buf))
|
||||||
insert(buf, len);
|
insert(buf, len);
|
||||||
break;
|
break;
|
||||||
case XK_Delete:
|
case XK_Delete:
|
||||||
|
case XK_KP_Delete:
|
||||||
if (text[cursor] == '\0')
|
if (text[cursor] == '\0')
|
||||||
return;
|
return;
|
||||||
cursor = nextrune(+1);
|
cursor = nextrune(+1);
|
||||||
|
@ -410,6 +429,7 @@ insert:
|
||||||
insert(NULL, nextrune(-1) - cursor);
|
insert(NULL, nextrune(-1) - cursor);
|
||||||
break;
|
break;
|
||||||
case XK_End:
|
case XK_End:
|
||||||
|
case XK_KP_End:
|
||||||
if (text[cursor] != '\0') {
|
if (text[cursor] != '\0') {
|
||||||
cursor = strlen(text);
|
cursor = strlen(text);
|
||||||
break;
|
break;
|
||||||
|
@ -429,6 +449,7 @@ insert:
|
||||||
cleanup();
|
cleanup();
|
||||||
exit(1);
|
exit(1);
|
||||||
case XK_Home:
|
case XK_Home:
|
||||||
|
case XK_KP_Home:
|
||||||
if (sel == matches) {
|
if (sel == matches) {
|
||||||
cursor = 0;
|
cursor = 0;
|
||||||
break;
|
break;
|
||||||
|
@ -437,6 +458,7 @@ insert:
|
||||||
calcoffsets();
|
calcoffsets();
|
||||||
break;
|
break;
|
||||||
case XK_Left:
|
case XK_Left:
|
||||||
|
case XK_KP_Left:
|
||||||
if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
|
if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
|
||||||
cursor = nextrune(-1);
|
cursor = nextrune(-1);
|
||||||
break;
|
break;
|
||||||
|
@ -445,18 +467,21 @@ insert:
|
||||||
return;
|
return;
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case XK_Up:
|
case XK_Up:
|
||||||
|
case XK_KP_Up:
|
||||||
if (sel && sel->left && (sel = sel->left)->right == curr) {
|
if (sel && sel->left && (sel = sel->left)->right == curr) {
|
||||||
curr = prev;
|
curr = prev;
|
||||||
calcoffsets();
|
calcoffsets();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case XK_Next:
|
case XK_Next:
|
||||||
|
case XK_KP_Next:
|
||||||
if (!next)
|
if (!next)
|
||||||
return;
|
return;
|
||||||
sel = curr = next;
|
sel = curr = next;
|
||||||
calcoffsets();
|
calcoffsets();
|
||||||
break;
|
break;
|
||||||
case XK_Prior:
|
case XK_Prior:
|
||||||
|
case XK_KP_Prior:
|
||||||
if (!prev)
|
if (!prev)
|
||||||
return;
|
return;
|
||||||
sel = curr = prev;
|
sel = curr = prev;
|
||||||
|
@ -473,6 +498,7 @@ insert:
|
||||||
sel->out = 1;
|
sel->out = 1;
|
||||||
break;
|
break;
|
||||||
case XK_Right:
|
case XK_Right:
|
||||||
|
case XK_KP_Right:
|
||||||
if (text[cursor] != '\0') {
|
if (text[cursor] != '\0') {
|
||||||
cursor = nextrune(+1);
|
cursor = nextrune(+1);
|
||||||
break;
|
break;
|
||||||
|
@ -481,6 +507,7 @@ insert:
|
||||||
return;
|
return;
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case XK_Down:
|
case XK_Down:
|
||||||
|
case XK_KP_Down:
|
||||||
if (sel && sel->right && (sel = sel->right) == next) {
|
if (sel && sel->right && (sel = sel->right) == next) {
|
||||||
curr = next;
|
curr = next;
|
||||||
calcoffsets();
|
calcoffsets();
|
||||||
|
@ -489,9 +516,9 @@ insert:
|
||||||
case XK_Tab:
|
case XK_Tab:
|
||||||
if (!sel)
|
if (!sel)
|
||||||
return;
|
return;
|
||||||
strncpy(text, sel->text, sizeof text - 1);
|
cursor = strnlen(sel->text, sizeof text - 1);
|
||||||
text[sizeof text - 1] = '\0';
|
memcpy(text, sel->text, cursor);
|
||||||
cursor = strlen(text);
|
text[cursor] = '\0';
|
||||||
match();
|
match();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -521,29 +548,27 @@ paste(void)
|
||||||
static void
|
static void
|
||||||
readstdin(void)
|
readstdin(void)
|
||||||
{
|
{
|
||||||
char buf[sizeof text], *p;
|
char *line = NULL;
|
||||||
size_t i, imax = 0, size = 0;
|
size_t i, itemsiz = 0, linesiz = 0;
|
||||||
unsigned int tmpmax = 0;
|
ssize_t len;
|
||||||
|
|
||||||
/* 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; fgets(buf, sizeof buf, stdin); i++) {
|
for (i = 0; (len = getline(&line, &linesiz, stdin)) != -1; i++) {
|
||||||
if (i + 1 >= size / sizeof *items)
|
if (i + 1 >= itemsiz) {
|
||||||
if (!(items = realloc(items, (size += BUFSIZ))))
|
itemsiz += 256;
|
||||||
die("cannot realloc %u bytes:", size);
|
if (!(items = realloc(items, itemsiz * sizeof(*items))))
|
||||||
if ((p = strchr(buf, '\n')))
|
die("cannot realloc %zu bytes:", itemsiz * sizeof(*items));
|
||||||
*p = '\0';
|
|
||||||
if (!(items[i].text = strdup(buf)))
|
|
||||||
die("cannot strdup %u bytes:", strlen(buf) + 1);
|
|
||||||
items[i].out = 0;
|
|
||||||
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
|
|
||||||
if (tmpmax > inputw) {
|
|
||||||
inputw = tmpmax;
|
|
||||||
imax = i;
|
|
||||||
}
|
}
|
||||||
|
if (line[len - 1] == '\n')
|
||||||
|
line[len - 1] = '\0';
|
||||||
|
if (!(items[i].text = strdup(line)))
|
||||||
|
die("strdup:");
|
||||||
|
|
||||||
|
items[i].out = 0;
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -634,7 +659,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]))
|
if (INTERSECT(x, y, 1, 1, info[i]) != 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
x = info[i].x_org;
|
x = info[i].x_org;
|
||||||
|
@ -652,14 +677,14 @@ 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 = MIN(inputw, mw/3);
|
inputw = mw / 3; /* input width: ~33% of monitor width */
|
||||||
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, parentwin, x, y, mw, mh, 0,
|
win = XCreateWindow(dpy, root, 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);
|
||||||
|
@ -674,6 +699,7 @@ 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)
|
||||||
|
@ -689,9 +715,8 @@ setup(void)
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
|
die("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
|
||||||
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
|
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
103
drw.c
103
drw.c
|
@ -133,19 +133,6 @@ 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;
|
||||||
|
@ -251,12 +238,10 @@ 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)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
int ty, ellipsis_x = 0;
|
||||||
int ty;
|
unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
|
||||||
unsigned int ew;
|
|
||||||
XftDraw *d = NULL;
|
XftDraw *d = NULL;
|
||||||
Fnt *usedfont, *curfont, *nextfont;
|
Fnt *usedfont, *curfont, *nextfont;
|
||||||
size_t i, len;
|
|
||||||
int utf8strlen, utf8charlen, render = x || y || w || h;
|
int utf8strlen, utf8charlen, render = x || y || w || h;
|
||||||
long utf8codepoint = 0;
|
long utf8codepoint = 0;
|
||||||
const char *utf8str;
|
const char *utf8str;
|
||||||
|
@ -264,13 +249,15 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
||||||
FcPattern *fcpattern;
|
FcPattern *fcpattern;
|
||||||
FcPattern *match;
|
FcPattern *match;
|
||||||
XftResult result;
|
XftResult result;
|
||||||
int charexists = 0;
|
int charexists = 0, overflow = 0;
|
||||||
|
/* keep track of a couple codepoints for which we have no match. */
|
||||||
|
static unsigned int nomatches[128], ellipsis_width;
|
||||||
|
|
||||||
if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
|
if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!render) {
|
if (!render) {
|
||||||
w = ~w;
|
w = invert ? invert : ~invert;
|
||||||
} 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);
|
||||||
|
@ -282,8 +269,10 @@ 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, "...");
|
||||||
while (1) {
|
while (1) {
|
||||||
utf8strlen = 0;
|
ew = ellipsis_len = utf8strlen = 0;
|
||||||
utf8str = text;
|
utf8str = text;
|
||||||
nextfont = NULL;
|
nextfont = NULL;
|
||||||
while (*text) {
|
while (*text) {
|
||||||
|
@ -291,9 +280,27 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
||||||
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) {
|
||||||
if (curfont == usedfont) {
|
drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
|
||||||
|
if (ew + ellipsis_width <= w) {
|
||||||
|
/* 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) {
|
||||||
utf8strlen += utf8charlen;
|
utf8strlen += utf8charlen;
|
||||||
text += utf8charlen;
|
text += utf8charlen;
|
||||||
|
ew += tmpw;
|
||||||
} else {
|
} else {
|
||||||
nextfont = curfont;
|
nextfont = curfont;
|
||||||
}
|
}
|
||||||
|
@ -301,36 +308,25 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!charexists || nextfont)
|
if (overflow || !charexists || nextfont)
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
charexists = 0;
|
charexists = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (utf8strlen) {
|
if (utf8strlen) {
|
||||||
drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
|
if (render) {
|
||||||
/* shorten text if necessary */
|
ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
|
||||||
for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
|
XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
|
||||||
drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
|
usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
|
||||||
|
|
||||||
if (len) {
|
|
||||||
memcpy(buf, utf8str, len);
|
|
||||||
buf[len] = '\0';
|
|
||||||
if (len < utf8strlen)
|
|
||||||
for (i = len; i && i > len - 3; buf[--i] = '.')
|
|
||||||
; /* NOP */
|
|
||||||
|
|
||||||
if (render) {
|
|
||||||
ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
|
|
||||||
XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
|
|
||||||
usedfont->xfont, x, ty, (XftChar8 *)buf, len);
|
|
||||||
}
|
|
||||||
x += ew;
|
|
||||||
w -= ew;
|
|
||||||
}
|
}
|
||||||
|
x += ew;
|
||||||
|
w -= ew;
|
||||||
}
|
}
|
||||||
|
if (render && overflow)
|
||||||
|
drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
|
||||||
|
|
||||||
if (!*text) {
|
if (!*text || overflow) {
|
||||||
break;
|
break;
|
||||||
} else if (nextfont) {
|
} else if (nextfont) {
|
||||||
charexists = 0;
|
charexists = 0;
|
||||||
|
@ -340,6 +336,15 @@ 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);
|
||||||
|
|
||||||
|
@ -351,7 +356,6 @@ 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);
|
||||||
|
@ -368,6 +372,8 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -397,6 +403,15 @@ 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,6 +35,7 @@ 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 */
|
||||||
|
|
2
stest.c
2
stest.c
|
@ -84,7 +84,7 @@ main(int argc, char *argv[])
|
||||||
if (!argc) {
|
if (!argc) {
|
||||||
/* read list from stdin */
|
/* read list from stdin */
|
||||||
while ((n = getline(&line, &linesiz, stdin)) > 0) {
|
while ((n = getline(&line, &linesiz, stdin)) > 0) {
|
||||||
if (n && line[n - 1] == '\n')
|
if (line[n - 1] == '\n')
|
||||||
line[n - 1] = '\0';
|
line[n - 1] = '\0';
|
||||||
test(line, line);
|
test(line, line);
|
||||||
}
|
}
|
||||||
|
|
23
util.c
23
util.c
|
@ -6,18 +6,9 @@
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
void *
|
|
||||||
ecalloc(size_t nmemb, size_t size)
|
|
||||||
{
|
|
||||||
void *p;
|
|
||||||
|
|
||||||
if (!(p = calloc(nmemb, size)))
|
|
||||||
die("calloc:");
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
die(const char *fmt, ...) {
|
die(const char *fmt, ...)
|
||||||
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
|
@ -33,3 +24,13 @@ die(const char *fmt, ...) {
|
||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
ecalloc(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
void *p;
|
||||||
|
|
||||||
|
if (!(p = calloc(nmemb, size)))
|
||||||
|
die("calloc:");
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
1
util.h
1
util.h
|
@ -3,6 +3,7 @@
|
||||||
#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…
Reference in New Issue