sites

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

slock-multi-image-1.5.diff (6714B)


      1 From cc85ae2c548660f7ccbcd244dbcfb52420160973 Mon Sep 17 00:00:00 2001
      2 From: Drew Marino <drewmarino25@gmail.com>
      3 Date: Sat, 2 Dec 2023 02:30:59 -0500
      4 Subject: [PATCH] Allow for per-status images instead of colors
      5 
      6 ---
      7  config.def.h |  3 +-
      8  config.mk    |  2 +-
      9  slock.c      | 78 ++++++++++++++++++++++++++++++++++++++++++++--------
     10  3 files changed, 70 insertions(+), 13 deletions(-)
     11 
     12 diff --git a/config.def.h b/config.def.h
     13 index 9855e21..ee79df9 100644
     14 --- a/config.def.h
     15 +++ b/config.def.h
     16 @@ -1,7 +1,8 @@
     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 +/* image files can be used if absolute path is given */
     23  static const char *colorname[NUMCOLS] = {
     24  	[INIT] =   "black",     /* after initialization */
     25  	[INPUT] =  "#005577",   /* during input */
     26 diff --git a/config.mk b/config.mk
     27 index 514c236..db0641c 100644
     28 --- a/config.mk
     29 +++ b/config.mk
     30 @@ -12,7 +12,7 @@ X11LIB = /usr/X11R6/lib
     31  
     32  # includes and libs
     33  INCS = -I. -I/usr/include -I${X11INC}
     34 -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
     35 +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lImlib2
     36  
     37  # flags
     38  CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
     39 diff --git a/slock.c b/slock.c
     40 index b2f14e3..a0378df 100644
     41 --- a/slock.c
     42 +++ b/slock.c
     43 @@ -18,6 +18,7 @@
     44  #include <X11/keysym.h>
     45  #include <X11/Xlib.h>
     46  #include <X11/Xutil.h>
     47 +#include <Imlib2.h>
     48  
     49  #include "arg.h"
     50  #include "util.h"
     51 @@ -190,9 +191,12 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
     52  			color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
     53  			if (running && oldc != color) {
     54  				for (screen = 0; screen < nscreens; screen++) {
     55 -					XSetWindowBackground(dpy,
     56 -					                     locks[screen]->win,
     57 -					                     locks[screen]->colors[color]);
     58 +					if (colorname[color][0]!='/')
     59 +						XSetWindowBackground(dpy, locks[screen]->win,
     60 +						                     locks[screen]->colors[color]);
     61 +					else
     62 +						XSetWindowBackgroundPixmap(dpy, locks[screen]->win,
     63 +						                           locks[screen]->colors[color]);
     64  					XClearWindow(dpy, locks[screen]->win);
     65  				}
     66  				oldc = color;
     67 @@ -220,14 +224,17 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
     68  }
     69  
     70  static struct lock *
     71 -lockscreen(Display *dpy, struct xrandr *rr, int screen)
     72 +lockscreen(Display *dpy, struct xrandr *rr, int screen,
     73 +           Imlib_Image images[NUMCOLS])
     74  {
     75  	char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
     76 -	int i, ptgrab, kbgrab;
     77 +	int i, j, ptgrab, kbgrab, num_monitors, img_width, img_height;
     78  	struct lock *lock;
     79  	XColor color, dummy;
     80  	XSetWindowAttributes wa;
     81  	Cursor invisible;
     82 +	Imlib_Image image;
     83 +	XRRMonitorInfo *monitors;
     84  
     85  	if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
     86  		return NULL;
     87 @@ -235,15 +242,48 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
     88  	lock->screen = screen;
     89  	lock->root = RootWindow(dpy, lock->screen);
     90  
     91 +	/* set colors for each state, if is image render the image into pixmap */
     92 +	imlib_context_set_display(dpy);
     93 +	imlib_context_set_visual(DefaultVisual(dpy, lock->screen));
     94 +	imlib_context_set_colormap(DefaultColormap(dpy, lock->screen));
     95 +
     96  	for (i = 0; i < NUMCOLS; i++) {
     97 -		XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
     98 -		                 colorname[i], &color, &dummy);
     99 -		lock->colors[i] = color.pixel;
    100 +		if (colorname[i][0]!='/') {
    101 +			XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
    102 +			                 colorname[i], &color, &dummy);
    103 +			lock->colors[i] = color.pixel;
    104 +		} else {
    105 +			lock->colors[i] = XCreatePixmap(dpy, lock->root,
    106 +			                                DisplayWidth(dpy, lock->screen),
    107 +			                                DisplayHeight(dpy, lock->screen),
    108 +			                                DefaultDepth(dpy, lock->screen));
    109 +			imlib_context_set_image(images[i]);
    110 +			img_width = imlib_image_get_width();
    111 +			img_height = imlib_image_get_height();
    112 +			monitors = XRRGetMonitors(dpy, RootWindow(dpy, lock->screen),
    113 +			                          True, &num_monitors);
    114 +			image = imlib_create_image(XDisplayWidth(dpy, lock->screen),
    115 +			                           XDisplayHeight(dpy, lock->screen));
    116 +			imlib_context_set_image(image);
    117 +			/* resize the image for each monitor in the screen */
    118 +			for (j=0; j < num_monitors; j++)
    119 +				imlib_blend_image_onto_image(images[i], 0, 0, 0, img_width,
    120 +				                             img_height, monitors[j].x,
    121 +				                             monitors[j].y,
    122 +				                             monitors[j].width,
    123 +				                             monitors[j].height);
    124 +			imlib_context_set_drawable(lock->colors[i]);
    125 +			imlib_render_image_on_drawable(0, 0);
    126 +			imlib_free_image();
    127 +		}
    128  	}
    129  
    130  	/* init */
    131  	wa.override_redirect = 1;
    132 -	wa.background_pixel = lock->colors[INIT];
    133 +	if (colorname[INIT][0]!='/')
    134 +		wa.background_pixel = lock->colors[INIT];
    135 +	else
    136 +		wa.background_pixmap = lock->colors[INIT];
    137  	lock->win = XCreateWindow(dpy, lock->root, 0, 0,
    138  	                          DisplayWidth(dpy, lock->screen),
    139  	                          DisplayHeight(dpy, lock->screen),
    140 @@ -313,7 +353,8 @@ main(int argc, char **argv) {
    141  	gid_t dgid;
    142  	const char *hash;
    143  	Display *dpy;
    144 -	int s, nlocks, nscreens;
    145 +	int s, i, nlocks, nscreens;
    146 +	Imlib_Image images[NUMCOLS];
    147  
    148  	ARGBEGIN {
    149  	case 'v':
    150 @@ -347,6 +388,14 @@ main(int argc, char **argv) {
    151  	if (!(dpy = XOpenDisplay(NULL)))
    152  		die("slock: cannot open display\n");
    153  
    154 +	/* load image files, if there are any */
    155 +	for (i = 0; i < NUMCOLS; i++)
    156 +		if (colorname[i][0]=='/')
    157 +			if ((images[i] = imlib_load_image_with_errno_return(colorname[i],
    158 +			                                                    &errno))==NULL)
    159 +				die("slock: unable to load image file: %s: %s\n",
    160 +				    colorname[i], strerror(errno));
    161 +
    162  	/* drop privileges */
    163  	if (setgroups(0, NULL) < 0)
    164  		die("slock: setgroups: %s\n", strerror(errno));
    165 @@ -363,7 +412,7 @@ main(int argc, char **argv) {
    166  	if (!(locks = calloc(nscreens, sizeof(struct lock *))))
    167  		die("slock: out of memory\n");
    168  	for (nlocks = 0, s = 0; s < nscreens; s++) {
    169 -		if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL)
    170 +		if ((locks[s] = lockscreen(dpy, &rr, s, images)) != NULL)
    171  			nlocks++;
    172  		else
    173  			break;
    174 @@ -374,6 +423,13 @@ main(int argc, char **argv) {
    175  	if (nlocks != nscreens)
    176  		return 1;
    177  
    178 +	/* unload image files */
    179 +	for (i = 0; i < NUMCOLS; i++)
    180 +		if (colorname[i][0]=='/') {
    181 +			imlib_context_set_image(images[i]);
    182 +			imlib_free_image();
    183 +		}
    184 +
    185  	/* run post-lock command */
    186  	if (argc > 0) {
    187  		switch (fork()) {
    188 -- 
    189 2.43.0
    190