sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slock-background-image-20220318-1c5a538.diff (5467B)


      1 From 1c5a5383a1cf3351fe9c80a21cfbc98c5ec4355d Mon Sep 17 00:00:00 2001
      2 From: Yan Doroshenko <yan1994doroshenko@gmail.com>
      3 Date: Fri, 18 Mar 2022 12:28:13 +0100
      4 Subject: [PATCH] Provide a way to set a background image
      5 
      6 ---
      7  config.def.h |  5 ++++-
      8  config.mk    |  2 +-
      9  slock.c      | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
     10  3 files changed, 52 insertions(+), 5 deletions(-)
     11 
     12 diff --git a/config.def.h b/config.def.h
     13 index 9855e21..eb88b3d 100644
     14 --- a/config.def.h
     15 +++ b/config.def.h
     16 @@ -1,6 +1,6 @@
     17  /* user and group to drop privileges to */
     18  static const char *user  = "nobody";
     19 -static const char *group = "nogroup";
     20 +static const char *group = "nobody";
     21  
     22  static const char *colorname[NUMCOLS] = {
     23  	[INIT] =   "black",     /* after initialization */
     24 @@ -10,3 +10,6 @@ static const char *colorname[NUMCOLS] = {
     25  
     26  /* treat a cleared input like a wrong password (color) */
     27  static const int failonclear = 1;
     28 +
     29 +/* Background image path, should be available to the user above */
     30 +static const char* background_image = "";
     31 diff --git a/config.mk b/config.mk
     32 index 74429ae..987819e 100644
     33 --- a/config.mk
     34 +++ b/config.mk
     35 @@ -12,7 +12,7 @@ X11LIB = /usr/X11R6/lib
     36  
     37  # includes and libs
     38  INCS = -I. -I/usr/include -I${X11INC}
     39 -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
     40 +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lImlib2
     41  
     42  # flags
     43  CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
     44 diff --git a/slock.c b/slock.c
     45 index 5ae738c..345a279 100644
     46 --- a/slock.c
     47 +++ b/slock.c
     48 @@ -18,6 +18,7 @@
     49  #include <X11/keysym.h>
     50  #include <X11/Xlib.h>
     51  #include <X11/Xutil.h>
     52 +#include <Imlib2.h>
     53  
     54  #include "arg.h"
     55  #include "util.h"
     56 @@ -35,6 +36,7 @@ struct lock {
     57  	int screen;
     58  	Window root, win;
     59  	Pixmap pmap;
     60 +	Pixmap bgmap;
     61  	unsigned long colors[NUMCOLS];
     62  };
     63  
     64 @@ -46,6 +48,8 @@ struct xrandr {
     65  
     66  #include "config.h"
     67  
     68 +Imlib_Image image;
     69 +
     70  static void
     71  die(const char *errstr, ...)
     72  {
     73 @@ -190,9 +194,10 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
     74  			color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
     75  			if (running && oldc != color) {
     76  				for (screen = 0; screen < nscreens; screen++) {
     77 -					XSetWindowBackground(dpy,
     78 -					                     locks[screen]->win,
     79 -					                     locks[screen]->colors[color]);
     80 +                                        if (locks[screen]->bgmap)
     81 +                                                XSetWindowBackgroundPixmap(dpy, locks[screen]->win, locks[screen]->bgmap);
     82 +                                        else
     83 +                                                XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[0]);
     84  					XClearWindow(dpy, locks[screen]->win);
     85  				}
     86  				oldc = color;
     87 @@ -235,6 +240,17 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
     88  	lock->screen = screen;
     89  	lock->root = RootWindow(dpy, lock->screen);
     90  
     91 +        if(image)
     92 +        {
     93 +            lock->bgmap = XCreatePixmap(dpy, lock->root, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen), DefaultDepth(dpy, lock->screen));
     94 +            imlib_context_set_display(dpy);
     95 +            imlib_context_set_visual(DefaultVisual(dpy, lock->screen));
     96 +            imlib_context_set_colormap(DefaultColormap(dpy, lock->screen));
     97 +            imlib_context_set_drawable(lock->bgmap);
     98 +            imlib_render_image_on_drawable(0, 0);
     99 +            imlib_free_image();
    100 +        }
    101 +
    102  	for (i = 0; i < NUMCOLS; i++) {
    103  		XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
    104  		                 colorname[i], &color, &dummy);
    105 @@ -251,6 +267,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
    106  	                          CopyFromParent,
    107  	                          DefaultVisual(dpy, lock->screen),
    108  	                          CWOverrideRedirect | CWBackPixel, &wa);
    109 +        if(lock->bgmap)
    110 +          XSetWindowBackgroundPixmap(dpy, lock->win, lock->bgmap);
    111  	lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
    112  	invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
    113  	                                &color, &color, 0, 0);
    114 @@ -355,6 +373,32 @@ main(int argc, char **argv) {
    115  	if (setuid(duid) < 0)
    116  		die("slock: setuid: %s\n", strerror(errno));
    117  
    118 +	/* Load picture */
    119 +        Imlib_Image buffer = imlib_load_image(background_image);
    120 +	imlib_context_set_image(buffer);
    121 +        int background_image_width = imlib_image_get_width();
    122 +        int background_image_height = imlib_image_get_height();
    123 +
    124 +        /* Create an image to be rendered */
    125 +	Screen *scr = ScreenOfDisplay(dpy, DefaultScreen(dpy));
    126 +	image = imlib_create_image(scr->width, scr->height);
    127 +        imlib_context_set_image(image);
    128 +
    129 +        /* Fill the image for every X monitor */
    130 +        XRRMonitorInfo	*monitors;
    131 +        int number_of_monitors;
    132 +        monitors = XRRGetMonitors(dpy, RootWindow(dpy, XScreenNumberOfScreen(scr)), True, &number_of_monitors);
    133 +
    134 +        int i;
    135 +        for (i = 0; i < number_of_monitors; i++) {
    136 +            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);
    137 +        }
    138 +
    139 +        /* Clean up */
    140 +        imlib_context_set_image(buffer);
    141 +        imlib_free_image();
    142 +        imlib_context_set_image(image);
    143 +
    144  	/* check for Xrandr support */
    145  	rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);
    146  
    147 -- 
    148 2.35.1
    149