abg

Animated background for X11
git clone git://git.laack.co/abg.git
Log | Files | Refs | README | LICENSE

background.cpp (1409B)


      1 #include <X11/Xatom.h>
      2 #include <X11/Xlib.h>
      3 #include <unistd.h>
      4 
      5 #include <cstddef>
      6 #include <cstdint>
      7 #include <stdexcept>
      8 #include <string>
      9 #include <vector>
     10 
     11 std::vector<uint32_t> getScreenSize() {
     12     Display* dpy;
     13     Screen* screen;
     14     dpy = XOpenDisplay(NULL);
     15     int count = ScreenCount(dpy);
     16     if (count == 0) {
     17         throw std::runtime_error{"Unable to find screens."};
     18     }
     19     screen = ScreenOfDisplay(dpy, 0);
     20     std::vector<uint32_t> res{(uint32_t)screen->width,
     21                               (uint32_t)screen->height};
     22     XCloseDisplay(dpy);
     23     return res;
     24 }
     25 
     26 void sendToBg(std::string name) {
     27     Display* d = XOpenDisplay(nullptr);
     28     Window root = DefaultRootWindow(d), r, p, *kids;
     29     uint32_t n;
     30 
     31     XQueryTree(d, root, &r, &p, &kids, &n);
     32     for (unsigned i = 0; i < n; i++) {
     33         char* wn = nullptr;
     34         XFetchName(d, kids[i], &wn);
     35 
     36         if (wn == nullptr) {
     37             continue;
     38         }
     39 
     40         bool hit = name == wn;
     41         XFree(wn);
     42 
     43         if (!hit) {
     44             continue;
     45         }
     46 
     47         XSetWindowAttributes a;
     48 
     49         a.override_redirect = True;
     50         XChangeWindowAttributes(d, kids[i], CWOverrideRedirect, &a);
     51 
     52         // have to do unmap / map to make it bg for all tags
     53         XUnmapWindow(d, kids[i]);
     54         XMapWindow(d, kids[i]);
     55 
     56         XLowerWindow(d, kids[i]);
     57         break;
     58     }
     59 
     60     XFree(kids);
     61     XCloseDisplay(d);
     62 }