setxbg

X11 background image
git clone https://porkepik.fr/setxbg
Log | Files | Refs | README | LICENSE

commit a61a1523871ac3ab0282615ebef6a0e7a8113338
parent c51b322edd68769a6d83baa0195a143ea88cf370
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Sun,  4 Aug 2019 05:37:48 +0200

fix background not being displayed when using compton

Diffstat:
Msetxbg.c | 46+++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/setxbg.c b/setxbg.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> +#include <X11/Xatom.h> #include <X11/Xlib.h> #include <X11/Xutil.h> @@ -91,6 +92,36 @@ resize(struct image img, int new_w, int new_h) } static void +forget_old_background(Display *disp, Window root) +{ + Atom prop, prop_esetroot, type; + int format; + unsigned long length, after; + unsigned char *data_root = NULL, *data_esetroot = NULL; + + prop = XInternAtom(disp, "_XROOTPMAP_ID", True); + prop_esetroot = XInternAtom(disp, "ESETROOT_PMAP_ID", True); + if (prop == None || prop_esetroot == None) + return; + + XGetWindowProperty(disp, root, prop, 0L, 1L, False, AnyPropertyType, + &type, &format, &length, &after, &data_root); + + if (type == XA_PIXMAP) { + XGetWindowProperty(disp, root, prop_esetroot, 0L, 1L, False, + AnyPropertyType, &type, &format, &length, &after, + &data_esetroot); + if (type == XA_PIXMAP && data_root == data_esetroot) + XKillClient(disp, *data_root); + } + + if (data_root) + XFree(data_root); + if (data_esetroot) + XFree(data_esetroot); +} + +static void apply_background(Display *disp, struct image img, int screen_w, int screen_h) { XImage *ximg; @@ -101,14 +132,21 @@ apply_background(Display *disp, struct image img, int screen_w, int screen_h) XColor color; XGCValues gcval; GC gc; - - ximg = XCreateImage(disp, DefaultVisual(disp, 0), 24, ZPixmap, 0, - (char *)img.data, img.width, img.height, 8, 0); + Atom prop_root; root = DefaultRootWindow(disp); + forget_old_background(disp, root); + depth = DefaultDepth(disp, 0); pixmap = XCreatePixmap(disp, root, screen_w, screen_h, depth); + prop_root = XInternAtom(disp, "_XROOTPMAP_ID", False); + if (prop_root == None) + errx(1, "Creation of _XSETROOT_ID atom failed."); + XChangeProperty(disp, root, prop_root, XA_PIXMAP, 32, PropModeReplace, + (unsigned char *)&pixmap, 1); + XSetCloseDownMode(disp, RetainPermanent); + cmap = DefaultColormap(disp, DefaultScreen(disp)); if (img.bg_color == NULL) XAllocNamedColor(disp, cmap, "black", &color, &color); @@ -118,6 +156,8 @@ apply_background(Display *disp, struct image img, int screen_w, int screen_h) gc = XCreateGC(disp, root, GCForeground, &gcval); XFillRectangle(disp, pixmap, gc, 0, 0, screen_w, screen_h); + ximg = XCreateImage(disp, DefaultVisual(disp, 0), 24, ZPixmap, 0, + (char *)img.data, img.width, img.height, 8, 0); XPutImage(disp, pixmap, DefaultGC(disp, 0), ximg, img.src_x, img.src_y, img.dest_x, img.dest_y, img.width, img.height); XSetWindowBackgroundPixmap(disp, root, pixmap);