1
votes

In short, I want to write a Gnome-Shell-style window switcher. So I need to fetch snapshots of all the windows. My current program looks like this:

char filename[101];
sprintf(filename, "%d.png", (int)win_list[i]);
GdkWindow *win_gdk = gdk_x11_window_foreign_new_for_display
     (gdk_display_get_default(), win_list[i]);
gint _w, _h;
gdk_drawable_get_size(GDK_DRAWABLE(win_gdk), &_w, &_h);
XEvent _xevent;
_xevent.xexpose =
     (XExposeEvent)
     {
          .type = Expose,
          .send_event = True,
          .display = xsu_vars.dpy,
          .window = win_list[i],
          .x = 0, .y = 0, .width = _w, .height = _h,
          .count = 0
     };
XSendEvent(xsu_vars.dpy, win_list[i], 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) {
     cairo_surface_t *_surf_cairo = cairo_image_surface_create(
          CAIRO_FORMAT_RGB24, _w, _h);
     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);
     printf("%s saved successfully!\n", filename);
} else {
     printf("failed...\n");
}

The program works well well, but it will not generate correct images for those windows which are on a different desktop of minimized -- they would look like this: enter image description here

Note that I send a expose event to all windows before generating pixbufs of them.

UPDATE: It seems that xlib doesn't support that. So the only way may be creating cache manually.

2

2 Answers

0
votes

Yes, your update is correct. When a window is unmapped (or covered up), X just discards its contents; they don't exist anywhere in order to be snapshotted.

I believe libwnck contains code to do this and other parts of writing a switcher, btw. It's basically a library for writing things like window switchers.