Adding background image patch

This commit is contained in:
bakkeby 2022-03-28 14:22:21 +02:00
parent 312951b978
commit e71a42ee2b
9 changed files with 100 additions and 21 deletions

View File

@ -26,6 +26,8 @@ slock tool, how to install it and how it works.
### Changelog:
2022-03-28 - Added the background image patch
2021-09-13 - Added the dwm logo patch
2021-09-09 - Added the auto-timeout, failure-command and secret-password patches
@ -49,6 +51,9 @@ slock tool, how to install it and how it works.
- [auto-timeout](https://tools.suckless.org/slock/patches/auto-timeout/)
- allows for a command to be executed after a specified time of inactivity
- [background_image](https://tools.suckless.org/slock/patches/background-image/)
- sets the lockscreen picture to a background image
- [blur_pixelated_screen](https://tools.suckless.org/slock/patches/blur-pixelated-screen/)
- sets the lockscreen picture to a blured or pixelated screenshot

View File

@ -31,6 +31,11 @@ static const char * text_color = "#ffffff";
static const char * font_name = "6x10";
#endif // MESSAGE_PATCH | COLOR_MESSAGE_PATCH
#if BACKGROUND_IMAGE_PATCH
/* Background image path, should be available to the user above */
static const char * background_image = "";
#endif // BACKGROUND_IMAGE_PATCH
#if DWM_LOGO_PATCH
/* insert grid pattern with scale 1:1, the size can be changed with logosize */
static const int logosize = 75;
@ -57,19 +62,24 @@ static XRectangle rectangles[] = {
*/
ResourcePref resources[] = {
#if DWM_LOGO_PATCH && !BLUR_PIXELATED_SCREEN_PATCH
{ "color2", STRING, &colorname[BACKGROUND] },
{ "background", STRING, &colorname[BACKGROUND] },
#endif //DWM_LOGO_PATCH
{ "color0", STRING, &colorname[INIT] },
{ "color4", STRING, &colorname[INPUT] },
{ "color1", STRING, &colorname[FAILED] },
#if BACKGROUND_IMAGE_PATCH
{ "bg_image", STRING, &background_image },
#endif // BACKGROUND_IMAGE_PATCH
{ "locked", STRING, &colorname[INIT] },
{ "input", STRING, &colorname[INPUT] },
{ "failed", STRING, &colorname[FAILED] },
#if CAPSCOLOR_PATCH
{ "color3", STRING, &colorname[CAPS] },
{ "capslock", STRING, &colorname[CAPS] },
#endif // CAPSCOLOR_PATCH
#if PAMAUTH_PATCH
{ "color5", STRING, &colorname[PAM] },
{ "pamauth", STRING, &colorname[PAM] },
#endif // PAMAUTH_PATCH
#if MESSAGE_PATCH || COLOR_MESSAGE_PATCH
{ "color6", STRING, &text_color },
{ "message", STRING, &message },
{ "text_color", STRING, &text_color },
{ "font_name", STRING, &font_name },
#endif // MESSAGE_PATCH | COLOR_MESSAGE_PATCH
};
#endif // XRESOURCES_PATCH

View File

@ -17,7 +17,7 @@ X11LIB = /usr/X11R6/lib
# Uncomment for pam auth patch / PAMAUTH_PATCH
#PAM=-lpam
# Uncomment for blur pixelated screen patch / BLUR_PIXELATED_SCREEN_PATCH
# Uncomment for blur pixelated screen and background image patches / BLUR_PIXELATED_SCREEN_PATCH, BACKGROUND_IMAGE_PATCH
#IMLIB=-lImlib2
# includes and libs

47
patch/background_image.c Normal file
View File

@ -0,0 +1,47 @@
#include <Imlib2.h>
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_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)
{
/* Load picture */
Imlib_Image buffer = imlib_load_image(background_image);
imlib_context_set_image(buffer);
int background_image_width = imlib_image_get_width();
int background_image_height = imlib_image_get_height();
/* Create an image to be rendered */
Screen *scr = ScreenOfDisplay(dpy, DefaultScreen(dpy));
image = imlib_create_image(scr->width, scr->height);
imlib_context_set_image(image);
/* Fill the image for every X monitor */
XRRMonitorInfo *monitors;
int number_of_monitors;
monitors = XRRGetMonitors(dpy, RootWindow(dpy, XScreenNumberOfScreen(scr)), True, &number_of_monitors);
int i;
for (i = 0; i < number_of_monitors; i++) {
imlib_blend_image_onto_image(buffer, 0, 0, 0, background_image_width, background_image_height, monitors[i].x, monitors[i].y, monitors[i].width, monitors[i].height);
}
/* Clean up */
imlib_context_set_image(buffer);
imlib_free_image();
imlib_context_set_image(image);
}

4
patch/background_image.h Normal file
View File

@ -0,0 +1,4 @@
#include <Imlib2.h>
static void create_lock_image(Display *dpy);
static void render_lock_image(Display *dpy, struct lock *lock, Imlib_Image image);

View File

@ -1,5 +1,7 @@
/* Patches */
#if BLUR_PIXELATED_SCREEN_PATCH
#if BACKGROUND_IMAGE_PATCH
#include "background_image.c"
#elif BLUR_PIXELATED_SCREEN_PATCH
#include "blur_pixelated_screen.c"
#endif

View File

@ -1,5 +1,7 @@
/* Patches */
#if BLUR_PIXELATED_SCREEN_PATCH
#if BACKGROUND_IMAGE_PATCH
#include "background_image.h"
#elif BLUR_PIXELATED_SCREEN_PATCH
#include "blur_pixelated_screen.h"
#endif

View File

@ -21,9 +21,18 @@
*/
#define AUTO_TIMEOUT_PATCH 0
/* This patch adds a background image for slock.
* This patch depends on the Imlib2 library, uncomment the relevant line in
* config.mk when enabling this patch.
* This patch is a variant of the blur pixelated screen patch and takes precedence over that.
* https://tools.suckless.org/slock/patches/background-image/
*/
#define BACKGROUND_IMAGE_PATCH 0
/* 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.
* The background image patch takes precedence over this patch.
* https://tools.suckless.org/slock/patches/blur-pixelated-screen/
*/
#define BLUR_PIXELATED_SCREEN_PATCH 0

22
slock.c
View File

@ -107,9 +107,9 @@ struct lock {
int screen;
Window root, win;
Pixmap pmap;
#if BLUR_PIXELATED_SCREEN_PATCH
#if BLUR_PIXELATED_SCREEN_PATCH || BACKGROUND_IMAGE_PATCH
Pixmap bgmap;
#endif // BLUR_PIXELATED_SCREEN_PATCH
#endif // BLUR_PIXELATED_SCREEN_PATCH | BACKGROUND_IMAGE_PATCH
unsigned long colors[NUMCOLS];
#if DWM_LOGO_PATCH
unsigned int x, y;
@ -408,7 +408,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
for (screen = 0; screen < nscreens; screen++) {
#if DWM_LOGO_PATCH
drawlogo(dpy, locks[screen], color);
#elif BLUR_PIXELATED_SCREEN_PATCH
#elif BLUR_PIXELATED_SCREEN_PATCH || BACKGROUND_IMAGE_PATCH
if (locks[screen]->bgmap)
XSetWindowBackgroundPixmap(dpy, locks[screen]->win, locks[screen]->bgmap);
else
@ -488,9 +488,9 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
lock->screen = screen;
lock->root = RootWindow(dpy, lock->screen);
#if BLUR_PIXELATED_SCREEN_PATCH
#if BLUR_PIXELATED_SCREEN_PATCH || BACKGROUND_IMAGE_PATCH
render_lock_image(dpy, lock, image);
#endif // BLUR_PIXELATED_SCREEN_PATCH
#endif // BLUR_PIXELATED_SCREEN_PATCH | BACKGROUND_IMAGE_PATCH
for (i = 0; i < NUMCOLS; i++) {
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
@ -539,10 +539,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
CopyFromParent,
DefaultVisual(dpy, lock->screen),
CWOverrideRedirect | CWBackPixel, &wa);
#if BLUR_PIXELATED_SCREEN_PATCH
#if BLUR_PIXELATED_SCREEN_PATCH || BACKGROUND_IMAGE_PATCH
if (lock->bgmap)
XSetWindowBackgroundPixmap(dpy, lock->win, lock->bgmap);
#endif // BLUR_PIXELATED_SCREEN_PATCH
#endif // BLUR_PIXELATED_SCREEN_PATCH | BACKGROUND_IMAGE_PATCH
lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
&color, &color, 0, 0);
@ -698,14 +698,14 @@ 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
#if BLUR_PIXELATED_SCREEN_PATCH || BACKGROUND_IMAGE_PATCH
create_lock_image(dpy);
#endif // BLUR_PIXELATED_SCREEN_PATCH | BACKGROUND_IMAGE_PATCH
#if KEYPRESS_FEEDBACK_PATCH
time_t t;
srand((unsigned) time(&t));