[download]

local/bin/xrootdump

   1 //usr/bin/cc -xc $0 -o$0~ -lX11 && exec $0~ "$@"
   2 // https://gist.github.com/ehamberg/767824/b4b4b0d1732d565d96b585723ebcc976cf6ca6a9
   3 // https://www.apriorit.com/dev-blog/672-lin-how-to-take-multi-monitor-screenshots-on-linux
   4 
   5 #include <stdio.h>
   6 #include <stdlib.h>
   7 
   8 #include <X11/Xlib.h>
   9 #include <X11/Xatom.h>
  10 #include <X11/Xutil.h>
  11 
  12 void dumpbmp(XImage*);
  13 
  14 int main(int argc, char *argv[]){
  15     Atom type;
  16     int format;
  17     unsigned long nitems;
  18     unsigned long bytes_after;
  19     unsigned char *prop = NULL;
  20 
  21     Display *dpy = XOpenDisplay(NULL);
  22     int screen = DefaultScreen(dpy);
  23 
  24     XGetWindowProperty(dpy, RootWindow(dpy, screen), XInternAtom(dpy, "_XROOTPMAP_ID", False),
  25     0, 1, False, XA_PIXMAP, &type, &format, &nitems, &bytes_after, &prop);
  26 
  27     Pixmap pixmap = *(Pixmap*)prop;
  28     XFree(prop);
  29 
  30     XImage *img = XGetImage(dpy, pixmap, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen), ~0, ZPixmap);
  31 
  32     dumpbmp(img);
  33     XDestroyImage(img);
  34     XCloseDisplay(dpy);
  35 
  36     return 0;
  37 }
  38 
  39 void dumpbmp(XImage *img){
  40     struct bmpfile { /* {{{ */
  41         short bfType;
  42         int   bfSize;
  43         int   bfReserved;
  44         int   bfOffBits;
  45     } __attribute__((packed)); /* }}} */
  46     struct bmpinfo { /* {{{ */
  47         int   biSize;
  48         int   biWidth;
  49         int   biHeight;
  50         short biPlanes;
  51         short biBitCount;
  52         int   biCompression;
  53         int   biSizeImage;
  54         int   biXPelsPerMeter;
  55         int   biYPelsPerMeter;
  56         int   biClrUsed;
  57         int   biClrImportant;
  58     } __attribute__((packed)); /* }}} */
  59 
  60     struct bmpfile bf = { 0 };
  61     struct bmpinfo bi = { 0 };
  62 
  63     bf.bfType = 0x4d42;
  64     bf.bfOffBits = sizeof(bf) + sizeof(bi);
  65     bf.bfSize = bf.bfOffBits + img->height * img->bytes_per_line;
  66 
  67     bi.biSize   = sizeof(bi);
  68     bi.biWidth  = img->width;
  69     bi.biHeight = -img->height;
  70     bi.biPlanes = 1;
  71     bi.biBitCount  = img->bits_per_pixel;
  72 
  73     fwrite(&bf, 1, sizeof(bf), stdout);
  74     fwrite(&bi, 1, sizeof(bi), stdout);
  75     fwrite(img->data, 1, img->height * img->bytes_per_line, stdout);
  76 }
  77 
  78 // vim: ft=c