From 14c708234fafdf8f1a8ccddf77d025a154216ff7 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 00:45:35 +0530 Subject: [PATCH 01/10] Adds random block generation for key feedback --- config.def.h | 8 ++++++++ slock.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/config.def.h b/config.def.h index 9855e21..7419fec 100644 --- a/config.def.h +++ b/config.def.h @@ -10,3 +10,11 @@ static const char *colorname[NUMCOLS] = { /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; + +static short int enable_bar = 1; +static const int bar_width = 0; +static const int bar_height = 30; +static const int bar_x = 0; +static const int bar_y = 0; +static const int bar_blocks = 10; + diff --git a/slock.c b/slock.c index 5ae738c..6292b1e 100644 --- a/slock.c +++ b/slock.c @@ -83,6 +83,40 @@ 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[INPUT]; + GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); + + gr_values.foreground = locks[screen]->colors[INIT]; + GC gcblank = XCreateGC(dpy, win, GCForeground, &gr_values); + + int width = bar_width, + height = bar_height; + if (bar_height == 0 || bar_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 = bar_width ? bar_width : screen_width; + height = bar_height ? bar_height : screen_height; + } + + unsigned int blocks = bar_blocks; + unsigned int block_width = width / blocks; + unsigned int position = rand() % blocks; + + XFillRectangle(dpy, win, gcblank, bar_x, bar_y, width, bar_height + 1); + XFillRectangle(dpy, win, gc, bar_x + position*block_width, bar_y, block_width, bar_height); + + XFreeGC(dpy, gc); +} + static const char * gethash(void) { @@ -185,6 +219,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, memcpy(passwd + len, buf, num); len += num; } + for (screen = 0; screen < nscreens; screen++) { + draw_key_feedback(dpy, locks, screen); + } break; } color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT); From c257db9dc9acff00e1df1e4d078d47cd32358f7f Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 00:48:26 +0530 Subject: [PATCH 02/10] Clears window instead of drawing under --- slock.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/slock.c b/slock.c index 6292b1e..bec5662 100644 --- a/slock.c +++ b/slock.c @@ -91,11 +91,8 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) Window win = locks[screen]->win; Window root_win; - gr_values.foreground = locks[screen]->colors[INPUT]; - GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); - gr_values.foreground = locks[screen]->colors[INIT]; - GC gcblank = XCreateGC(dpy, win, GCForeground, &gr_values); + GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); int width = bar_width, height = bar_height; @@ -111,7 +108,7 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) unsigned int block_width = width / blocks; unsigned int position = rand() % blocks; - XFillRectangle(dpy, win, gcblank, bar_x, bar_y, width, bar_height + 1); + XClearWindow(dpy, win); XFillRectangle(dpy, win, gc, bar_x + position*block_width, bar_y, block_width, bar_height); XFreeGC(dpy, gc); From 41a2301df2a1894e6d1ad77e7a30e27a0117d9d2 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 00:52:20 +0530 Subject: [PATCH 03/10] Adds block color customization --- config.def.h | 1 + slock.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config.def.h b/config.def.h index 7419fec..144b6a8 100644 --- a/config.def.h +++ b/config.def.h @@ -6,6 +6,7 @@ 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) */ diff --git a/slock.c b/slock.c index bec5662..1c320bb 100644 --- a/slock.c +++ b/slock.c @@ -28,7 +28,8 @@ enum { INIT, INPUT, FAILED, - NUMCOLS + BLOCKS, + NUMCOLS, }; struct lock { @@ -91,7 +92,7 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) Window win = locks[screen]->win; Window root_win; - gr_values.foreground = locks[screen]->colors[INIT]; + gr_values.foreground = locks[screen]->colors[BLOCKS]; GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); int width = bar_width, From 59c39a6d9982e8d442aef12fc930689697382276 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 01:00:42 +0530 Subject: [PATCH 04/10] Adds seed for random values --- slock.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/slock.c b/slock.c index 1c320bb..0337103 100644 --- a/slock.c +++ b/slock.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -390,6 +391,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); From 47bc2d5d5e684cea2ebdda1053bc45471884fa4d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 01:07:16 +0530 Subject: [PATCH 05/10] Fixes indentation --- slock.c | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/slock.c b/slock.c index 0337103..6805587 100644 --- a/slock.c +++ b/slock.c @@ -90,33 +90,33 @@ 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 = bar_width, - height = bar_height; - if (bar_height == 0 || bar_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 = bar_width ? bar_width : screen_width; - height = bar_height ? bar_height : screen_height; - } - - unsigned int blocks = bar_blocks; - unsigned int block_width = width / blocks; - unsigned int position = rand() % blocks; - - XClearWindow(dpy, win); - XFillRectangle(dpy, win, gc, bar_x + position*block_width, bar_y, block_width, bar_height); - - XFreeGC(dpy, gc); + 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 = bar_width, + height = bar_height; + if (bar_height == 0 || bar_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 = bar_width ? bar_width : screen_width; + height = bar_height ? bar_height : screen_height; + } + + unsigned int blocks = bar_blocks; + unsigned int block_width = width / blocks; + unsigned int position = rand() % blocks; + + XClearWindow(dpy, win); + XFillRectangle(dpy, win, gc, bar_x + position*block_width, bar_y, block_width, bar_height); + + XFreeGC(dpy, gc); } -static const char * + static const char * gethash(void) { const char *hash; @@ -391,8 +391,8 @@ main(int argc, char **argv) { if (setuid(duid) < 0) die("slock: setuid: %s\n", strerror(errno)); - time_t t; - srand((unsigned) time(&t)); + time_t t; + srand((unsigned) time(&t)); /* check for Xrandr support */ From ff3e1d5b9044c1611cda33ae0eddbbcac5cfb33b Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 01:18:34 +0530 Subject: [PATCH 06/10] Renamed bar to blocks --- config.def.h | 19 +++++++++++++------ slock.c | 22 ++++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/config.def.h b/config.def.h index 144b6a8..945f362 100644 --- a/config.def.h +++ b/config.def.h @@ -12,10 +12,17 @@ static const char *colorname[NUMCOLS] = { /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; -static short int enable_bar = 1; -static const int bar_width = 0; -static const int bar_height = 30; -static const int bar_x = 0; -static const int bar_y = 0; -static const int bar_blocks = 10; + +// ### 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; // 0 = full height + +// 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 6805587..b29bc64 100644 --- a/slock.c +++ b/slock.c @@ -96,22 +96,21 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) gr_values.foreground = locks[screen]->colors[BLOCKS]; GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); - int width = bar_width, - height = bar_height; - if (bar_height == 0 || bar_width == 0) { + 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 = bar_width ? bar_width : screen_width; - height = bar_height ? bar_height : screen_height; + width = blocks_width ? blocks_width : screen_width; + height = blocks_height ? blocks_height : screen_height; } - unsigned int blocks = bar_blocks; - unsigned int block_width = width / blocks; - unsigned int position = rand() % blocks; + unsigned int block_width = width / blocks_count; + unsigned int position = rand() % blocks_count; XClearWindow(dpy, win); - XFillRectangle(dpy, win, gc, bar_x + position*block_width, bar_y, block_width, bar_height); + XFillRectangle(dpy, win, gc, blocks_x + position*block_width, blocks_y, block_width, blocks_height); XFreeGC(dpy, gc); } @@ -218,9 +217,8 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, memcpy(passwd + len, buf, num); len += num; } - for (screen = 0; screen < nscreens; screen++) { - draw_key_feedback(dpy, locks, screen); - } + for (screen = 0; screen < nscreens; screen++) + draw_key_feedback(dpy, locks, screen); break; } color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT); From 172fccf997ee3f5c6273e730cec03343749f8616 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 01:18:50 +0530 Subject: [PATCH 07/10] Add blocks enabled flag --- slock.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/slock.c b/slock.c index b29bc64..bd2e3d1 100644 --- a/slock.c +++ b/slock.c @@ -217,8 +217,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, memcpy(passwd + len, buf, num); len += num; } - for (screen = 0; screen < nscreens; screen++) - draw_key_feedback(dpy, locks, screen); + if (blocks_enabled) + for (screen = 0; screen < nscreens; screen++) + draw_key_feedback(dpy, locks, screen); break; } color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT); From d409a08e7f999fd242aa71c7b1607c9a69f70c49 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 01:35:04 +0530 Subject: [PATCH 08/10] Removes comment --- config.def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index 945f362..29734e2 100644 --- a/config.def.h +++ b/config.def.h @@ -16,7 +16,7 @@ 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; // 0 = full height +static const int blocks_height = 16; // position static const int blocks_x = 0; From 8132e0420eb0962c0859d66cc839679a86fb7b09 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 19:28:33 +0530 Subject: [PATCH 09/10] Keyfeedback - Fixes 'height' full height --- slock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slock.c b/slock.c index bd2e3d1..dbbf070 100644 --- a/slock.c +++ b/slock.c @@ -98,7 +98,7 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) int width = blocks_width, height = blocks_height; - if (blocks_height == 0 || blocks_width == 0) { + if (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); @@ -110,7 +110,7 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) unsigned int position = rand() % blocks_count; XClearWindow(dpy, win); - XFillRectangle(dpy, win, gc, blocks_x + position*block_width, blocks_y, block_width, blocks_height); + XFillRectangle(dpy, win, gc, blocks_x + position*block_width, blocks_y, block_width, height); XFreeGC(dpy, gc); } From 282fe89b5330c259bdde17ae331c9a68f615063d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 3 Aug 2020 19:33:13 +0530 Subject: [PATCH 10/10] Cleanup --- slock.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/slock.c b/slock.c index dbbf070..cf52710 100644 --- a/slock.c +++ b/slock.c @@ -97,20 +97,23 @@ draw_key_feedback(Display *dpy, struct lock **locks, int screen) GC gc = XCreateGC(dpy, win, GCForeground, &gr_values); int width = blocks_width, - height = blocks_height; - if (blocks_width == 0) { + 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 = blocks_width ? blocks_width : screen_width; - height = blocks_height ? blocks_height : screen_height; + 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, blocks_x + position*block_width, blocks_y, block_width, height); + XFillRectangle(dpy, win, gc, x + position*block_width, y, block_width, height); XFreeGC(dpy, gc); }