background.cpp (2012B)
1 #include <X11/Xatom.h> 2 #include <X11/Xlib.h> 3 #include <X11/extensions/Xrandr.h> 4 #include <unistd.h> 5 6 #include <cstddef> 7 #include <cstdint> 8 #include <stdexcept> 9 #include <string> 10 #include <vector> 11 12 std::vector<uint32_t> getScreenSizeAndOffsets() { 13 Display* dpy; 14 dpy = XOpenDisplay(NULL); 15 16 if (dpy == NULL) { 17 throw std::runtime_error{"Unable to open display."}; 18 } 19 20 int count = ScreenCount(dpy); 21 22 if (count == 0) { 23 throw std::runtime_error{"Unable to find screens."}; 24 } 25 26 XRRMonitorInfo* mons = 27 XRRGetMonitors(dpy, DefaultRootWindow(dpy), True, &count); 28 29 if (count == 0) { 30 throw std::runtime_error{"Unable to find monitors."}; 31 } 32 33 XRRMonitorInfo* m = &mons[0]; 34 for (int i = 0; i < count; ++i) { 35 if (mons[i].primary) { 36 m = &mons[i]; 37 break; 38 } 39 } 40 41 std::vector<uint32_t> res{(uint32_t)m->width, (uint32_t)m->height, 42 (uint32_t)m->x, (uint32_t)m->y}; 43 44 XRRFreeMonitors(mons); 45 XCloseDisplay(dpy); 46 return res; 47 } 48 49 void sendToBg(std::string name, uint32_t xOffset, uint32_t yOffset) { 50 Display* d = XOpenDisplay(nullptr); 51 Window root = DefaultRootWindow(d), r, p, *kids; 52 uint32_t n; 53 54 XQueryTree(d, root, &r, &p, &kids, &n); 55 for (unsigned i = 0; i < n; i++) { 56 char* wn = nullptr; 57 XFetchName(d, kids[i], &wn); 58 59 if (wn == nullptr) { 60 continue; 61 } 62 63 bool hit = name == wn; 64 XFree(wn); 65 66 if (!hit) { 67 continue; 68 } 69 70 XSetWindowAttributes a; 71 72 a.override_redirect = True; 73 XChangeWindowAttributes(d, kids[i], CWOverrideRedirect, &a); 74 75 // have to do unmap / map to make it bg for all tags 76 XUnmapWindow(d, kids[i]); 77 XMoveWindow(d, kids[i], xOffset, yOffset); 78 XLowerWindow(d, kids[i]); 79 XMapWindow(d, kids[i]); 80 81 XLowerWindow(d, kids[i]); 82 break; 83 } 84 85 XFree(kids); 86 XCloseDisplay(d); 87 }