slock-foreground-and-background-20210611-35633d4.diff (10213B)
1 From 61f4d247d4060f42cbdbf2771061f0e165ada3a9 Mon Sep 17 00:00:00 2001 2 From: KNIX 3 <nki3@protonmail.com> 3 Date: Fri, 11 Jun 2021 13:20:54 -0400 4 Subject: [PATCH] Foreground and Background 5 6 --- 7 config.def.h | 28 +++++++++ 8 config.mk | 16 ++++-- 9 slock.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++--- 10 3 files changed, 187 insertions(+), 13 deletions(-) 11 12 diff --git a/config.def.h b/config.def.h 13 index 9855e21..ceceeb0 100644 14 --- a/config.def.h 15 +++ b/config.def.h 16 @@ -10,3 +10,31 @@ static const char *colorname[NUMCOLS] = { 17 18 /* treat a cleared input like a wrong password (color) */ 19 static const int failonclear = 1; 20 + 21 +/* insert grid pattern with scale 1:1, the size can be changed with logosize */ 22 +static const int logosize = 75; 23 +/* grid width and height for right center alignment */ 24 +static const int logow = 12; 25 +static const int logoh = 6; 26 + 27 +static XRectangle rectangles[9] = { 28 + /* x y w h */ 29 + { 0, 3, 1, 3 }, 30 + { 1, 3, 2, 1 }, 31 + { 0, 5, 8, 1 }, 32 + { 3, 0, 1, 5 }, 33 + { 5, 3, 1, 2 }, 34 + { 7, 3, 1, 2 }, 35 + { 8, 3, 4, 1 }, 36 + { 9, 4, 1, 2 }, 37 + { 11, 4, 1, 2 }, 38 +}; 39 + 40 +/*Enable blur*/ 41 +#define BLUR 42 +/*Set blur radius*/ 43 +static const int blurRadius=5; 44 +/*Enable Pixelation*/ 45 +//#define PIXELATION 46 +/*Set pixelation radius*/ 47 +static const int pixelSize=0; 48 diff --git a/config.mk b/config.mk 49 index 74429ae..e851ede 100644 50 --- a/config.mk 51 +++ b/config.mk 52 @@ -10,13 +10,21 @@ MANPREFIX = ${PREFIX}/share/man 53 X11INC = /usr/X11R6/include 54 X11LIB = /usr/X11R6/lib 55 56 +# Xinerama 57 +XINERAMALIBS = -lXinerama 58 +XINERAMAFLAGS = -DXINERAMA 59 + 60 +# freetype 61 +FREETYPELIBS = -lXft 62 +FREETYPEINC = /usr/include/freetype2 63 + 64 # includes and libs 65 -INCS = -I. -I/usr/include -I${X11INC} 66 -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr 67 +INCS = -I. -I/usr/include -I${X11INC} -I${FREETYPEINC} 68 +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lXext -lXrandr -lImlib2 69 70 # flags 71 -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H 72 -CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS} 73 +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H ${XINERAMAFLAGS} 74 +CFLAGS = -std=c99 -pedantic -Wall -Ofast ${INCS} ${CPPFLAGS} 75 LDFLAGS = -s ${LIBS} 76 COMPATSRC = explicit_bzero.c 77 78 diff --git a/slock.c b/slock.c 79 index 5ae738c..efbe833 100644 80 --- a/slock.c 81 +++ b/slock.c 82 @@ -1,5 +1,6 @@ 83 /* See LICENSE file for license details. */ 84 -#define _XOPEN_SOURCE 500 85 +#define _XOPEN_SOURCE 500 86 +#define LENGTH(X) (sizeof X / sizeof X[0]) 87 #if HAVE_SHADOW_H 88 #include <shadow.h> 89 #endif 90 @@ -15,9 +16,14 @@ 91 #include <unistd.h> 92 #include <sys/types.h> 93 #include <X11/extensions/Xrandr.h> 94 +#ifdef XINERAMA 95 +#include <X11/extensions/Xinerama.h> 96 +#endif 97 #include <X11/keysym.h> 98 #include <X11/Xlib.h> 99 #include <X11/Xutil.h> 100 +#include <X11/Xft/Xft.h> 101 +#include <Imlib2.h> 102 103 #include "arg.h" 104 #include "util.h" 105 @@ -31,11 +37,19 @@ enum { 106 NUMCOLS 107 }; 108 109 +#include "config.h" 110 + 111 struct lock { 112 int screen; 113 Window root, win; 114 Pixmap pmap; 115 + Pixmap bgmap; 116 unsigned long colors[NUMCOLS]; 117 + unsigned int x, y; 118 + unsigned int xoff, yoff, mw, mh; 119 + Drawable drawable; 120 + GC gc; 121 + XRectangle rectangles[LENGTH(rectangles)]; 122 }; 123 124 struct xrandr { 125 @@ -44,7 +58,7 @@ struct xrandr { 126 int errbase; 127 }; 128 129 -#include "config.h" 130 +Imlib_Image image; 131 132 static void 133 die(const char *errstr, ...) 134 @@ -124,6 +138,34 @@ gethash(void) 135 return hash; 136 } 137 138 +static void 139 +resizerectangles(struct lock *lock) 140 +{ 141 + int i; 142 + 143 + for (i = 0; i < LENGTH(rectangles); i++){ 144 + lock->rectangles[i].x = (rectangles[i].x * logosize) 145 + + lock->xoff + ((lock->mw) / 2) - (logow / 2 * logosize); 146 + lock->rectangles[i].y = (rectangles[i].y * logosize) 147 + + lock->yoff + ((lock->mh) / 2) - (logoh / 2 * logosize); 148 + lock->rectangles[i].width = rectangles[i].width * logosize; 149 + lock->rectangles[i].height = rectangles[i].height * logosize; 150 + } 151 +} 152 + 153 +static void 154 +drawlogo(Display *dpy, struct lock *lock, int color) 155 +{ 156 + /* 157 + XSetForeground(dpy, lock->gc, lock->colors[BACKGROUND]); 158 + XFillRectangle(dpy, lock->drawable, lock->gc, 0, 0, lock->x, lock->y); */ 159 + lock->drawable = lock->bgmap; 160 + XSetForeground(dpy, lock->gc, lock->colors[color]); 161 + XFillRectangles(dpy, lock->drawable, lock->gc, lock->rectangles, LENGTH(rectangles)); 162 + XCopyArea(dpy, lock->drawable, lock->win, lock->gc, 0, 0, lock->x, lock->y, 0, 0); 163 + XSync(dpy, False); 164 +} 165 + 166 static void 167 readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 168 const char *hash) 169 @@ -190,10 +232,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 170 color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT); 171 if (running && oldc != color) { 172 for (screen = 0; screen < nscreens; screen++) { 173 - XSetWindowBackground(dpy, 174 - locks[screen]->win, 175 - locks[screen]->colors[color]); 176 - XClearWindow(dpy, locks[screen]->win); 177 + drawlogo(dpy, locks[screen], color); 178 } 179 oldc = color; 180 } 181 @@ -228,6 +267,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 182 XColor color, dummy; 183 XSetWindowAttributes wa; 184 Cursor invisible; 185 +#ifdef XINERAMA 186 + XineramaScreenInfo *info; 187 + int n; 188 +#endif 189 190 if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock)))) 191 return NULL; 192 @@ -235,27 +278,60 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 193 lock->screen = screen; 194 lock->root = RootWindow(dpy, lock->screen); 195 196 + if(image) 197 + { 198 + lock->bgmap = XCreatePixmap(dpy, lock->root, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen), DefaultDepth(dpy, lock->screen)); 199 + imlib_context_set_image(image); 200 + imlib_context_set_display(dpy); 201 + imlib_context_set_visual(DefaultVisual(dpy, lock->screen)); 202 + imlib_context_set_colormap(DefaultColormap(dpy, lock->screen)); 203 + imlib_context_set_drawable(lock->bgmap); 204 + imlib_render_image_on_drawable(0, 0); 205 + imlib_free_image(); 206 + } 207 for (i = 0; i < NUMCOLS; i++) { 208 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), 209 colorname[i], &color, &dummy); 210 lock->colors[i] = color.pixel; 211 } 212 213 + lock->x = DisplayWidth(dpy, lock->screen); 214 + lock->y = DisplayHeight(dpy, lock->screen); 215 +#ifdef XINERAMA 216 + if ((info = XineramaQueryScreens(dpy, &n))) { 217 + lock->xoff = info[0].x_org; 218 + lock->yoff = info[0].y_org; 219 + lock->mw = info[0].width; 220 + lock->mh = info[0].height; 221 + } else 222 +#endif 223 + { 224 + lock->xoff = lock->yoff = 0; 225 + lock->mw = lock->x; 226 + lock->mh = lock->y; 227 + } 228 + lock->drawable = XCreatePixmap(dpy, lock->root, 229 + lock->x, lock->y, DefaultDepth(dpy, screen)); 230 + lock->gc = XCreateGC(dpy, lock->root, 0, NULL); 231 + XSetLineAttributes(dpy, lock->gc, 1, LineSolid, CapButt, JoinMiter); 232 + 233 /* init */ 234 wa.override_redirect = 1; 235 - wa.background_pixel = lock->colors[INIT]; 236 lock->win = XCreateWindow(dpy, lock->root, 0, 0, 237 - DisplayWidth(dpy, lock->screen), 238 - DisplayHeight(dpy, lock->screen), 239 + lock->x, lock->y, 240 0, DefaultDepth(dpy, lock->screen), 241 CopyFromParent, 242 DefaultVisual(dpy, lock->screen), 243 CWOverrideRedirect | CWBackPixel, &wa); 244 + if(lock->bgmap) 245 + XSetWindowBackgroundPixmap(dpy, lock->win, lock->bgmap); 246 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8); 247 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, 248 &color, &color, 0, 0); 249 XDefineCursor(dpy, lock->win, invisible); 250 251 + resizerectangles(lock); 252 + 253 /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */ 254 for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) { 255 if (ptgrab != GrabSuccess) { 256 @@ -276,6 +352,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 257 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask); 258 259 XSelectInput(dpy, lock->root, SubstructureNotifyMask); 260 + drawlogo(dpy, lock, INIT); 261 return lock; 262 } 263 264 @@ -355,6 +432,60 @@ main(int argc, char **argv) { 265 if (setuid(duid) < 0) 266 die("slock: setuid: %s\n", strerror(errno)); 267 268 + /*Create screenshot Image*/ 269 + Screen *scr = ScreenOfDisplay(dpy, DefaultScreen(dpy)); 270 + image = imlib_create_image(scr->width,scr->height); 271 + imlib_context_set_image(image); 272 + imlib_context_set_display(dpy); 273 + imlib_context_set_visual(DefaultVisual(dpy,0)); 274 + imlib_context_set_drawable(RootWindow(dpy,XScreenNumberOfScreen(scr))); 275 + imlib_copy_drawable_to_image(0,0,0,scr->width,scr->height,0,0,1); 276 + 277 +#ifdef BLUR 278 + 279 + /*Blur function*/ 280 + imlib_image_blur(blurRadius); 281 +#endif // BLUR 282 + 283 +#ifdef PIXELATION 284 + /*Pixelation*/ 285 + int width = scr->width; 286 + int height = scr->height; 287 + 288 + for(int y = 0; y < height; y += pixelSize) 289 + { 290 + for(int x = 0; x < width; x += pixelSize) 291 + { 292 + int red = 0; 293 + int green = 0; 294 + int blue = 0; 295 + 296 + Imlib_Color pixel; 297 + Imlib_Color* pp; 298 + pp = &pixel; 299 + for(int j = 0; j < pixelSize && j < height; j++) 300 + { 301 + for(int i = 0; i < pixelSize && i < width; i++) 302 + { 303 + imlib_image_query_pixel(x+i,y+j,pp); 304 + red += pixel.red; 305 + green += pixel.green; 306 + blue += pixel.blue; 307 + } 308 + } 309 + red /= (pixelSize*pixelSize); 310 + green /= (pixelSize*pixelSize); 311 + blue /= (pixelSize*pixelSize); 312 + imlib_context_set_color(red,green,blue,pixel.alpha); 313 + imlib_image_fill_rectangle(x,y,pixelSize,pixelSize); 314 + red = 0; 315 + green = 0; 316 + blue = 0; 317 + } 318 + } 319 + 320 + 321 +#endif 322 /* check for Xrandr support */ 323 rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase); 324 325 @@ -391,5 +522,12 @@ main(int argc, char **argv) { 326 /* everything is now blank. Wait for the correct password */ 327 readpw(dpy, &rr, locks, nscreens, hash); 328 329 + for (nlocks = 0, s = 0; s < nscreens; s++) { 330 + XFreePixmap(dpy, locks[s]->drawable); 331 + XFreeGC(dpy, locks[s]->gc); 332 + } 333 + 334 + XSync(dpy, 0); 335 + XCloseDisplay(dpy); 336 return 0; 337 } 338 -- 339 2.31.1 340