slock-background-image-20220318-1c5a538.diff 5.3 KB

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