summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2020-10-15 17:06:19 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2020-10-15 17:06:19 -0400
commit8116f551e3d98727e81199a908260021dc1d096e (patch)
treeaa19a04d3cf7b585581b84a7672e47a24d7e65c7
parent70aa9e0d8c9dce081e47d69deda26fc225f505dd (diff)
Working centered dmenu
-rw-r--r--.gitignore7
-rw-r--r--README.md3
-rw-r--r--config.def.h (renamed from config.h)8
-rw-r--r--dmenu.13
-rw-r--r--dmenu.c39
5 files changed, 47 insertions, 13 deletions
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.h b/config.def.h
index 5fbe225..3098fbe 100644
--- a/config.h
+++ b/config.def.h
@@ -2,10 +2,12 @@
/* 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=10",
- "JoyPixels:pixelsize=8:antialias=true:autohint=true"
+ "monospace:size=12",
+ "JoyPixels:pixelsize=11:antialias=true:autohint=true"
};
static const unsigned int bgalpha = 0xe0;
static const unsigned int fgalpha = OPAQUE;
@@ -24,7 +26,7 @@ static const unsigned int alphas[SchemeLast][2] = {
};
/* -l option; if nonzero, dmenu uses vertical list with given number of lines */
-static unsigned int lines = 0;
+static unsigned int lines = 11;
/*
* Characters not considered part of a word while deleting words
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;