2005-05-18 06:42:50 +08:00
|
|
|
/*
|
|
|
|
* test-clipboard.c -- do clipboard things
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* Use this code for whatever you like.
|
|
|
|
*/
|
|
|
|
|
2005-05-18 08:56:22 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2005-05-18 06:42:50 +08:00
|
|
|
#include <stdlib.h>
|
2005-05-22 10:33:13 +08:00
|
|
|
#include <string.h>
|
2005-05-18 08:56:22 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-05-18 06:42:50 +08:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2005-08-22 22:08:49 +08:00
|
|
|
#include "libgimpbase/gimpversion.h"
|
|
|
|
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
typedef struct _CopyData CopyData;
|
|
|
|
|
|
|
|
struct _CopyData
|
|
|
|
{
|
|
|
|
const gchar *filename;
|
|
|
|
gboolean file_copied;
|
|
|
|
GError *error;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-08-22 22:08:49 +08:00
|
|
|
static void test_clipboard_show_version (void) G_GNUC_NORETURN;
|
2005-05-22 10:33:13 +08:00
|
|
|
static gboolean test_clipboard_parse_selection (const gchar *option_name,
|
|
|
|
const gchar *value,
|
|
|
|
gpointer data,
|
|
|
|
GError **error);
|
2005-05-25 06:06:52 +08:00
|
|
|
static gboolean test_clipboard_list_targets (GtkClipboard *clipboard);
|
|
|
|
static gboolean test_clipboard_copy (GtkClipboard *clipboard,
|
2005-05-22 10:33:13 +08:00
|
|
|
const gchar *target,
|
|
|
|
const gchar *filename);
|
2005-05-25 06:06:52 +08:00
|
|
|
static gboolean test_clipboard_store (GtkClipboard *clipboard,
|
|
|
|
const gchar *target,
|
|
|
|
const gchar *filename);
|
|
|
|
static gboolean test_clipboard_paste (GtkClipboard *clipboard,
|
2005-05-22 10:33:13 +08:00
|
|
|
const gchar *target,
|
|
|
|
const gchar *filename);
|
|
|
|
static void test_clipboard_copy_callback (GtkClipboard *clipboard,
|
|
|
|
GtkSelectionData *selection,
|
|
|
|
guint info,
|
|
|
|
gpointer data);
|
|
|
|
|
|
|
|
|
|
|
|
static GdkAtom option_selection_type = GDK_SELECTION_CLIPBOARD;
|
|
|
|
static gboolean option_list_targets = FALSE;
|
|
|
|
static gchar *option_target = NULL;
|
|
|
|
static gchar *option_copy_filename = NULL;
|
2005-05-25 06:06:52 +08:00
|
|
|
static gchar *option_store_filename = NULL;
|
2005-05-22 10:33:13 +08:00
|
|
|
static gchar *option_paste_filename = NULL;
|
|
|
|
|
|
|
|
static const GOptionEntry main_entries[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"selection-type", 's', 0,
|
|
|
|
G_OPTION_ARG_CALLBACK, test_clipboard_parse_selection,
|
2005-09-26 18:01:22 +08:00
|
|
|
"Selection type (primary|secondary|clipboard)", "<type>"
|
2005-05-22 10:33:13 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"list-targets", 'l', 0,
|
|
|
|
G_OPTION_ARG_NONE, &option_list_targets,
|
|
|
|
"List the targets offered by the clipboard", NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"target", 't', 0,
|
|
|
|
G_OPTION_ARG_STRING, &option_target,
|
|
|
|
"The target format to copy or paste", "<target>"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"copy", 'c', 0,
|
|
|
|
G_OPTION_ARG_STRING, &option_copy_filename,
|
|
|
|
"Copy <file> to clipboard", "<file>"
|
|
|
|
},
|
2005-05-25 06:06:52 +08:00
|
|
|
{
|
|
|
|
"store", 'S', 0,
|
|
|
|
G_OPTION_ARG_STRING, &option_store_filename,
|
|
|
|
"Store <file> in the clipboard manager", "<file>"
|
|
|
|
},
|
2005-05-22 10:33:13 +08:00
|
|
|
{
|
|
|
|
"paste", 'p', 0,
|
|
|
|
G_OPTION_ARG_STRING, &option_paste_filename,
|
2008-10-09 02:45:01 +08:00
|
|
|
"Paste clipoard into <file> ('-' pastes to STDOUT)", "<file>"
|
2005-05-22 10:33:13 +08:00
|
|
|
},
|
2005-08-22 22:08:49 +08:00
|
|
|
{
|
2005-09-26 18:01:22 +08:00
|
|
|
"version", 'v', G_OPTION_FLAG_NO_ARG,
|
2005-08-22 22:08:49 +08:00
|
|
|
G_OPTION_ARG_CALLBACK, test_clipboard_show_version,
|
|
|
|
"Show version information and exit", NULL
|
|
|
|
},
|
2005-05-22 10:33:13 +08:00
|
|
|
{ NULL }
|
|
|
|
};
|
2005-05-18 06:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
gint
|
|
|
|
main (gint argc,
|
|
|
|
gchar *argv[])
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
|
|
|
GtkClipboard *clipboard;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
context = g_option_context_new (NULL);
|
|
|
|
g_option_context_add_main_entries (context, main_entries, NULL);
|
|
|
|
g_option_context_add_group (context, gtk_get_option_group (TRUE));
|
|
|
|
|
|
|
|
if (! g_option_context_parse (context, &argc, &argv, &error))
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print ("%s\n",
|
|
|
|
"Could not initialize the graphical user interface.\n"
|
|
|
|
"Make sure a proper setup for your display environment "
|
|
|
|
"exists.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
|
2005-05-22 10:33:13 +08:00
|
|
|
option_selection_type);
|
2005-05-18 06:42:50 +08:00
|
|
|
|
|
|
|
if (! clipboard)
|
|
|
|
g_error ("gtk_clipboard_get_for_display");
|
|
|
|
|
2005-05-22 10:33:13 +08:00
|
|
|
if (option_list_targets)
|
2005-05-18 06:42:50 +08:00
|
|
|
{
|
2005-05-25 06:06:52 +08:00
|
|
|
if (! test_clipboard_list_targets (clipboard))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2005-05-18 06:42:50 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
if ((option_copy_filename && option_paste_filename) ||
|
|
|
|
(option_copy_filename && option_store_filename) ||
|
|
|
|
(option_paste_filename && option_store_filename))
|
2005-05-19 06:34:15 +08:00
|
|
|
{
|
2005-05-25 06:06:52 +08:00
|
|
|
g_printerr ("Can't perform two operations at the same time\n");
|
2005-05-19 06:34:15 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2005-05-22 10:33:13 +08:00
|
|
|
if (option_copy_filename)
|
2005-05-18 06:42:50 +08:00
|
|
|
{
|
2005-05-22 10:33:13 +08:00
|
|
|
if (! option_target)
|
2005-05-19 06:34:15 +08:00
|
|
|
{
|
|
|
|
g_printerr ("Usage: %s -t <target> -c <file>\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
if (! test_clipboard_copy (clipboard, option_target,
|
|
|
|
option_copy_filename))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option_store_filename)
|
|
|
|
{
|
|
|
|
if (! option_target)
|
|
|
|
{
|
|
|
|
g_printerr ("Usage: %s -t <target> -S <file>\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! test_clipboard_store (clipboard, option_target,
|
|
|
|
option_store_filename))
|
|
|
|
return EXIT_FAILURE;
|
2005-05-18 06:42:50 +08:00
|
|
|
}
|
|
|
|
|
2005-05-22 10:33:13 +08:00
|
|
|
if (option_paste_filename)
|
2005-05-18 06:42:50 +08:00
|
|
|
{
|
2005-05-22 10:33:13 +08:00
|
|
|
if (! option_target)
|
2005-05-19 06:34:15 +08:00
|
|
|
{
|
|
|
|
g_printerr ("Usage: %s -t <target> -p <file>\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
if (! test_clipboard_paste (clipboard, option_target,
|
|
|
|
option_paste_filename))
|
|
|
|
return EXIT_FAILURE;
|
2005-05-18 06:42:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-08-22 22:08:49 +08:00
|
|
|
static void
|
|
|
|
test_clipboard_show_version (void)
|
|
|
|
{
|
|
|
|
g_print ("test-clipboard (GIMP clipboard testbed) version %s\n",
|
|
|
|
GIMP_VERSION);
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2005-05-22 10:33:13 +08:00
|
|
|
static gboolean
|
|
|
|
test_clipboard_parse_selection (const gchar *option_name,
|
|
|
|
const gchar *value,
|
|
|
|
gpointer data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
if (! strcmp (value, "primary"))
|
|
|
|
option_selection_type = GDK_SELECTION_PRIMARY;
|
|
|
|
else if (! strcmp (value, "secondary"))
|
|
|
|
option_selection_type = GDK_SELECTION_SECONDARY;
|
|
|
|
else if (! strcmp (value, "clipboard"))
|
|
|
|
option_selection_type = GDK_SELECTION_CLIPBOARD;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
static gboolean
|
2005-05-18 06:42:50 +08:00
|
|
|
test_clipboard_list_targets (GtkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
GtkSelectionData *data;
|
|
|
|
|
|
|
|
data = gtk_clipboard_wait_for_contents (clipboard,
|
|
|
|
gdk_atom_intern ("TARGETS",
|
|
|
|
FALSE));
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
GdkAtom *targets;
|
|
|
|
gint n_targets;
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
success = gtk_selection_data_get_targets (data, &targets, &n_targets);
|
|
|
|
|
|
|
|
gtk_selection_data_free (data);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_targets; i++)
|
|
|
|
g_print ("%s\n", gdk_atom_name (targets[i]));
|
|
|
|
|
|
|
|
g_free (targets);
|
|
|
|
}
|
|
|
|
}
|
2005-05-25 06:06:52 +08:00
|
|
|
|
|
|
|
return TRUE;
|
2005-05-18 06:42:50 +08:00
|
|
|
}
|
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
static gboolean
|
2005-05-18 06:42:50 +08:00
|
|
|
test_clipboard_copy (GtkClipboard *clipboard,
|
2005-05-18 08:56:22 +08:00
|
|
|
const gchar *target,
|
2005-05-18 06:42:50 +08:00
|
|
|
const gchar *filename)
|
|
|
|
{
|
2005-05-18 08:56:22 +08:00
|
|
|
GtkTargetEntry entry;
|
2005-05-25 06:06:52 +08:00
|
|
|
CopyData data;
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-18 08:56:22 +08:00
|
|
|
entry.target = g_strdup (target);
|
|
|
|
entry.flags = 0;
|
|
|
|
entry.info = 1;
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
data.filename = filename;
|
|
|
|
data.file_copied = FALSE;
|
|
|
|
data.error = NULL;
|
|
|
|
|
2005-05-18 08:56:22 +08:00
|
|
|
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
|
2005-05-18 06:42:50 +08:00
|
|
|
test_clipboard_copy_callback,
|
|
|
|
NULL,
|
2005-05-25 06:06:52 +08:00
|
|
|
&data))
|
|
|
|
{
|
|
|
|
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
|
|
|
|
g_get_prgname());
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-18 06:42:50 +08:00
|
|
|
|
|
|
|
gtk_main ();
|
2005-05-25 06:06:52 +08:00
|
|
|
|
|
|
|
if (! data.file_copied)
|
|
|
|
{
|
|
|
|
if (data.error)
|
|
|
|
{
|
|
|
|
g_printerr ("%s: copying failed: %s\n",
|
|
|
|
g_get_prgname (), data.error->message);
|
|
|
|
g_error_free (data.error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_printerr ("%s: copying failed\n",
|
|
|
|
g_get_prgname ());
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2005-05-18 06:42:50 +08:00
|
|
|
}
|
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
static gboolean
|
|
|
|
test_clipboard_store (GtkClipboard *clipboard,
|
|
|
|
const gchar *target,
|
|
|
|
const gchar *filename)
|
|
|
|
{
|
|
|
|
GtkTargetEntry entry;
|
|
|
|
CopyData data;
|
|
|
|
|
|
|
|
entry.target = g_strdup (target);
|
|
|
|
entry.flags = 0;
|
|
|
|
entry.info = 1;
|
|
|
|
|
|
|
|
data.filename = filename;
|
|
|
|
data.file_copied = FALSE;
|
|
|
|
data.error = NULL;
|
|
|
|
|
|
|
|
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
|
|
|
|
test_clipboard_copy_callback,
|
|
|
|
NULL,
|
|
|
|
&data))
|
|
|
|
{
|
|
|
|
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
|
|
|
|
g_get_prgname ());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_clipboard_set_can_store (clipboard, &entry, 1);
|
|
|
|
gtk_clipboard_store (clipboard);
|
|
|
|
|
|
|
|
if (! data.file_copied)
|
|
|
|
{
|
|
|
|
if (data.error)
|
|
|
|
{
|
|
|
|
g_printerr ("%s: storing failed: %s\n",
|
|
|
|
g_get_prgname (), data.error->message);
|
|
|
|
g_error_free (data.error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_printerr ("%s: could not contact clipboard manager\n",
|
|
|
|
g_get_prgname ());
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2005-05-18 08:56:22 +08:00
|
|
|
test_clipboard_paste (GtkClipboard *clipboard,
|
2005-09-25 00:17:45 +08:00
|
|
|
const gchar *target,
|
|
|
|
const gchar *filename)
|
2005-05-18 08:56:22 +08:00
|
|
|
{
|
2009-03-22 23:49:13 +08:00
|
|
|
GtkSelectionData *sel_data;
|
2005-05-18 08:56:22 +08:00
|
|
|
|
2009-03-22 23:49:13 +08:00
|
|
|
sel_data = gtk_clipboard_wait_for_contents (clipboard,
|
|
|
|
gdk_atom_intern (target,
|
|
|
|
FALSE));
|
|
|
|
if (sel_data)
|
2005-05-18 08:56:22 +08:00
|
|
|
{
|
2009-03-22 23:49:13 +08:00
|
|
|
const guchar *data;
|
|
|
|
gint length;
|
|
|
|
gint fd;
|
2005-05-18 08:56:22 +08:00
|
|
|
|
2008-10-09 02:43:14 +08:00
|
|
|
if (! strcmp (filename, "-"))
|
|
|
|
fd = 1;
|
|
|
|
else
|
2009-06-16 01:28:06 +08:00
|
|
|
fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2005-05-18 08:56:22 +08:00
|
|
|
|
|
|
|
if (fd < 0)
|
2005-05-25 06:06:52 +08:00
|
|
|
{
|
|
|
|
g_printerr ("%s: open() filed: %s",
|
|
|
|
g_get_prgname (), g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-18 08:56:22 +08:00
|
|
|
|
2009-03-22 23:49:13 +08:00
|
|
|
data = gtk_selection_data_get_data (sel_data);
|
|
|
|
length = gtk_selection_data_get_length (sel_data);
|
|
|
|
|
|
|
|
if (write (fd, data, length) < length)
|
2005-05-25 06:06:52 +08:00
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
g_printerr ("%s: write() failed: %s",
|
|
|
|
g_get_prgname (), g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-18 08:56:22 +08:00
|
|
|
|
|
|
|
if (close (fd) < 0)
|
2005-05-25 06:06:52 +08:00
|
|
|
{
|
|
|
|
g_printerr ("%s: close() failed: %s",
|
|
|
|
g_get_prgname (), g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-05-18 08:56:22 +08:00
|
|
|
|
2009-03-22 23:49:13 +08:00
|
|
|
gtk_selection_data_free (sel_data);
|
2005-05-18 08:56:22 +08:00
|
|
|
}
|
2005-05-25 06:06:52 +08:00
|
|
|
|
|
|
|
return TRUE;
|
2005-05-18 08:56:22 +08:00
|
|
|
}
|
|
|
|
|
2005-05-18 06:42:50 +08:00
|
|
|
static void
|
|
|
|
test_clipboard_copy_callback (GtkClipboard *clipboard,
|
|
|
|
GtkSelectionData *selection,
|
|
|
|
guint info,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2005-05-25 06:06:52 +08:00
|
|
|
CopyData *copy_data = data;
|
|
|
|
gchar *buf;
|
|
|
|
gsize buf_size;
|
2005-05-18 06:42:50 +08:00
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
if (! g_file_get_contents (copy_data->filename, &buf, &buf_size,
|
|
|
|
©_data->error))
|
2005-06-16 06:59:45 +08:00
|
|
|
{
|
|
|
|
if (! option_store_filename)
|
|
|
|
gtk_main_quit ();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2005-05-18 08:56:22 +08:00
|
|
|
|
2009-03-22 23:49:13 +08:00
|
|
|
gtk_selection_data_set (selection,
|
|
|
|
gtk_selection_data_get_target (selection),
|
2005-05-18 06:42:50 +08:00
|
|
|
8, (guchar *) buf, buf_size);
|
|
|
|
|
|
|
|
g_free (buf);
|
|
|
|
|
2005-05-25 06:06:52 +08:00
|
|
|
copy_data->file_copied = TRUE;
|
2005-09-02 22:04:38 +08:00
|
|
|
|
|
|
|
g_print ("%s: data transfer in progress, hit <ctrl>+c when pasted...",
|
|
|
|
G_STRFUNC);
|
2005-05-18 06:42:50 +08:00
|
|
|
}
|