commit 17f3dbe69cd92eef39836ced8ba24e3035aee89f
parent f3edf619a8441c5ebc5ea4b209770bf30788c137
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 28 Dec 2022 22:23:39 +0100
Add numpad shortcuts.
Diffstat:
3 files changed, 86 insertions(+), 9 deletions(-)
diff --git a/config.mk b/config.mk
@@ -16,7 +16,7 @@ INCS = -I. -I/usr/include -I${X11INC}
 LIBS = -L/usr/lib -L${X11LIB} -lc -lX11
 
 # flags
-CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE=200809L
+CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE
 CFLAGS = -g -std=c99 -pedantic -Wall ${INCS} ${CPPFLAGS}
 LDFLAGS = -g ${LIBS}
 #LDFLAGS = -s ${LIBS}
diff --git a/thingmenu.1 b/thingmenu.1
@@ -9,9 +9,10 @@
 .Sh SYNOPSIS
 .Nm
 .Bk -words
-.Op Fl x
-.Op Fl s
+.Op Fl n
 .Op Fl o
+.Op Fl s
+.Op Fl x
 .Op Fl g Ar geometry
 .Op Fl w Ar widthscaling
 .Op Fl e Ar heightscaling
@@ -62,6 +63,9 @@ button is clicked.
 .It Fl o
 Use the horizontal layout.
 .
+.It Fl n
+Prepend the numpad keys for shortcuts to the entries.
+.
 .It Fl g
 Define the X11 geometry string, which is to be used.
 .
diff --git a/thingmenu.c b/thingmenu.c
@@ -106,6 +106,7 @@ static char *name = "thingmenu";
 
 Entry **entries = NULL;
 int nentries = 0;
+int exitentry = -1;
 int oneshot = 1;
 Bool ispressing = 0;
 
@@ -157,6 +158,57 @@ keyrelease(XEvent *e)
 
 	for (i = 0; i < nentries && !entries[i]->highlighted; i++);
 
+	if (key >= XK_0 && key <= XK_9) {
+		i = key - XK_0;
+		key = XK_Return;
+	} else if (key >= XK_KP_0 && key <= XK_KP_9) {
+		i = key - XK_KP_0;
+		key = XK_Return;
+	}
+
+	switch (key) {
+	case XK_KP_Insert:
+		i = 0;
+		key = XK_Return;
+		break;
+	case XK_KP_End:
+		i = 1;
+		key = XK_Return;
+		break;
+	case XK_KP_Down:
+		i = 2;
+		key = XK_Return;
+		break;
+	case XK_KP_Page_Down:
+		i = 3;
+		key = XK_Return;
+		break;
+	case XK_KP_Left:
+		i = 4;
+		key = XK_Return;
+		break;
+	case XK_KP_Begin:
+		i = 5;
+		key = XK_Return;
+		break;
+	case XK_KP_Right:
+		i = 6;
+		key = XK_Return;
+		break;
+	case XK_KP_Home:
+		i = 7;
+		key = XK_Return;
+		break;
+	case XK_KP_Up:
+		i = 8;
+		key = XK_Return;
+		break;
+	case XK_KP_Page_Up:
+		i = 9;
+		key = XK_Return;
+		break;
+	}
+
 	switch (key) {
 	case XK_k:
 		key = XK_Up;
@@ -183,6 +235,10 @@ keyrelease(XEvent *e)
 		entries[i]->highlighted = True;
 		drawentry(entries[i]);
 		break;
+	case XK_period:
+	case XK_KP_Decimal:
+	case XK_KP_Delete:
+		i = exitentry;
 	case XK_Return:
 	case XK_space:
 		if (i < nentries) {
@@ -614,7 +670,7 @@ updateentries(void)
 void
 usage(void)
 {
-	fprintf(stderr, "usage: %s [-hxso] [-g geometry] [-w widthscaling] "
+	fprintf(stderr, "usage: %s [-hnosx] [-g geometry] [-w widthscaling] "
 			"[-e heightscaling] [--] "
 			"label0 cmd0 [label1 cmd1 ...]\n", argv0);
 	exit(1);
@@ -623,7 +679,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-	Bool addexit;
+	Bool addexit, usenumpad;
 	char *label, *cmd;
 	int i, xr, yr, bitm;
 	unsigned int wr, hr;
@@ -631,6 +687,7 @@ main(int argc, char *argv[])
 	argv0 = argv[0];
 
 	addexit = True;
+	usenumpad = False;
 
 	if (argc < 2)
 		usage();
@@ -654,6 +711,9 @@ main(int argc, char *argv[])
 	case 'e':
 		heightscaling = atof(EARGF(usage()));
 		break;
+	case 'n':
+		usenumpad = True;
+		break;
 	case 'o':
 		horizontal = True;
 		break;
@@ -681,8 +741,15 @@ main(int argc, char *argv[])
 			die("realloc returned NULL");
 		if (!(entries[nentries-1] = calloc(1, sizeof(*entries[0]))))
 			die("calloc returned NULL");
-		if (!(entries[nentries-1]->label = strdup(label)))
-			die("strdup returned NULL\n");
+		if (usenumpad == True && nentries < 11) {
+			if (!(asprintf(&entries[nentries-1]->label,
+					"%d:%s", nentries-1, strdup(label)))) {
+				die("asprintf returned NULL\n");
+			}
+		} else {
+			if (!(entries[nentries-1]->label = strdup(label)))
+				die("strdup returned NULL\n");
+		}
 		if (!(entries[nentries-1]->cmd = strdup(cmd)))
 			die("strdup returned NULL\n");
 		entries[nentries-1]->forceexit = False;
@@ -695,11 +762,17 @@ main(int argc, char *argv[])
 			die("realloc returned NULL");
 		if (!(entries[nentries-1] = calloc(1, sizeof(*entries[0]))))
 			die("calloc returned NULL");
-		if (!(entries[nentries-1]->label = strdup("cancel")))
-			die("strdup returned NULL\n");
+		if (usenumpad == True) {
+			if (!(entries[nentries-1]->label = strdup(".:cancel")))
+				die("strdup returned NULL\n");
+		} else {
+			if (!(entries[nentries-1]->label = strdup("cancel")))
+				die("strdup returned NULL\n");
+		}
 		if (!(entries[nentries-1]->cmd = strdup("exit")))
 			die("strdup returned NULL\n");
 		entries[nentries-1]->forceexit = True;
+		exitentry = nentries - 1;
 	}
 
 	if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())