This commit is contained in:
DarthMooMancer
2025-08-08 19:28:30 -04:00
parent 5d4ec31a60
commit 8036c58808
3 changed files with 75 additions and 184 deletions
+3 -8
View File
@@ -1,18 +1,13 @@
.POSIX: .POSIX:
VERSION = 5.3
PREFIX = /usr/local PREFIX = /usr/local
X11INC = /usr/X11R6/include X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib X11LIB = /usr/X11R6/lib
FREETYPELIBS = -lfontconfig -lXft INCS = -I$(X11INC) -I/usr/include/freetype2
FREETYPEINC = /usr/include/freetype2 LIBS = -L$(X11LIB) -lX11 -lfontconfig -lXft
INCS = -I$(X11INC) -I$(FREETYPEINC) CPPFLAGS = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L
LIBS = -L$(X11LIB) -lX11 $(FREETYPELIBS)
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L
CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS) CFLAGS = -std=c99 -pedantic -Wall -Os $(INCS) $(CPPFLAGS)
LDFLAGS = $(LIBS) LDFLAGS = $(LIBS)
+69 -169
View File
@@ -32,7 +32,7 @@ struct item {
static char text[BUFSIZ] = ""; static char text[BUFSIZ] = "";
static char *embed; static char *embed;
static int bh, mw, mh; static int bh, mw, mh;
static int inputw = 0, promptw; static int inputw = 0;
static int lrpad; /* sum of left and right padding */ static int lrpad; /* sum of left and right padding */
static size_t cursor; static size_t cursor;
static struct item *items = NULL; static struct item *items = NULL;
@@ -48,11 +48,9 @@ static XIC xic;
static Drw *drw; static Drw *drw;
static Clr *scheme[SchemeLast]; static Clr *scheme[SchemeLast];
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
static const char *fonts[] = { static const char *fonts[] = {
"CaskaydiaCove NFM style:SemiBold:size=11" "CaskaydiaCove NFM style:SemiBold:size=11"
}; };
static const char *prompt = NULL; /* -p option; prompt to the left of input field */
static const char *colors[SchemeLast][2] = { static const char *colors[SchemeLast][2] = {
[SchemeNorm] = { "#D3C6AA", "#272E33" }, [SchemeNorm] = { "#D3C6AA", "#272E33" },
[SchemeSel] = { "#272E33", "#DBBC7F" }, [SchemeSel] = { "#272E33", "#DBBC7F" },
@@ -65,94 +63,65 @@ static const char worddelimiters[] = " ";
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 static unsigned int textw_clamp(const char *str, unsigned int n) {
textw_clamp(const char *str, unsigned int n)
{
unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad; unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad;
return MIN(w, n); 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) if (*last) (*last)->right = item;
{ else *list = item;
if (*last)
(*last)->right = item;
else
*list = item;
item->left = *last; item->left = *last;
item->right = NULL; item->right = NULL;
*last = item; *last = item;
} }
static void static void calcoffsets(void) {
calcoffsets(void)
{
int i, n; int i, n;
if (lines > 0) if (lines > 0) n = lines * bh;
n = lines * bh; else n = mw - (inputw + TEXTW("<") + TEXTW(">"));
else
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 : 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 : textw_clamp(prev->left->text, n)) > n) if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n) break;
break;
} }
static void static void cleanup(void) {
cleanup(void)
{
size_t i; size_t i;
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);
for (i = 0; items && items[i].text; ++i)
free(items[i].text);
free(items); 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 *h, const char *n) {
cistrstr(const char *h, const char *n)
{
size_t i; size_t i;
if (!n[0]) if (!n[0]) return (char *)h;
return (char *)h;
for (; *h; ++h) { for (; *h; ++h) {
for (i = 0; n[i] && tolower((unsigned char)n[i]) == for (i = 0; n[i] && tolower((unsigned char)n[i]) ==
tolower((unsigned char)h[i]); ++i) tolower((unsigned char)h[i]); ++i)
; ;
if (n[i] == '\0') if (n[i] == '\0') return (char *)h;
return (char *)h;
} }
return NULL; return NULL;
} }
static int static int drawitem(struct item *item, int x, int y, int w) {
drawitem(struct item *item, int x, int y, int w) if (item == sel) drw_setscheme(drw, scheme[SchemeSel]);
{ else if (item->out) drw_setscheme(drw, scheme[SchemeOut]);
if (item == sel) else drw_setscheme(drw, scheme[SchemeNorm]);
drw_setscheme(drw, scheme[SchemeSel]);
else if (item->out)
drw_setscheme(drw, scheme[SchemeOut]);
else
drw_setscheme(drw, scheme[SchemeNorm]);
return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
} }
static void static void drawmenu(void) {
drawmenu(void)
{
unsigned int curpos; unsigned int curpos;
struct item *item; struct item *item;
int x = 0, y = 0, w; int x = 0, y = 0, w;
@@ -160,10 +129,6 @@ drawmenu(void)
drw_setscheme(drw, scheme[SchemeNorm]); drw_setscheme(drw, scheme[SchemeNorm]);
drw_rect(drw, 0, 0, mw, mh, 1, 1); drw_rect(drw, 0, 0, mw, mh, 1, 1);
if (prompt && *prompt) {
drw_setscheme(drw, scheme[SchemeSel]);
x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0);
}
/* draw input field */ /* draw input field */
w = (lines > 0 || !matches) ? mw - x : inputw; w = (lines > 0 || !matches) ? mw - x : inputw;
drw_setscheme(drw, scheme[SchemeNorm]); drw_setscheme(drw, scheme[SchemeNorm]);
@@ -200,31 +165,25 @@ drawmenu(void)
drw_map(drw, win, 0, 0, mw, mh); drw_map(drw, win, 0, 0, mw, mh);
} }
static void static void grabfocus(void) {
grabfocus(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
Window focuswin; Window focuswin;
int i, revertwin; int i, revertwin;
for (i = 0; i < 100; ++i) { for (i = 0; i < 100; ++i) {
XGetInputFocus(dpy, &focuswin, &revertwin); XGetInputFocus(dpy, &focuswin, &revertwin);
if (focuswin == win) if (focuswin == win) return;
return;
XSetInputFocus(dpy, win, RevertToParent, CurrentTime); XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
} }
die("cannot grab focus"); die("cannot grab focus");
} }
static void static void grabkeyboard(void) {
grabkeyboard(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 }; struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
int i; int i;
if (embed) if (embed) return;
return;
/* try to grab keyboard, we may have to wait for another process to ungrab */ /* try to grab keyboard, we may have to wait for another process to ungrab */
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync, if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
@@ -235,9 +194,7 @@ grabkeyboard(void)
die("cannot grab keyboard"); die("cannot grab keyboard");
} }
static void static void match(void) {
match(void)
{
static char **tokv = NULL; static char **tokv = NULL;
static int tokn = 0; static int tokn = 0;
@@ -262,76 +219,56 @@ match(void)
if (i != tokc) /* not all tokens match */ if (i != tokc) /* not all tokens match */
continue; continue;
/* exact matches go first, then prefixes, then substrings */ /* exact matches go first, then prefixes, then substrings */
if (!tokc || !fstrncmp(text, item->text, textsize)) if (!tokc || !fstrncmp(text, item->text, textsize)) appenditem(item, &matches, &matchend);
appenditem(item, &matches, &matchend); else if (!fstrncmp(tokv[0], item->text, len)) appenditem(item, &lprefix, &prefixend);
else if (!fstrncmp(tokv[0], item->text, len)) else appenditem(item, &lsubstr, &substrend);
appenditem(item, &lprefix, &prefixend);
else
appenditem(item, &lsubstr, &substrend);
} }
if (lprefix) { if (lprefix) {
if (matches) { if (matches) {
matchend->right = lprefix; matchend->right = lprefix;
lprefix->left = matchend; lprefix->left = matchend;
} else } else matches = lprefix;
matches = lprefix;
matchend = prefixend; matchend = prefixend;
} }
if (lsubstr) { if (lsubstr) {
if (matches) { if (matches) {
matchend->right = lsubstr; matchend->right = lsubstr;
lsubstr->left = matchend; lsubstr->left = matchend;
} else } else matches = lsubstr;
matches = lsubstr;
matchend = substrend; matchend = substrend;
} }
curr = sel = matches; curr = sel = matches;
calcoffsets(); calcoffsets();
} }
static void static void insert(const char *str, ssize_t n) {
insert(const char *str, ssize_t n) if (strlen(text) + n > sizeof text - 1) return;
{
if (strlen(text) + n > sizeof text - 1)
return;
/* move existing text out of the way, insert new text, and update cursor */ /* move existing text out of the way, insert new text, and update cursor */
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
if (n > 0) if (n > 0) memcpy(&text[cursor], str, n);
memcpy(&text[cursor], str, n);
cursor += n; cursor += n;
match(); match();
} }
static size_t static size_t nextrune(int inc) {
nextrune(int inc)
{
ssize_t n; ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */ /* return location of next utf8 rune in the given direction (+1 or -1) */
for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc) for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
;
return n; return n;
} }
static void static void movewordedge(int dir) {
movewordedge(int dir)
{
if (dir < 0) { /* move cursor to the start of the word*/ if (dir < 0) { /* move cursor to the start of the word*/
while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)])) cursor = nextrune(-1);
cursor = nextrune(-1); while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)])) cursor = nextrune(-1);
while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
cursor = nextrune(-1);
} else { /* move cursor to the end of the word */ } else { /* move cursor to the end of the word */
while (text[cursor] && strchr(worddelimiters, text[cursor])) while (text[cursor] && strchr(worddelimiters, text[cursor])) cursor = nextrune(+1);
cursor = nextrune(+1); while (text[cursor] && !strchr(worddelimiters, text[cursor])) cursor = nextrune(+1);
while (text[cursor] && !strchr(worddelimiters, text[cursor]))
cursor = nextrune(+1);
} }
} }
static void static void keypress(XKeyEvent *ev) {
keypress(XKeyEvent *ev)
{
char buf[64]; char buf[64];
int len; int len;
KeySym ksym = NoSymbol; KeySym ksym = NoSymbol;
@@ -536,9 +473,7 @@ draw:
drawmenu(); drawmenu();
} }
static void static void paste(void) {
paste(void)
{
char *p, *q; char *p, *q;
int di; int di;
unsigned long dl; unsigned long dl;
@@ -554,9 +489,7 @@ paste(void)
drawmenu(); drawmenu();
} }
static void static void readstdin(void) {
readstdin(void)
{
char *line = NULL; char *line = NULL;
size_t i, itemsiz = 0, linesiz = 0; size_t i, itemsiz = 0, linesiz = 0;
ssize_t len; ssize_t len;
@@ -568,60 +501,46 @@ readstdin(void)
if (!(items = realloc(items, itemsiz * sizeof(*items)))) if (!(items = realloc(items, itemsiz * sizeof(*items))))
die("cannot realloc %zu bytes:", itemsiz * sizeof(*items)); die("cannot realloc %zu bytes:", itemsiz * sizeof(*items));
} }
if (line[len - 1] == '\n') if (line[len - 1] == '\n') line[len - 1] = '\0';
line[len - 1] = '\0'; if (!(items[i].text = strdup(line))) die("strdup:");
if (!(items[i].text = strdup(line)))
die("strdup:");
items[i].out = 0; items[i].out = 0;
} }
free(line); free(line);
if (items) if (items) items[i].text = NULL;
items[i].text = NULL;
lines = MIN(lines, i); lines = MIN(lines, i);
} }
static void static void run(void) {
run(void)
{
XEvent ev; XEvent ev;
while (!XNextEvent(dpy, &ev)) { while (!XNextEvent(dpy, &ev)) {
if (XFilterEvent(&ev, win)) if (XFilterEvent(&ev, win)) continue;
continue;
switch(ev.type) { switch(ev.type) {
case DestroyNotify: case DestroyNotify:
if (ev.xdestroywindow.window != win) if (ev.xdestroywindow.window != win) break;
break;
cleanup(); cleanup();
exit(1); exit(1);
case Expose: case Expose:
if (ev.xexpose.count == 0) if (ev.xexpose.count == 0) drw_map(drw, win, 0, 0, mw, mh);
drw_map(drw, win, 0, 0, mw, mh);
break; break;
case FocusIn: case FocusIn:
/* regrab focus from parent window */ /* regrab focus from parent window */
if (ev.xfocus.window != win) if (ev.xfocus.window != win) grabfocus();
grabfocus();
break; break;
case KeyPress: case KeyPress:
keypress(&ev.xkey); keypress(&ev.xkey);
break; break;
case SelectionNotify: case SelectionNotify:
if (ev.xselection.property == utf8) if (ev.xselection.property == utf8) paste();
paste();
break; break;
case VisibilityNotify: case VisibilityNotify:
if (ev.xvisibility.state != VisibilityUnobscured) if (ev.xvisibility.state != VisibilityUnobscured) XRaiseWindow(dpy, win);
XRaiseWindow(dpy, win);
break; break;
} }
} }
} }
static void static void setup(void) {
setup(void)
{
int x, y, i, j; int x, y, i, j;
unsigned int du; unsigned int du;
XSetWindowAttributes swa; XSetWindowAttributes swa;
@@ -630,8 +549,7 @@ setup(void)
XWindowAttributes wa; XWindowAttributes wa;
XClassHint ch = {"dmenu", "dmenu"}; XClassHint ch = {"dmenu", "dmenu"};
/* init appearance */ /* init appearance */
for (j = 0; j < SchemeLast; j++) for (j = 0; j < SchemeLast; j++) scheme[j] = drw_scm_create(drw, colors[j], 2);
scheme[j] = drw_scm_create(drw, colors[j], 2);
clip = XInternAtom(dpy, "CLIPBOARD", False); clip = XInternAtom(dpy, "CLIPBOARD", False);
utf8 = XInternAtom(dpy, "UTF8_STRING", False); utf8 = XInternAtom(dpy, "UTF8_STRING", False);
@@ -642,13 +560,11 @@ setup(void)
mh = (lines + 1) * bh; mh = (lines + 1) * bh;
{ {
if (!XGetWindowAttributes(dpy, parentwin, &wa)) if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx", die("could not get embedding window attributes: 0x%lx", parentwin);
parentwin);
x = 0; x = 0;
y = topbar ? 0 : wa.height - mh; y = 0;
mw = wa.width; mw = wa.width;
} }
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
inputw = mw / 3; /* input width: ~33% of monitor width */ inputw = mw / 3; /* input width: ~33% of monitor width */
match(); match();
@@ -662,11 +578,9 @@ setup(void)
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");
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, win, XNFocusWindow, win, NULL);
XNClientWindow, win, XNFocusWindow, win, NULL);
XMapRaised(dpy, win); XMapRaised(dpy, win);
if (embed) { if (embed) {
@@ -683,24 +597,18 @@ setup(void)
drawmenu(); drawmenu();
} }
static void static void usage(void) {
usage(void) die("usage: dmenu [-bfiv] [-l lines] [-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]"); " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
} }
int int main(int argc, char *argv[]) {
main(int argc, char *argv[])
{
XWindowAttributes wa; XWindowAttributes wa;
int i, fast = 0; int i, fast = 0;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
/* these options take no arguments */ /* these options take no arguments */
if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */ if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
topbar = 0;
else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
fast = 1; fast = 1;
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp; fstrncmp = strncasecmp;
@@ -712,8 +620,6 @@ main(int argc, char *argv[])
lines = atoi(argv[++i]); lines = atoi(argv[++i]);
else if (!strcmp(argv[i], "-m")) else if (!strcmp(argv[i], "-m"))
mon = atoi(argv[++i]); mon = atoi(argv[++i]);
else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
prompt = argv[++i];
else if (!strcmp(argv[i], "-fn")) /* font or font set */ else if (!strcmp(argv[i], "-fn")) /* font or font set */
fonts[0] = argv[++i]; fonts[0] = argv[++i];
else if (!strcmp(argv[i], "-nb")) /* normal background color */ else if (!strcmp(argv[i], "-nb")) /* normal background color */
@@ -729,20 +635,14 @@ main(int argc, char *argv[])
else else
usage(); usage();
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) fputs("warning: no locale support\n", stderr);
fputs("warning: no locale support\n", stderr); if (!(dpy = XOpenDisplay(NULL))) die("cannot open display");
if (!(dpy = XOpenDisplay(NULL)))
die("cannot open display");
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
if (!embed || !(parentwin = strtol(embed, NULL, 0))) if (!embed || !(parentwin = strtol(embed, NULL, 0))) parentwin = root;
parentwin = root; if (!XGetWindowAttributes(dpy, parentwin, &wa)) die("could not get embedding window attributes: 0x%lx", parentwin);
if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx",
parentwin);
drw = drw_create(dpy, screen, root, wa.width, wa.height); drw = drw_create(dpy, screen, root, wa.width, wa.height);
if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) die("no fonts could be loaded.");
die("no fonts could be loaded.");
lrpad = drw->fonts->h; lrpad = drw->fonts->h;
if (fast && !isatty(0)) { if (fast && !isatty(0)) {
+3 -7
View File
@@ -114,13 +114,9 @@ static Fnt * xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern
return font; return font;
} }
static void static void xfont_free(Fnt *font) {
xfont_free(Fnt *font) if (!font) return;
{ if (font->pattern) FcPatternDestroy(font->pattern);
if (!font)
return;
if (font->pattern)
FcPatternDestroy(font->pattern);
XftFontClose(font->dpy, font->xfont); XftFontClose(font->dpy, font->xfont);
free(font); free(font);
} }