#include <gtk/gtk.h>

static GtkTargetEntry targets[] = {
    { "UTF8_STRING", 0, 0 },
    { "TEXT", 0, 0 },
    { "text/plain", 0, 0 }
};

static void cb_clear    (GtkClipboard *clipboard,
                         gpointer data);
static void cb_get      (GtkClipboard *clipboard,
                         GtkSelectionData *selection_data,
                         guint info,
                         gpointer data);
static void cb_grab     (void);


static gboolean
cb_grab_in_timeout (gpointer data)
{
    cb_grab ();
    return FALSE;
}


static void
cb_grab (void)
{
    GtkClipboard *cb;
    cb = gtk_clipboard_get (GDK_SELECTION_PRIMARY);

    if (!gtk_clipboard_set_with_data (cb, targets, G_N_ELEMENTS (targets),
                                      cb_get, cb_clear, NULL))
        g_timeout_add (10, cb_grab_in_timeout, NULL);
}


static void
cb_clear (GtkClipboard *clipboard,
          gpointer data)
{
    cb_grab ();
}


static void
cb_get (GtkClipboard *clipboard,
        GtkSelectionData *selection_data,
        guint info,
        gpointer data)
{
    gtk_selection_data_set_text (selection_data, "", 0);
}


int main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    cb_grab ();
    gtk_main ();
    return 0;
}
