From ec9e6099839e372f6ff5303e84db325e641e0633 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 21 Nov 2020 01:51:15 -0500 Subject: Keypress feedback patch and use tiny c compiler --- config.def.h | 14 +++++ config.mk | 2 +- patches/slock-key-feedback.diff | 113 ++++++++++++++++++++++++++++++++++++++++ slock.c | 41 +++++++++++++++ 4 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 patches/slock-key-feedback.diff diff --git a/config.def.h b/config.def.h index d6f0cc3..639a980 100644 --- a/config.def.h +++ b/config.def.h @@ -6,11 +6,25 @@ static const char *colorname[NUMCOLS] = { [INIT] = "black", /* after initialization */ [INPUT] = "#66aabb", /* during input */ [FAILED] = "#cc3333", /* wrong password */ + [BLOCKS] = "#f2f1f0", /* 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 ### + /* time in seconds to cancel lock with mouse movement */ static const int timetocancel = 4; diff --git a/config.mk b/config.mk index 74429ae..aadfc5f 100644 --- a/config.mk +++ b/config.mk @@ -29,4 +29,4 @@ COMPATSRC = explicit_bzero.c #COMPATSRC = # compiler and linker -CC = cc +CC = tcc diff --git a/patches/slock-key-feedback.diff b/patches/slock-key-feedback.diff new file mode 100644 index 0000000..b479518 --- /dev/null +++ b/patches/slock-key-feedback.diff @@ -0,0 +1,113 @@ +diff --git a/config.def.h b/config.def.h +index 9855e21..29734e2 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -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..cf52710 100644 +--- a/slock.c ++++ b/slock.c +@@ -13,6 +13,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -28,7 +29,8 @@ enum { + INIT, + INPUT, + FAILED, +- NUMCOLS ++ BLOCKS, ++ NUMCOLS, + }; + + struct lock { +@@ -83,7 +85,40 @@ 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, ++ x = blocks_x, ++ y = blocks_y; ++ ++ if (height == 0 || 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 = width ? width : screen_width; ++ height = height ? height : screen_height; ++ } ++ ++ unsigned int block_width = width / blocks_count; ++ unsigned int position = rand() % blocks_count; ++ ++ XClearWindow(dpy, win); ++ XFillRectangle(dpy, win, gc, x + position*block_width, y, block_width, height); ++ ++ XFreeGC(dpy, gc); ++} ++ ++ static const char * + gethash(void) + { + const char *hash; +@@ -185,6 +220,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); +@@ -355,6 +393,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); + diff --git a/slock.c b/slock.c index c5fcc05..e2b67e2 100644 --- a/slock.c +++ b/slock.c @@ -32,6 +32,7 @@ enum { INIT, INPUT, FAILED, + BLOCKS, NUMCOLS }; @@ -87,6 +88,39 @@ dontkillme(void) } #endif +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, + x = blocks_x, + y = blocks_y; + + if (height == 0 || 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 = width ? width : screen_width; + height = height ? height : screen_height; + } + + unsigned int block_width = width / blocks_count; + unsigned int position = rand() % blocks_count; + + XClearWindow(dpy, win); + XFillRectangle(dpy, win, gc, x + position*block_width, y, block_width, height); + + XFreeGC(dpy, gc); +} + static const char * gethash(void) { @@ -190,6 +224,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); @@ -362,6 +399,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); -- cgit v1.2.3