From 74c66223a5743aba14789cfb07ffae1e9d4d6bc8 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Mon, 3 Aug 2020 16:56:33 +0200 Subject: [PATCH] Adding blur pixelated screen patch --- README.md | 5 ++- config.def.h | 11 ++++++ config.mk | 5 ++- patch/blur_pixelated_screen.c | 70 +++++++++++++++++++++++++++++++++++ patch/blur_pixelated_screen.h | 4 ++ patch/include.c | 4 ++ patch/include.h | 4 ++ patches.def.h | 7 ++++ slock.c | 22 +++++++++++ 9 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 patch/blur_pixelated_screen.c create mode 100644 patch/blur_pixelated_screen.h diff --git a/README.md b/README.md index 2495481c22238f23e28e79581ae0f5abb7b0a49e..cf4bbbdff15e0dfdfb7b84245ad3e41cf3584287 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Refer to [https://tools.suckless.org/slock/](https://tools.suckless.org/slock/) ### Changelog: -2020-08-03 - Added keypress_feedback patch +2020-08-03 - Added keypress_feedback and blur_pixelated_screen patches 2019-11-27 - Added xresources patch @@ -25,6 +25,9 @@ Refer to [https://tools.suckless.org/slock/](https://tools.suckless.org/slock/) ### Patches included: + - [blur_pixelated_screen](https://tools.suckless.org/slock/patches/blur-pixelated-screen/) + - sets the lockscreen picture to a blured or pixelated screenshot + - [capscolor](https://tools.suckless.org/slock/patches/capscolor/) - adds an additional color to indicate the state of Caps Lock diff --git a/config.def.h b/config.def.h index 56674322799a8696ac94d512755ac4a9e233cc77..406a00e02bcf9b1f8b03e10fc50a48b0abc48145 100644 --- a/config.def.h +++ b/config.def.h @@ -37,6 +37,17 @@ ResourcePref resources[] = { /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; +#if BLUR_PIXELATED_SCREEN_PATCH +/* Enable blur */ +#define BLUR +/* Set blur radius */ +static const int blurRadius=5; +/* Enable Pixelation */ +//#define PIXELATION +/* Set pixelation radius */ +static const int pixelSize=10; +#endif // BLUR_PIXELATED_SCREEN_PATCH + #if CONTROLCLEAR_PATCH /* allow control key to trigger fail on clear */ static const int controlkeyclear = 0; diff --git a/config.mk b/config.mk index ad12d755b95b215f350354ddc67c9c2348527d90..f0d1539f90bdad228dd7e72ad7a16bc7948dd4d2 100644 --- a/config.mk +++ b/config.mk @@ -16,9 +16,12 @@ X11LIB = /usr/X11R6/lib # Uncomment for pam auth patch / PAMAUTH_PATCH #PAM=-lpam +# Uncomment for blur pixelated screen patch / BLUR_PIXELATED_SCREEN_PATCH +#IMLIB=-lImlib2 + # includes and libs INCS = -I. -I/usr/include -I${X11INC} -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr ${XINERAMA} ${PAM} +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr ${XINERAMA} ${PAM} ${IMLIB} # flags CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H diff --git a/patch/blur_pixelated_screen.c b/patch/blur_pixelated_screen.c new file mode 100644 index 0000000000000000000000000000000000000000..75ae2645912f22281ad28f1019211c3329fb14e9 --- /dev/null +++ b/patch/blur_pixelated_screen.c @@ -0,0 +1,70 @@ +#include + +Imlib_Image image; + +void +render_lock_image(Display *dpy, struct lock *lock, Imlib_Image image) +{ + if (image) { + lock->bgmap = XCreatePixmap(dpy, lock->root, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen), DefaultDepth(dpy, lock->screen)); + imlib_context_set_image(image); + imlib_context_set_display(dpy); + imlib_context_set_visual(DefaultVisual(dpy, lock->screen)); + imlib_context_set_colormap(DefaultColormap(dpy, lock->screen)); + imlib_context_set_drawable(lock->bgmap); + imlib_render_image_on_drawable(0, 0); + imlib_free_image(); + } +} + +void +create_lock_image(Display *dpy) +{ + /* Create screenshot Image */ + Screen *scr = ScreenOfDisplay(dpy, DefaultScreen(dpy)); + image = imlib_create_image(scr->width,scr->height); + imlib_context_set_image(image); + imlib_context_set_display(dpy); + imlib_context_set_visual(DefaultVisual(dpy,0)); + imlib_context_set_drawable(RootWindow(dpy,XScreenNumberOfScreen(scr))); + imlib_copy_drawable_to_image(0,0,0,scr->width,scr->height,0,0,1); + + #ifdef BLUR + /* Blur function */ + imlib_image_blur(blurRadius); + #endif // BLUR + + #ifdef PIXELATION + /* Pixelation */ + int width = scr->width; + int height = scr->height; + + for (int y = 0; y < height; y += pixelSize) { + for (int x = 0; x < width; x += pixelSize) { + int red = 0; + int green = 0; + int blue = 0; + + Imlib_Color pixel; + Imlib_Color* pp; + pp = &pixel; + for (int j = 0; j < pixelSize && j < height; j++) { + for (int i = 0; i < pixelSize && i < width; i++) { + imlib_image_query_pixel(x+i,y+j,pp); + red += pixel.red; + green += pixel.green; + blue += pixel.blue; + } + } + red /= (pixelSize*pixelSize); + green /= (pixelSize*pixelSize); + blue /= (pixelSize*pixelSize); + imlib_context_set_color(red,green,blue,pixel.alpha); + imlib_image_fill_rectangle(x,y,pixelSize,pixelSize); + red = 0; + green = 0; + blue = 0; + } + } + #endif +} \ No newline at end of file diff --git a/patch/blur_pixelated_screen.h b/patch/blur_pixelated_screen.h new file mode 100644 index 0000000000000000000000000000000000000000..2a9050f34cf298196cff7023c9889aa3b54a819e --- /dev/null +++ b/patch/blur_pixelated_screen.h @@ -0,0 +1,4 @@ +#include + +static void create_lock_image(Display *dpy); +static void render_lock_image(Display *dpy, struct lock *lock, Imlib_Image image); \ No newline at end of file diff --git a/patch/include.c b/patch/include.c index 1cb977bf9bd0ac85647091dcd33e3b5ce92cde2a..2fb96eee7894dfada70a322f32aec1706894b3bd 100644 --- a/patch/include.c +++ b/patch/include.c @@ -1,4 +1,8 @@ /* Patches */ +#if BLUR_PIXELATED_SCREEN_PATCH +#include "blur_pixelated_screen.c" +#endif + #if MESSAGE_PATCH #include "message.c" #endif diff --git a/patch/include.h b/patch/include.h index 5f43fa78821f2121c4e42406de0cb56284f5af8b..06ac17c1da0707392edc203234e466b0e08cc1f4 100644 --- a/patch/include.h +++ b/patch/include.h @@ -1,4 +1,8 @@ /* Patches */ +#if BLUR_PIXELATED_SCREEN_PATCH +#include "blur_pixelated_screen.h" +#endif + #if PAMAUTH_PATCH #include "pamauth.h" #endif diff --git a/patches.def.h b/patches.def.h index 91bba23d9a17ff6e48bfa7bb2ef6b2ee40f3dfee..02d77f7c6a21f2de2bc9def3ba1583033954127f 100644 --- a/patches.def.h +++ b/patches.def.h @@ -9,6 +9,13 @@ /* Patches */ +/* This patch sets the lockscreen picture to a blured or pixelated screenshot. + * This patch depends on the Imlib2 library, uncomment the relevant line in + * config.mk when enabling this patch. + * https://tools.suckless.org/slock/patches/blur-pixelated-screen/ + */ +#define BLUR_PIXELATED_SCREEN_PATCH 0 + /* This patch introduces an additional color to indicate the state of Caps Lock. * https://tools.suckless.org/slock/patches/capscolor/ */ diff --git a/slock.c b/slock.c index 9497925bb63c5e10b75c768fb106256472369c13..a8b37f1db41e78c95e77e138461d692c3cc479ef 100644 --- a/slock.c +++ b/slock.c @@ -65,6 +65,9 @@ struct lock { int screen; Window root, win; Pixmap pmap; + #if BLUR_PIXELATED_SCREEN_PATCH + Pixmap bgmap; + #endif // BLUR_PIXELATED_SCREEN_PATCH unsigned long colors[NUMCOLS]; }; @@ -321,9 +324,16 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, #endif // CAPSCOLOR_PATCH if (running && oldc != color) { for (screen = 0; screen < nscreens; screen++) { + #if BLUR_PIXELATED_SCREEN_PATCH + if (locks[screen]->bgmap) + XSetWindowBackgroundPixmap(dpy, locks[screen]->win, locks[screen]->bgmap); + else + XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[0]); + #else XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]); + #endif // BLUR_PIXELATED_SCREEN_PATCH XClearWindow(dpy, locks[screen]->win); #if MESSAGE_PATCH writemessage(dpy, locks[screen]->win, screen); @@ -369,6 +379,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) lock->screen = screen; lock->root = RootWindow(dpy, lock->screen); + #if BLUR_PIXELATED_SCREEN_PATCH + render_lock_image(dpy, lock, image); + #endif // BLUR_PIXELATED_SCREEN_PATCH + for (i = 0; i < NUMCOLS; i++) { XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy); @@ -385,6 +399,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) CopyFromParent, DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa); + #if BLUR_PIXELATED_SCREEN_PATCH + if (lock->bgmap) + XSetWindowBackgroundPixmap(dpy, lock->win, lock->bgmap); + #endif // BLUR_PIXELATED_SCREEN_PATCH lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8); invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0); @@ -528,6 +546,10 @@ main(int argc, char **argv) { if (setuid(duid) < 0) die("slock: setuid: %s\n", strerror(errno)); + #if BLUR_PIXELATED_SCREEN_PATCH + create_lock_image(dpy); + #endif // BLUR_PIXELATED_SCREEN_PATCH + #if XRESOURCES_PATCH config_init(dpy); #endif // XRESOURCES_PATCH