From 8116f551e3d98727e81199a908260021dc1d096e Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 15 Oct 2020 17:06:19 -0400 Subject: Working centered dmenu --- .gitignore | 7 +++++-- README.md | 3 ++- config.def.h | 35 +++++++++++++++++++++++++++++++++++ config.h | 33 --------------------------------- dmenu.1 | 3 +++ dmenu.c | 39 ++++++++++++++++++++++++++++++++------- 6 files changed, 77 insertions(+), 43 deletions(-) create mode 100644 config.def.h delete mode 100644 config.h diff --git a/.gitignore b/.gitignore index a211f7e..c6c700f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,11 @@ -# C files +# c files *.o + +# dmenu files dmenu stest +config.h -# Patch files +# patch files *.orig *.rej diff --git a/README.md b/README.md index c8fb594..1cac6f5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Luke's dmenu +# My dmenu Extra stuff added to vanilla dmenu: @@ -7,6 +7,7 @@ Extra stuff added to vanilla dmenu: - can view color characters like emoji (libxft-bgra is required for this reason) - `-P` for password mode: hide user input - `-r` to reject non-matching input +- `-c` to reject non-matching input - dmenu options are mouse clickable ## Installation diff --git a/config.def.h b/config.def.h new file mode 100644 index 0000000..3098fbe --- /dev/null +++ b/config.def.h @@ -0,0 +1,35 @@ +/* See LICENSE file for copyright and license details. */ +/* Default settings; can be overriden by command line. */ + +static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ +static int centered = 1; /* -c option; centers dmenu on screen */ +static int min_width = 500; /* minimum width when centered */ +/* -fn option overrides fonts[0]; default X11 font or font set */ +static const char *fonts[] = { + "monospace:size=12", + "JoyPixels:pixelsize=11:antialias=true:autohint=true" +}; +static const unsigned int bgalpha = 0xe0; +static const unsigned int fgalpha = OPAQUE; +static const char *prompt = NULL; /* -p option; prompt to the left of input field */ +static const char *colors[SchemeLast][2] = { + /* fg bg */ + [SchemeNorm] = { "#bbbbbb", "#222222" }, + [SchemeSel] = { "#eeeeee", "#005577" }, + [SchemeOut] = { "#000000", "#00ffff" }, +}; +static const unsigned int alphas[SchemeLast][2] = { + /* fgalpha bgalphga */ + [SchemeNorm] = { fgalpha, bgalpha }, + [SchemeSel] = { fgalpha, bgalpha }, + [SchemeOut] = { fgalpha, bgalpha }, +}; + +/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ +static unsigned int lines = 11; + +/* + * Characters not considered part of a word while deleting words + * for example: " /?\"&[]" + */ +static const char worddelimiters[] = " "; diff --git a/config.h b/config.h deleted file mode 100644 index 5fbe225..0000000 --- a/config.h +++ /dev/null @@ -1,33 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -/* Default settings; can be overriden by command line. */ - -static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ -/* -fn option overrides fonts[0]; default X11 font or font set */ -static const char *fonts[] = { - "monospace:size=10", - "JoyPixels:pixelsize=8:antialias=true:autohint=true" -}; -static const unsigned int bgalpha = 0xe0; -static const unsigned int fgalpha = OPAQUE; -static const char *prompt = NULL; /* -p option; prompt to the left of input field */ -static const char *colors[SchemeLast][2] = { - /* fg bg */ - [SchemeNorm] = { "#bbbbbb", "#222222" }, - [SchemeSel] = { "#eeeeee", "#005577" }, - [SchemeOut] = { "#000000", "#00ffff" }, -}; -static const unsigned int alphas[SchemeLast][2] = { - /* fgalpha bgalphga */ - [SchemeNorm] = { fgalpha, bgalpha }, - [SchemeSel] = { fgalpha, bgalpha }, - [SchemeOut] = { fgalpha, bgalpha }, -}; - -/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ -static unsigned int lines = 0; - -/* - * Characters not considered part of a word while deleting words - * for example: " /?\"&[]" - */ -static const char worddelimiters[] = " "; diff --git a/dmenu.1 b/dmenu.1 index 4c87074..80295a4 100644 --- a/dmenu.1 +++ b/dmenu.1 @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL. .B \-b dmenu appears at the bottom of the screen. .TP +.B \-c +dmenu appears non-centered on the screen. +.TP .B \-f dmenu grabs the keyboard before reading stdin if not reading from a tty. This is faster, but will lock up X until stdin reaches end\-of\-file. diff --git a/dmenu.c b/dmenu.c index 7174098..9aa6a9d 100644 --- a/dmenu.c +++ b/dmenu.c @@ -138,6 +138,15 @@ calcoffsets(void) break; } +static int +max_textw(void) +{ + int len = 0; + for (struct item *item = items; item && item->text; item++) + len = MAX(TEXTW(item->text), len); + return len; +} + static void cleanup(void) { @@ -796,6 +805,7 @@ setup(void) bh = drw->fonts->h + 2; lines = MAX(lines, 0); mh = (lines + 1) * bh; + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; #ifdef XINERAMA i = 0; if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { @@ -822,9 +832,16 @@ setup(void) if (INTERSECT(x, y, 1, 1, info[i])) break; - x = info[i].x_org; - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); - mw = info[i].width; + if (centered) { + mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width); + x = info[i].x_org + ((info[i].width - mw) / 2); + y = info[i].y_org + ((info[i].height - mh) / 2); + } else { + x = info[i].x_org; + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); + mw = info[i].width; + } + XFree(info); } else #endif @@ -832,11 +849,17 @@ setup(void) if (!XGetWindowAttributes(dpy, parentwin, &wa)) die("could not get embedding window attributes: 0x%lx", parentwin); - x = 0; - y = topbar ? 0 : wa.height - mh; - mw = wa.width; + + if (centered) { + mw = MIN(MAX(max_textw() + promptw, min_width), wa.width); + x = (wa.width - mw) / 2; + y = (wa.height - mh) / 2; + } else { + x = 0; + y = topbar ? 0 : wa.height - mh; + mw = wa.width; + } } - promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; inputw = MIN(inputw, mw/3); match(); @@ -920,6 +943,8 @@ main(int argc, char *argv[]) topbar = 0; else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ fast = 1; + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ + centered = 0; else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ fstrncmp = strncasecmp; fstrstr = cistrstr; -- cgit v1.2.3