[hackers] [slock][PATCH] Adds keypress feedback patch in slock

From: Akshay Nair <phenax5_AT_gmail.com>
Date: Mon, 3 Aug 2020 15:11:01 +0530

---
 config.def.h | 16 ++++++++++++++++
 slock.c      | 43 +++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/config.def.h b/config.def.h
index 9855e21..29734e2 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -6,7 +6,23 @@ static const char *colorname[NUMCOLS] = {
 	[INIT] =   "black",     /* after initialization */
 	[INPUT] =  "#005577",   /* during input */
 	[FAILED] = "#CC3333",   /* wrong password */
+	[BLOCKS] = "#ffffff",   /* key feedback block */
 };
 
 /* treat a cleared input like a wrong password (color) */
 static const int failonclear = 1;
+
+
+// ### Blocks bar ###
+static short int blocks_enabled = 1; // 0 = don't show blocks
+static const int blocks_width = 0; // 0 = full width
+static const int blocks_height = 16;
+
+// position
+static const int blocks_x = 0;
+static const int blocks_y = 0;
+
+// Number of blocks
+static const int blocks_count = 10;
+// ### \Blocks bar ###
+
diff --git a/slock.c b/slock.c
index 5ae738c..bd2e3d1 100644
--- a/slock.c
+++ b/slock.c
_AT_@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <time.h>
 #include <sys/types.h>
 #include <X11/extensions/Xrandr.h>
 #include <X11/keysym.h>
_AT_@ -28,7 +29,8 @@ enum {
 	INIT,
 	INPUT,
 	FAILED,
-	NUMCOLS
+	BLOCKS,
+	NUMCOLS,
 };
 
 struct lock {
_AT_@ -83,7 +85,37 @@ dontkillme(void)
 }
 #endif
 
-static const char *
+static void
+draw_key_feedback(Display *dpy, struct lock **locks, int screen)
+{
+	XGCValues gr_values;
+
+	Window win = locks[screen]->win;
+	Window root_win;
+
+	gr_values.foreground = locks[screen]->colors[BLOCKS];
+	GC gc = XCreateGC(dpy, win, GCForeground, &gr_values);
+
+	int width = blocks_width,
+			height = blocks_height;
+	if (blocks_height == 0 || blocks_width == 0) {
+		int _x, _y;
+		unsigned int screen_width, screen_height, _b, _d;
+		XGetGeometry(dpy, win, &root_win, &_x, &_y, &screen_width, &screen_height, &_b, &_d);
+		width = blocks_width ? blocks_width : screen_width;
+		height = blocks_height ? blocks_height : screen_height;
+	}
+
+	unsigned int block_width = width / blocks_count;
+	unsigned int position = rand() % blocks_count;
+
+	XClearWindow(dpy, win);
+	XFillRectangle(dpy, win, gc, blocks_x + position*block_width, blocks_y, block_width, blocks_height);
+
+	XFreeGC(dpy, gc);
+}
+
+	static const char *
 gethash(void)
 {
 	const char *hash;
_AT_@ -185,6 +217,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
 					memcpy(passwd + len, buf, num);
 					len += num;
 				}
+				if (blocks_enabled)
+					for (screen = 0; screen < nscreens; screen++)
+						draw_key_feedback(dpy, locks, screen);
 				break;
 			}
 			color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
_AT_@ -355,6 +390,10 @@ main(int argc, char **argv) {
 	if (setuid(duid) < 0)
 		die("slock: setuid: %s\n", strerror(errno));
 
+	time_t t;
+	srand((unsigned) time(&t));
+
+
 	/* check for Xrandr support */
 	rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);
 
-- 
2.28.0
Received on Mon Aug 03 2020 - 11:41:01 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 03 2020 - 13:36:32 CEST