#include <stdio.h>
#include <gtk/gtk.h>

gboolean take_screenshot(gpointer data);

int main(int argc, char **argv) {

     gtk_init(&argc, &argv);

     GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_widget_show_all(win);

     /* Take a screenshot. */
     g_timeout_add(1000, (GSourceFunc)take_screenshot, NULL);

     gtk_main();
     return 0;
}

gboolean take_screenshot(gpointer data) {

     GdkScreen *screen = gdk_screen_get_default();
     GdkWindow *root_win = gdk_screen_get_root_window(screen);

     int screen_width = gdk_screen_get_width(screen);
     int screen_height = gdk_screen_get_height(screen);

     /* ... */
     GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(root_win),
						  NULL, 0, 0,
						  0, 0,
						  gdk_screen_get_width(screen),
						  gdk_screen_get_height(screen));

     gchar *buffer = NULL;
     gint buffer_size = 0;

     /* Save the pixbuf to a buffer. */
     gdk_pixbuf_save_to_bufferv(pb, &buffer, &buffer_size,
				"bmp", NULL, NULL, NULL);

     /* Dump the buffer to a file. */
     FILE *fp = fopen("screenshot.bmp", "wb");
     fwrite(buffer, sizeof(char), buffer_size, fp);
     fclose(fp);

     g_free(buffer);
     g_object_unref(pb);

     /* Quit. */
     exit(0);
     return FALSE;
}
