title: Get Pixmap out of Xlib
body: <p>I've been trying to find a way of grabbing video directly from an X window and got pointed to Xlib and the compositing extension.</p>
  
  <p>So far, I've been able to listen to change events and grab a Pixmap with this code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;X11/Intrinsic.h&gt;
  #include &lt;X11/extensions/Xcomposite.h&gt;
  #include &lt;X11/extensions/Xdamage.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main(int argc, char ** argv) {
      int br, base;
      Window win = 0x3400003;
      Display *dsp = XOpenDisplay(0);
      XCompositeQueryExtension(dsp, &amp;base, &amp;br);
      XDamageQueryExtension(dsp,&amp;base, &amp;br);
  
  
      cout &lt;&lt; base &lt;&lt; endl;
  
      Damage damage = XDamageCreate(dsp, win, XDamageReportRawRectangles);
      XCompositeRedirectWindow(dsp, win, CompositeRedirectAutomatic);
  
      XEvent ev;
      while(true) {
          XNextEvent(dsp, &amp;ev);
          if(ev.type == base + XDamageNotify) {
              Pixmap pm = XCompositeNameWindowPixmap(dsp, win);
              XWindowAttributes attr;
              XGetWindowAttributes(dsp, win, &amp;attr);
              XWriteBitmapFile(dsp, "test.bmp", pm, attr.width, attr.height, -1, -1);
              return 0;
          }
      }
  
      XCompositeUnredirectWindow(dsp, win, CompositeRedirectAutomatic);
      XDamageDestroy(dsp, damage);
      return 0;
  }
  </code></pre>
  
  <p>The problem here is that the resulting bmp (created by XWriteBitmapFile) is black&amp;white horribleness. Disregarding the fact that I don't want to write the data to file anyway, I am apparently doing something wrong in reading it.
  I would love to convert the Pixmap to either a framebuffer in opengl or a binary blob in ram.</p>
  
  <p>Thanks in advance for any help you can give me on this.</p>
  
  <p>Best regards.</p>