2
* test-clipboard.c -- do clipboard things
4
* Copyright (C) 2005 Michael Natterer <mitch@gimp.org>
6
* Use this code for whatever you like.
15
#include <sys/types.h>
23
#include "libgimpbase/gimpversion.h"
26
typedef struct _CopyData CopyData;
30
const gchar *filename;
36
static void test_clipboard_show_version (void) G_GNUC_NORETURN;
37
static gboolean test_clipboard_parse_selection (const gchar *option_name,
41
static gboolean test_clipboard_list_targets (GtkClipboard *clipboard);
42
static gboolean test_clipboard_copy (GtkClipboard *clipboard,
44
const gchar *filename);
45
static gboolean test_clipboard_store (GtkClipboard *clipboard,
47
const gchar *filename);
48
static gboolean test_clipboard_paste (GtkClipboard *clipboard,
50
const gchar *filename);
51
static void test_clipboard_copy_callback (GtkClipboard *clipboard,
52
GtkSelectionData *selection,
57
static GdkAtom option_selection_type = GDK_SELECTION_CLIPBOARD;
58
static gboolean option_list_targets = FALSE;
59
static gchar *option_target = NULL;
60
static gchar *option_copy_filename = NULL;
61
static gchar *option_store_filename = NULL;
62
static gchar *option_paste_filename = NULL;
64
static const GOptionEntry main_entries[] =
67
"selection-type", 's', 0,
68
G_OPTION_ARG_CALLBACK, test_clipboard_parse_selection,
69
"Selection type (primary|secondary|clipboard)", "<type>"
72
"list-targets", 'l', 0,
73
G_OPTION_ARG_NONE, &option_list_targets,
74
"List the targets offered by the clipboard", NULL
78
G_OPTION_ARG_STRING, &option_target,
79
"The target format to copy or paste", "<target>"
83
G_OPTION_ARG_STRING, &option_copy_filename,
84
"Copy <file> to clipboard", "<file>"
88
G_OPTION_ARG_STRING, &option_store_filename,
89
"Store <file> in the clipboard manager", "<file>"
93
G_OPTION_ARG_STRING, &option_paste_filename,
94
"Paste clipoard into <file>", "<file>"
97
"version", 'v', G_OPTION_FLAG_NO_ARG,
98
G_OPTION_ARG_CALLBACK, test_clipboard_show_version,
99
"Show version information and exit", NULL
109
GOptionContext *context;
110
GtkClipboard *clipboard;
111
GError *error = NULL;
113
context = g_option_context_new (NULL);
114
g_option_context_add_main_entries (context, main_entries, NULL);
115
g_option_context_add_group (context, gtk_get_option_group (TRUE));
117
if (! g_option_context_parse (context, &argc, &argv, &error))
121
g_printerr ("%s\n", error->message);
122
g_error_free (error);
127
"Could not initialize the graphical user interface.\n"
128
"Make sure a proper setup for your display environment "
135
gtk_init (&argc, &argv);
137
clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
138
option_selection_type);
141
g_error ("gtk_clipboard_get_for_display");
143
if (option_list_targets)
145
if (! test_clipboard_list_targets (clipboard))
151
if ((option_copy_filename && option_paste_filename) ||
152
(option_copy_filename && option_store_filename) ||
153
(option_paste_filename && option_store_filename))
155
g_printerr ("Can't perform two operations at the same time\n");
159
if (option_copy_filename)
163
g_printerr ("Usage: %s -t <target> -c <file>\n", argv[0]);
167
if (! test_clipboard_copy (clipboard, option_target,
168
option_copy_filename))
172
if (option_store_filename)
176
g_printerr ("Usage: %s -t <target> -S <file>\n", argv[0]);
180
if (! test_clipboard_store (clipboard, option_target,
181
option_store_filename))
185
if (option_paste_filename)
189
g_printerr ("Usage: %s -t <target> -p <file>\n", argv[0]);
193
if (! test_clipboard_paste (clipboard, option_target,
194
option_paste_filename))
202
test_clipboard_show_version (void)
204
g_print ("test-clipboard (GIMP clipboard testbed) version %s\n",
211
test_clipboard_parse_selection (const gchar *option_name,
216
if (! strcmp (value, "primary"))
217
option_selection_type = GDK_SELECTION_PRIMARY;
218
else if (! strcmp (value, "secondary"))
219
option_selection_type = GDK_SELECTION_SECONDARY;
220
else if (! strcmp (value, "clipboard"))
221
option_selection_type = GDK_SELECTION_CLIPBOARD;
229
test_clipboard_list_targets (GtkClipboard *clipboard)
231
GtkSelectionData *data;
233
data = gtk_clipboard_wait_for_contents (clipboard,
234
gdk_atom_intern ("TARGETS",
242
success = gtk_selection_data_get_targets (data, &targets, &n_targets);
244
gtk_selection_data_free (data);
250
for (i = 0; i < n_targets; i++)
251
g_print ("%s\n", gdk_atom_name (targets[i]));
261
test_clipboard_copy (GtkClipboard *clipboard,
263
const gchar *filename)
265
GtkTargetEntry entry;
268
entry.target = g_strdup (target);
272
data.filename = filename;
273
data.file_copied = FALSE;
276
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
277
test_clipboard_copy_callback,
281
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
288
if (! data.file_copied)
292
g_printerr ("%s: copying failed: %s\n",
293
g_get_prgname (), data.error->message);
294
g_error_free (data.error);
298
g_printerr ("%s: copying failed\n",
309
test_clipboard_store (GtkClipboard *clipboard,
311
const gchar *filename)
313
GtkTargetEntry entry;
316
entry.target = g_strdup (target);
320
data.filename = filename;
321
data.file_copied = FALSE;
324
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
325
test_clipboard_copy_callback,
329
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
334
gtk_clipboard_set_can_store (clipboard, &entry, 1);
335
gtk_clipboard_store (clipboard);
337
if (! data.file_copied)
341
g_printerr ("%s: storing failed: %s\n",
342
g_get_prgname (), data.error->message);
343
g_error_free (data.error);
347
g_printerr ("%s: could not contact clipboard manager\n",
358
test_clipboard_paste (GtkClipboard *clipboard,
360
const gchar *filename)
362
GtkSelectionData *data;
364
data = gtk_clipboard_wait_for_contents (clipboard,
365
gdk_atom_intern (target,
372
fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
376
g_printerr ("%s: open() filed: %s",
377
g_get_prgname (), g_strerror (errno));
381
bytes = data->length * data->format / 8;
383
if (write (fd, data->data, bytes) < bytes)
386
g_printerr ("%s: write() failed: %s",
387
g_get_prgname (), g_strerror (errno));
393
g_printerr ("%s: close() failed: %s",
394
g_get_prgname (), g_strerror (errno));
398
gtk_selection_data_free (data);
405
test_clipboard_copy_callback (GtkClipboard *clipboard,
406
GtkSelectionData *selection,
410
CopyData *copy_data = data;
414
if (! g_file_get_contents (copy_data->filename, &buf, &buf_size,
417
if (! option_store_filename)
423
gtk_selection_data_set (selection, selection->target,
424
8, (guchar *) buf, buf_size);
428
copy_data->file_copied = TRUE;
430
g_print ("%s: data transfer in progress, hit <ctrl>+c when pasted...",