From 726bf58428f937b9ef40684f2a5f38c590ce738b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 15 Aug 2017 19:17:40 +0200 Subject: Handle multiscreen properly, make background persistent --- src/x11util.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/x11util.cpp (limited to 'src/x11util.cpp') diff --git a/src/x11util.cpp b/src/x11util.cpp new file mode 100644 index 0000000..f56d31e --- /dev/null +++ b/src/x11util.cpp @@ -0,0 +1,62 @@ +#include "x11util.h" +extern "C" { + #include + #include +} +#include +#include +#include + +static int eHandler(Display* dpy, XErrorEvent* e) +{ + char buf[1000]; + XGetErrorText(dpy, e->error_code, buf, 1000); + fprintf(stderr, "X ERROR %d: %s\n", (int)e->error_code, buf); + return 0; +} + +extern "C" +void AddPixmapToBackground(unsigned const char* imgData, int width, int height, int depth, int bytesPerLine, int byteCount) +{ + Pixmap pix = 0; + GC gc = NULL; + XImage *xi = NULL; + XGCValues gc_init; + memset(&gc_init, 0, sizeof(gc_init)); + Display* dpy = XOpenDisplay(NULL); + if (dpy == NULL) + return; + XSetErrorHandler(&eHandler); + int screen = DefaultScreen(dpy); + Window root = RootWindow(dpy, screen); + char *data = (char*)malloc(byteCount); + memcpy(data, imgData, byteCount); + xi = XCreateImage(dpy, CopyFromParent, depth, ZPixmap, 0, data, width, height, 32, bytesPerLine); + if (xi == NULL) + goto cleanup; + pix = XCreatePixmap(dpy, root, width, height, (unsigned int)DefaultDepth(dpy, screen)); + if (pix == 0) + goto cleanup; + gc_init.foreground = BlackPixel(dpy, screen); + gc_init.background = WhitePixel(dpy, screen); + gc = XCreateGC(dpy, pix, GCForeground|GCBackground, &gc_init); + if (gc == NULL) + goto cleanup; + int res1, res2, res3; + res1 = XPutImage(dpy, pix, gc, xi, 0, 0, 0, 0, width, height); + res2 = XSetWindowBackgroundPixmap(dpy, root, pix); + res3 = XClearWindow(dpy, root); +cleanup: + if (gc != NULL) { + XFreeGC(dpy, gc); + } + if (pix != 0) { + XFreePixmap(dpy, pix); + } + if (xi != NULL) { + XDestroyImage(xi); + } + if (dpy != NULL) { + XCloseDisplay(dpy); + } +} -- cgit v1.2.3-55-g7522