void write_window_to_picture(Window xid)
{
Display *dsp = XOpenDisplay(NULL);
char filename[101];
sprintf(filename, "%d.png", (int)xid);
GdkWindow *win_gdk = gdk_x11_window_foreign_new_for_display
(gdk_display_get_default(), xid);
gint w, h;
gdk_drawable_get_size(GDK_DRAWABLE(win_gdk), &w, &h);
XEvent _xevent;
_xevent.xexpose =
(XExposeEvent)
{
.type = Expose,
.serial = 0,
.send_event = True,
.display = dsp,
.window = xid,
.x = 0, .y = 0, .width = w, .height = h,
.count = 0
};
XSendEvent(dsp, xid, False, 0, &_xevent);
GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(
NULL, GDK_DRAWABLE(win_gdk), NULL, 0, 0, 0, 0, w, h);
if(pb != NULL)
{
XCloseDisplay(dsp)
cairo_surface_t *surf_cairo = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w/2, h/2);
cairo_t *cr = cairo_create(surf_cairo);
gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
cairo_paint(cr);
cairo_surface_write_to_png(surf_cairo, filename);
g_print("%s saved successfully!\n", filename);
}
}