2017-11-19 17:06:13 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkclipboardprivate.h"
|
|
|
|
#include "gdkclipboard-x11.h"
|
2017-11-23 00:59:19 +00:00
|
|
|
|
|
|
|
#include "gdkintl.h"
|
2018-02-07 05:00:01 +00:00
|
|
|
#include "gdkdisplay-x11.h"
|
2017-11-19 17:06:13 +00:00
|
|
|
#include "gdkprivate-x11.h"
|
2017-11-23 00:59:19 +00:00
|
|
|
#include "gdkselectioninputstream-x11.h"
|
2017-11-25 20:59:39 +00:00
|
|
|
#include "gdkselectionoutputstream-x11.h"
|
2017-11-23 00:59:19 +00:00
|
|
|
#include "gdktextlistconverter-x11.h"
|
2017-11-25 20:59:39 +00:00
|
|
|
#include "gdk/gdk-private.h"
|
2017-11-19 17:06:13 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_XFIXES
|
|
|
|
#include <X11/extensions/Xfixes.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define IDLE_ABORT_TIME 30 /* seconds */
|
|
|
|
|
|
|
|
typedef struct _GdkX11ClipboardClass GdkX11ClipboardClass;
|
|
|
|
|
|
|
|
typedef struct _RetrievalInfo RetrievalInfo;
|
|
|
|
|
|
|
|
struct _GdkX11Clipboard
|
|
|
|
{
|
|
|
|
GdkClipboard parent;
|
|
|
|
|
|
|
|
char *selection;
|
|
|
|
Atom xselection;
|
2017-11-25 20:59:39 +00:00
|
|
|
gulong timestamp;
|
2017-12-01 05:20:28 +00:00
|
|
|
|
|
|
|
GTask *store_task;
|
2017-11-19 17:06:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GdkX11ClipboardClass
|
|
|
|
{
|
|
|
|
GdkClipboardClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GdkX11Clipboard, gdk_x11_clipboard, GDK_TYPE_CLIPBOARD)
|
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
static void
|
|
|
|
print_atoms (GdkX11Clipboard *cb,
|
|
|
|
const char *prefix,
|
|
|
|
const Atom *atoms,
|
|
|
|
gsize n_atoms)
|
|
|
|
{
|
2020-01-18 07:34:25 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)), CLIPBOARD, {
|
2018-01-12 00:48:27 +00:00
|
|
|
gsize i;
|
2020-01-18 07:34:25 +00:00
|
|
|
GdkDisplay *display = gdk_clipboard_get_display (GDK_CLIPBOARD (cb));
|
2018-01-12 00:48:27 +00:00
|
|
|
|
|
|
|
g_printerr ("%s: %s [ ", cb->selection, prefix);
|
|
|
|
for (i = 0; i < n_atoms; i++)
|
|
|
|
g_printerr ("%s%s", i > 0 ? ", " : "", gdk_x11_get_xatom_name_for_display (display , atoms[i]));
|
|
|
|
g_printerr (" ]\n");
|
2020-01-18 07:34:25 +00:00
|
|
|
});
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_default_output_done (GObject *clipboard,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!gdk_clipboard_write_finish (GDK_CLIPBOARD (clipboard), result, &error))
|
|
|
|
{
|
2020-01-18 07:34:25 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (GDK_CLIPBOARD (clipboard)), CLIPBOARD,
|
2018-01-12 00:48:27 +00:00
|
|
|
g_printerr ("%s: failed to write stream: %s\n",
|
|
|
|
GDK_X11_CLIPBOARD (clipboard)->selection, error->message));
|
2017-11-25 20:59:39 +00:00
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-12 14:25:34 +00:00
|
|
|
gdk_x11_clipboard_default_output_handler (GOutputStream *stream,
|
|
|
|
const char *mime_type,
|
|
|
|
gpointer user_data)
|
2017-11-25 20:59:39 +00:00
|
|
|
{
|
2017-12-12 14:25:34 +00:00
|
|
|
gdk_clipboard_write_async (GDK_CLIPBOARD (user_data),
|
|
|
|
mime_type,
|
2017-11-25 20:59:39 +00:00
|
|
|
stream,
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
gdk_x11_clipboard_default_output_done,
|
|
|
|
NULL);
|
|
|
|
g_object_unref (stream);
|
|
|
|
}
|
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
static GInputStream *
|
|
|
|
text_list_convert (GdkX11Clipboard *cb,
|
|
|
|
GInputStream *stream,
|
|
|
|
const char *encoding,
|
|
|
|
int format)
|
|
|
|
{
|
|
|
|
GInputStream *converter_stream;
|
|
|
|
GConverter *converter;
|
|
|
|
|
|
|
|
converter = gdk_x11_text_list_converter_to_utf8_new (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)),
|
|
|
|
encoding,
|
|
|
|
format);
|
|
|
|
converter_stream = g_converter_input_stream_new (stream, converter);
|
|
|
|
|
|
|
|
g_object_unref (converter);
|
|
|
|
g_object_unref (stream);
|
|
|
|
|
|
|
|
return converter_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GInputStream *
|
|
|
|
no_convert (GdkX11Clipboard *cb,
|
|
|
|
GInputStream *stream,
|
|
|
|
const char *encoding,
|
|
|
|
int format)
|
|
|
|
{
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *x_target;
|
|
|
|
const char *mime_type;
|
|
|
|
GInputStream * (* convert) (GdkX11Clipboard *, GInputStream *, const char *, int);
|
2017-11-25 20:59:39 +00:00
|
|
|
const char *type;
|
|
|
|
gint format;
|
2017-11-23 00:59:19 +00:00
|
|
|
} special_targets[] = {
|
2017-12-12 14:25:34 +00:00
|
|
|
{ "UTF8_STRING", "text/plain;charset=utf-8", no_convert, "UTF8_STRING", 8 },
|
|
|
|
{ "COMPOUND_TEXT", "text/plain;charset=utf-8", text_list_convert, "COMPOUND_TEXT", 8 },
|
|
|
|
{ "TEXT", "text/plain;charset=utf-8", text_list_convert, "STRING", 8 },
|
|
|
|
{ "STRING", "text/plain;charset=utf-8", text_list_convert, "STRING", 8 },
|
|
|
|
{ "TARGETS", NULL, NULL, "ATOM", 32 },
|
|
|
|
{ "TIMESTAMP", NULL, NULL, "INTEGER", 32 },
|
|
|
|
{ "SAVE_TARGETS", NULL, NULL, "NULL", 32 }
|
2017-11-23 00:59:19 +00:00
|
|
|
};
|
|
|
|
|
2017-12-10 00:05:37 +00:00
|
|
|
GSList *
|
2017-11-23 00:59:19 +00:00
|
|
|
gdk_x11_clipboard_formats_to_targets (GdkContentFormats *formats)
|
|
|
|
{
|
|
|
|
GSList *targets;
|
|
|
|
const char * const *mime_types;
|
|
|
|
gsize i, j, n_mime_types;
|
|
|
|
|
|
|
|
targets = NULL;
|
|
|
|
mime_types = gdk_content_formats_get_mime_types (formats, &n_mime_types);
|
|
|
|
|
|
|
|
for (i = 0; i < n_mime_types; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < G_N_ELEMENTS (special_targets); j++)
|
|
|
|
{
|
2017-11-25 20:59:39 +00:00
|
|
|
if (special_targets[j].mime_type == NULL)
|
|
|
|
continue;
|
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
if (g_str_equal (mime_types[i], special_targets[j].mime_type))
|
2017-11-25 20:59:39 +00:00
|
|
|
targets = g_slist_prepend (targets, (gpointer) g_intern_string (special_targets[j].x_target));
|
2017-11-23 00:59:19 +00:00
|
|
|
}
|
|
|
|
targets = g_slist_prepend (targets, (gpointer) mime_types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_slist_reverse (targets);
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:25:34 +00:00
|
|
|
Atom *
|
2017-11-25 20:59:39 +00:00
|
|
|
gdk_x11_clipboard_formats_to_atoms (GdkDisplay *display,
|
2017-12-01 05:20:28 +00:00
|
|
|
gboolean include_special,
|
2017-11-25 20:59:39 +00:00
|
|
|
GdkContentFormats *formats,
|
|
|
|
gsize *n_atoms)
|
|
|
|
{
|
|
|
|
GSList *l, *targets;
|
|
|
|
Atom *atoms;
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
targets = gdk_x11_clipboard_formats_to_targets (formats);
|
|
|
|
|
2017-12-01 05:20:28 +00:00
|
|
|
if (include_special)
|
2017-11-25 20:59:39 +00:00
|
|
|
{
|
2017-12-01 05:20:28 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (special_targets); i++)
|
|
|
|
{
|
|
|
|
if (special_targets[i].mime_type != NULL)
|
|
|
|
continue;
|
2017-11-25 20:59:39 +00:00
|
|
|
|
2017-12-12 14:25:34 +00:00
|
|
|
targets = g_slist_prepend (targets, (gpointer) g_intern_string (special_targets[i].x_target));
|
2017-12-01 05:20:28 +00:00
|
|
|
}
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*n_atoms = g_slist_length (targets);
|
|
|
|
atoms = g_new (Atom, *n_atoms);
|
|
|
|
i = 0;
|
|
|
|
for (l = targets; l; l = l->next)
|
|
|
|
atoms[i++] = gdk_x11_get_xatom_by_name_for_display (display, l->data);
|
|
|
|
|
|
|
|
return atoms;
|
|
|
|
}
|
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
static GdkContentFormats *
|
|
|
|
gdk_x11_clipboard_formats_from_atoms (GdkDisplay *display,
|
|
|
|
const Atom *atoms,
|
|
|
|
gsize n_atoms)
|
|
|
|
{
|
|
|
|
GdkContentFormatsBuilder *builder;
|
|
|
|
gsize i, j;
|
|
|
|
|
|
|
|
builder = gdk_content_formats_builder_new ();
|
|
|
|
for (i = 0; i < n_atoms; i++)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = gdk_x11_get_xatom_name_for_display (display , atoms[i]);
|
|
|
|
if (strchr (name, '/'))
|
|
|
|
{
|
|
|
|
gdk_content_formats_builder_add_mime_type (builder, name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < G_N_ELEMENTS (special_targets); j++)
|
|
|
|
{
|
|
|
|
if (g_str_equal (name, special_targets[j].x_target))
|
|
|
|
{
|
2017-11-25 20:59:39 +00:00
|
|
|
if (special_targets[j].mime_type)
|
|
|
|
gdk_content_formats_builder_add_mime_type (builder, special_targets[j].mime_type);
|
2017-11-23 00:59:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Allow binding GdkContentFormatsBuilder
GdkContentFormatsBuilder is currently not introspectable, as it does not
have a GType. We can turn it into a boxed type, but we need to implement
memory management for it.
The current gdk_content_formats_builder_free() function returns a newly
constructed value, so we cannot use it as a GBoxedFreeFunc; additionally
copying a GdkContentFormatsBuilder contents would make it a bit odd, as
you could get multiple identical GdkContentFormats out of the copies.
A simple approach is to model the GdkContentFormatsBuilder API to follow
the GBytes one: use reference counting for memory management, and have
a function to release a reference, return a GdkContentFormats, and reset
the GdkContentFormatsBuilder state.
For language bindings, we can provide a get_formats() function that
returns the GdkContentFormats instance and resets the builder instance,
leaving the reference count untouched.
For C convenience we can keep gdk_content_formats_builder_free(), and
make it a wrapper around gdk_content_formats_builder_get_formats(), with
the guarantee that it'll free the builder instance regardless of its
current reference count.
https://bugzilla.gnome.org/show_bug.cgi?id=793097
https://blogs.gnome.org/otte/2018/02/03/builders/
2018-02-01 16:43:15 +00:00
|
|
|
return gdk_content_formats_builder_free_to_formats (builder);
|
2017-11-23 00:59:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:06:13 +00:00
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_request_targets_finish (GObject *source_object,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GInputStream *stream = G_INPUT_STREAM (source_object);
|
|
|
|
GdkX11Clipboard *cb = user_data;
|
|
|
|
GdkDisplay *display;
|
|
|
|
GdkContentFormats *formats;
|
|
|
|
GBytes *bytes;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2019-01-22 03:30:47 +00:00
|
|
|
display = gdk_clipboard_get_display (GDK_CLIPBOARD (cb));
|
|
|
|
|
2017-11-19 17:06:13 +00:00
|
|
|
bytes = g_input_stream_read_bytes_finish (stream, res, &error);
|
2017-11-22 08:25:35 +00:00
|
|
|
if (bytes == NULL)
|
2017-11-19 17:06:13 +00:00
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: error reading TARGETS: %s\n", cb->selection, error->message));
|
2017-11-22 08:25:35 +00:00
|
|
|
g_error_free (error);
|
|
|
|
g_object_unref (stream);
|
|
|
|
g_object_unref (cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (g_bytes_get_size (bytes) == 0)
|
|
|
|
{
|
|
|
|
g_bytes_unref (bytes);
|
2017-11-19 17:06:13 +00:00
|
|
|
g_object_unref (stream);
|
|
|
|
g_object_unref (cb);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-01 06:42:36 +00:00
|
|
|
else if (gdk_clipboard_is_local (GDK_CLIPBOARD (cb)))
|
|
|
|
{
|
|
|
|
/* FIXME: Use a cancellable for this request, so that we don't do he brittle
|
|
|
|
* is_local() check */
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
g_object_unref (stream);
|
|
|
|
g_object_unref (cb);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-19 17:06:13 +00:00
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
print_atoms (cb,
|
|
|
|
"received targets",
|
|
|
|
g_bytes_get_data (bytes, NULL),
|
|
|
|
g_bytes_get_size (bytes) / sizeof (Atom));
|
2017-11-19 17:06:13 +00:00
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
formats = gdk_x11_clipboard_formats_from_atoms (display,
|
|
|
|
g_bytes_get_data (bytes, NULL),
|
|
|
|
g_bytes_get_size (bytes) / sizeof (Atom));
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, char *s = gdk_content_formats_to_string (formats); g_printerr ("%s: got formats: %s\n", cb->selection, s); g_free (s));
|
2017-11-19 17:06:13 +00:00
|
|
|
|
2017-11-19 18:18:22 +00:00
|
|
|
/* union with previously loaded formats */
|
2017-12-01 06:42:36 +00:00
|
|
|
formats = gdk_content_formats_union (formats, gdk_clipboard_get_formats (GDK_CLIPBOARD (cb)));
|
2017-11-19 18:18:22 +00:00
|
|
|
gdk_clipboard_claim_remote (GDK_CLIPBOARD (cb), formats);
|
|
|
|
gdk_content_formats_unref (formats);
|
2017-12-01 06:42:36 +00:00
|
|
|
g_bytes_unref (bytes);
|
2017-11-19 18:18:22 +00:00
|
|
|
|
2017-11-19 17:06:13 +00:00
|
|
|
g_input_stream_read_bytes_async (stream,
|
2017-11-25 00:25:31 +00:00
|
|
|
gdk_x11_display_get_max_request_size (display),
|
2017-11-19 17:06:13 +00:00
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
gdk_x11_clipboard_request_targets_finish,
|
|
|
|
cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-11-22 08:25:35 +00:00
|
|
|
gdk_x11_clipboard_request_targets_got_stream (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
2017-11-19 17:06:13 +00:00
|
|
|
{
|
2017-11-22 08:25:35 +00:00
|
|
|
GdkX11Clipboard *cb = data;
|
2017-11-19 17:06:13 +00:00
|
|
|
GInputStream *stream;
|
|
|
|
GdkDisplay *display;
|
2017-11-22 08:25:35 +00:00
|
|
|
GError *error = NULL;
|
2017-11-23 00:59:19 +00:00
|
|
|
const char *type;
|
|
|
|
int format;
|
2017-11-22 08:25:35 +00:00
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
display = gdk_clipboard_get_display (GDK_CLIPBOARD (cb));
|
2017-11-23 00:59:19 +00:00
|
|
|
stream = gdk_x11_selection_input_stream_new_finish (result, &type, &format, &error);
|
2017-11-22 08:25:35 +00:00
|
|
|
if (stream == NULL)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: can't request TARGETS: %s\n", cb->selection, error->message));
|
2017-11-22 08:25:35 +00:00
|
|
|
g_object_unref (cb);
|
2017-11-25 20:59:39 +00:00
|
|
|
g_error_free (error);
|
2017-11-22 08:25:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-23 00:59:19 +00:00
|
|
|
else if (!g_str_equal (type, "ATOM") || format != 32)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: Wrong reply type to TARGETS: type %s != ATOM or format %d != 32\n",
|
2017-11-25 20:59:39 +00:00
|
|
|
cb->selection, type, format));
|
2017-11-23 00:59:19 +00:00
|
|
|
g_input_stream_close (stream, NULL, NULL);
|
|
|
|
g_object_unref (stream);
|
|
|
|
g_object_unref (cb);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-19 17:06:13 +00:00
|
|
|
|
|
|
|
g_input_stream_read_bytes_async (stream,
|
2017-11-25 00:25:31 +00:00
|
|
|
gdk_x11_display_get_max_request_size (display),
|
2017-11-19 17:06:13 +00:00
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
gdk_x11_clipboard_request_targets_finish,
|
2017-11-22 08:25:35 +00:00
|
|
|
cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_request_targets (GdkX11Clipboard *cb)
|
|
|
|
{
|
|
|
|
gdk_x11_selection_input_stream_new_async (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)),
|
|
|
|
cb->selection,
|
|
|
|
"TARGETS",
|
|
|
|
cb->timestamp,
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
gdk_x11_clipboard_request_targets_got_stream,
|
|
|
|
g_object_ref (cb));
|
2017-11-19 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_claim_remote (GdkX11Clipboard *cb,
|
|
|
|
guint32 timestamp)
|
|
|
|
{
|
|
|
|
GdkContentFormats *empty;
|
|
|
|
|
|
|
|
empty = gdk_content_formats_new (NULL, 0);
|
|
|
|
gdk_clipboard_claim_remote (GDK_CLIPBOARD (cb), empty);
|
|
|
|
gdk_content_formats_unref (empty);
|
|
|
|
cb->timestamp = timestamp;
|
|
|
|
gdk_x11_clipboard_request_targets (cb);
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:53:17 +00:00
|
|
|
static gboolean
|
|
|
|
gdk_x11_clipboard_xevent (GdkDisplay *display,
|
|
|
|
const XEvent *xevent,
|
|
|
|
gpointer data)
|
2017-11-19 18:52:07 +00:00
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (data);
|
|
|
|
Window xwindow;
|
|
|
|
|
|
|
|
xwindow = GDK_X11_DISPLAY (display)->leader_window;
|
|
|
|
|
|
|
|
if (xevent->xany.window != xwindow)
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-19 18:52:07 +00:00
|
|
|
|
|
|
|
switch (xevent->type)
|
|
|
|
{
|
2017-11-25 20:59:39 +00:00
|
|
|
case SelectionClear:
|
|
|
|
if (xevent->xselectionclear.selection != cb->xselection)
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-25 20:59:39 +00:00
|
|
|
|
|
|
|
if (xevent->xselectionclear.time < cb->timestamp)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: ignoring SelectionClear with too old timestamp (%lu vs %lu)\n",
|
|
|
|
cb->selection, xevent->xselectionclear.time, cb->timestamp));
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: got SelectionClear\n", cb->selection));
|
2017-11-25 20:59:39 +00:00
|
|
|
gdk_x11_clipboard_claim_remote (cb, xevent->xselectionclear.time);
|
2017-12-13 00:53:17 +00:00
|
|
|
return TRUE;
|
2017-11-25 20:59:39 +00:00
|
|
|
|
2017-12-01 05:20:28 +00:00
|
|
|
case SelectionNotify:
|
|
|
|
/* This code only checks clipboard manager replies, so... */
|
|
|
|
if (!g_str_equal (cb->selection, "CLIPBOARD"))
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-12-01 05:20:28 +00:00
|
|
|
|
|
|
|
/* selection is not for us */
|
|
|
|
if (xevent->xselection.selection != gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER") ||
|
|
|
|
xevent->xselection.target != gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS"))
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-12-01 05:20:28 +00:00
|
|
|
|
|
|
|
/* We already received a selectionNotify before */
|
|
|
|
if (cb->store_task == NULL)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: got SelectionNotify for nonexisting task?!\n", cb->selection));
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-12-01 05:20:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: got SelectionNotify for store task\n", cb->selection));
|
2017-12-01 05:20:28 +00:00
|
|
|
|
|
|
|
if (xevent->xselection.property != None)
|
|
|
|
g_task_return_boolean (cb->store_task, TRUE);
|
|
|
|
else
|
|
|
|
g_task_return_new_error (cb->store_task, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
_("Clipboard manager could not store selection."));
|
|
|
|
g_clear_object (&cb->store_task);
|
|
|
|
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-12-01 05:20:28 +00:00
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
case SelectionRequest:
|
|
|
|
{
|
2020-01-18 07:34:25 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
const char *target, *property;
|
|
|
|
#endif
|
2017-11-25 20:59:39 +00:00
|
|
|
|
|
|
|
if (xevent->xselectionrequest.selection != cb->xselection)
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-25 20:59:39 +00:00
|
|
|
|
2020-01-18 07:34:25 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2017-11-25 20:59:39 +00:00
|
|
|
target = gdk_x11_get_xatom_name_for_display (display, xevent->xselectionrequest.target);
|
|
|
|
if (xevent->xselectionrequest.property == None)
|
|
|
|
property = target;
|
|
|
|
else
|
|
|
|
property = gdk_x11_get_xatom_name_for_display (display, xevent->xselectionrequest.property);
|
2020-01-18 07:34:25 +00:00
|
|
|
#endif
|
2017-11-25 20:59:39 +00:00
|
|
|
|
|
|
|
if (!gdk_clipboard_is_local (GDK_CLIPBOARD (cb)))
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: got SelectionRequest for %s @ %s even though we don't own the selection, huh?\n",
|
|
|
|
cb->selection, target, property));
|
2017-12-13 00:53:17 +00:00
|
|
|
return TRUE;
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
if (xevent->xselectionrequest.requestor == None)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: got SelectionRequest for %s @ %s with NULL window, ignoring\n",
|
|
|
|
cb->selection, target, property));
|
2017-12-13 00:53:17 +00:00
|
|
|
return TRUE;
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: got SelectionRequest for %s @ %s\n", cb->selection, target, property));
|
2017-12-01 01:53:47 +00:00
|
|
|
|
2017-12-12 14:25:34 +00:00
|
|
|
gdk_x11_selection_output_streams_create (display,
|
|
|
|
gdk_clipboard_get_formats (GDK_CLIPBOARD (cb)),
|
|
|
|
xevent->xselectionrequest.requestor,
|
|
|
|
xevent->xselectionrequest.selection,
|
|
|
|
xevent->xselectionrequest.target,
|
|
|
|
xevent->xselectionrequest.property ? xevent->xselectionrequest.property
|
|
|
|
: xevent->xselectionrequest.target,
|
|
|
|
xevent->xselectionrequest.time,
|
|
|
|
gdk_x11_clipboard_default_output_handler,
|
|
|
|
cb);
|
2017-12-13 00:53:17 +00:00
|
|
|
return TRUE;
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:52:07 +00:00
|
|
|
default:
|
|
|
|
#ifdef HAVE_XFIXES
|
|
|
|
if (xevent->type - GDK_X11_DISPLAY (display)->xfixes_event_base == XFixesSelectionNotify)
|
2017-11-25 20:59:39 +00:00
|
|
|
{
|
|
|
|
XFixesSelectionNotifyEvent *sn = (XFixesSelectionNotifyEvent *) xevent;
|
2017-11-19 18:52:07 +00:00
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
if (sn->selection != cb->xselection)
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-25 20:59:39 +00:00
|
|
|
|
2018-07-21 09:17:57 +00:00
|
|
|
if (sn->selection_timestamp < cb->timestamp)
|
|
|
|
{
|
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: Ignoring XFixesSelectionNotify with too old timestamp (%lu vs %lu)\n",
|
|
|
|
cb->selection, sn->selection_timestamp, cb->timestamp));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
if (sn->owner == GDK_X11_DISPLAY (display)->leader_window)
|
2017-11-19 18:52:07 +00:00
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: Ignoring XFixesSelectionNotify for ourselves\n", cb->selection));
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-19 18:52:07 +00:00
|
|
|
}
|
2017-11-25 20:59:39 +00:00
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: Received XFixesSelectionNotify, claiming selection\n", cb->selection));
|
2017-11-25 20:59:39 +00:00
|
|
|
|
|
|
|
gdk_x11_clipboard_claim_remote (cb, sn->selection_timestamp);
|
2017-11-19 18:52:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-12-13 00:53:17 +00:00
|
|
|
return FALSE;
|
2017-11-19 18:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (object);
|
|
|
|
|
2017-12-12 23:56:05 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)),
|
2017-12-13 00:53:17 +00:00
|
|
|
gdk_x11_clipboard_xevent,
|
2017-12-12 23:56:05 +00:00
|
|
|
cb);
|
2017-11-19 18:52:07 +00:00
|
|
|
g_free (cb->selection);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gdk_x11_clipboard_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
static gboolean
|
|
|
|
gdk_x11_clipboard_claim (GdkClipboard *clipboard,
|
|
|
|
GdkContentFormats *formats,
|
|
|
|
gboolean local,
|
|
|
|
GdkContentProvider *content)
|
|
|
|
{
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (clipboard);
|
|
|
|
GdkDisplay *display = gdk_clipboard_get_display (GDK_CLIPBOARD (cb));
|
|
|
|
Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
|
|
|
|
Window xwindow = GDK_X11_DISPLAY (display)->leader_window;
|
|
|
|
guint32 time;
|
|
|
|
|
|
|
|
time = gdk_display_get_last_seen_time (display);
|
|
|
|
|
|
|
|
if (content)
|
|
|
|
{
|
|
|
|
XSetSelectionOwner (xdisplay, cb->xselection, xwindow, time);
|
|
|
|
|
|
|
|
if (XGetSelectionOwner (xdisplay, cb->xselection) != xwindow)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: failed XSetSelectionOwner()\n", cb->selection));
|
2017-11-25 20:59:39 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XSetSelectionOwner (xdisplay, cb->xselection, None, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->timestamp = time;
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: claimed via XSetSelectionOwner()\n", cb->selection));
|
2017-11-25 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GDK_CLIPBOARD_CLASS (gdk_x11_clipboard_parent_class)->claim (clipboard, formats, local, content);
|
|
|
|
}
|
|
|
|
|
2017-12-01 05:20:28 +00:00
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_store_async (GdkClipboard *clipboard,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (clipboard);
|
|
|
|
GdkDisplay *display = gdk_clipboard_get_display (clipboard);
|
|
|
|
Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
|
|
|
|
Atom clipboard_manager, save_targets, property_name;
|
|
|
|
GdkContentProvider *content;
|
|
|
|
GdkContentFormats *formats;
|
|
|
|
Atom *atoms;
|
|
|
|
gsize n_atoms;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* clipboard managers don't work on anythig but the clipbpoard selection */
|
|
|
|
if (!g_str_equal (cb->selection, "CLIPBOARD"))
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: can only store on CLIPBOARD\n", cb->selection));
|
2017-12-01 05:20:28 +00:00
|
|
|
GDK_CLIPBOARD_CLASS (gdk_x11_clipboard_parent_class)->store_async (clipboard,
|
|
|
|
io_priority,
|
|
|
|
cancellable,
|
|
|
|
callback,
|
|
|
|
user_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->store_task = g_task_new (clipboard, cancellable, callback, user_data);
|
|
|
|
g_task_set_priority (cb->store_task, io_priority);
|
|
|
|
g_task_set_source_tag (cb->store_task, gdk_x11_clipboard_store_async);
|
|
|
|
|
|
|
|
clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
|
|
|
|
save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");
|
|
|
|
|
|
|
|
if (XGetSelectionOwner (xdisplay, clipboard_manager) == None)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: XGetSelectionOwner (CLIPBOARD_MANAGER) returned None, aborting.\n",
|
|
|
|
cb->selection));
|
2017-12-01 05:20:28 +00:00
|
|
|
g_task_return_new_error (cb->store_task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
_("Cannot store clipboard. No clipboard manager is active."));
|
|
|
|
g_clear_object (&cb->store_task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
content = gdk_clipboard_get_content (clipboard);
|
|
|
|
if (content == NULL)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD, g_printerr ("%s: storing empty clipboard: SUCCESS!\n", cb->selection));
|
2017-12-01 05:20:28 +00:00
|
|
|
g_task_return_boolean (cb->store_task, TRUE);
|
|
|
|
g_clear_object (&cb->store_task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
formats = gdk_content_provider_ref_storable_formats (content);
|
|
|
|
formats = gdk_content_formats_union_serialize_mime_types (formats);
|
|
|
|
atoms = gdk_x11_clipboard_formats_to_atoms (display, FALSE, formats, &n_atoms);
|
|
|
|
print_atoms (cb, "requesting store from clipboard manager", atoms, n_atoms);
|
|
|
|
gdk_content_formats_unref (formats);
|
|
|
|
|
|
|
|
gdk_x11_display_error_trap_push (display);
|
|
|
|
|
|
|
|
if (n_atoms > 0)
|
|
|
|
{
|
|
|
|
property_name = gdk_x11_get_xatom_by_name_for_display (display, "GDK_CLIPBOARD_SAVE_TARGETS");
|
|
|
|
|
|
|
|
XChangeProperty (xdisplay, GDK_X11_DISPLAY (display)->leader_window,
|
|
|
|
property_name, XA_ATOM,
|
|
|
|
32, PropModeReplace, (guchar *)atoms, n_atoms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
property_name = None;
|
|
|
|
|
|
|
|
XConvertSelection (xdisplay,
|
|
|
|
clipboard_manager, save_targets, property_name,
|
|
|
|
GDK_X11_DISPLAY (display)->leader_window, cb->timestamp);
|
|
|
|
|
|
|
|
error = gdk_x11_display_error_trap_pop (display);
|
|
|
|
if (error != Success)
|
|
|
|
{
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (display, CLIPBOARD,
|
|
|
|
g_printerr ("%s: X error during ConvertSelection() while storing selection: %d\n", cb->selection, error));
|
2017-12-01 05:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gdk_x11_clipboard_store_finish (GdkClipboard *clipboard,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, clipboard), FALSE);
|
|
|
|
g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == gdk_x11_clipboard_store_async, FALSE);
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
|
|
|
}
|
|
|
|
|
2017-11-22 08:25:35 +00:00
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_read_got_stream (GObject *source,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GTask *task = data;
|
|
|
|
GError *error = NULL;
|
|
|
|
GInputStream *stream;
|
2017-11-23 00:59:19 +00:00
|
|
|
const char *type;
|
|
|
|
int format;
|
2017-11-22 08:25:35 +00:00
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
stream = gdk_x11_selection_input_stream_new_finish (res, &type, &format, &error);
|
2017-11-22 08:25:35 +00:00
|
|
|
if (stream == NULL)
|
2017-11-23 00:59:19 +00:00
|
|
|
{
|
|
|
|
GSList *targets, *next;
|
|
|
|
|
|
|
|
targets = g_task_get_task_data (task);
|
|
|
|
next = targets->next;
|
|
|
|
if (next)
|
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (g_task_get_source_object (task));
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (GDK_CLIPBOARD(cb)), CLIPBOARD,
|
|
|
|
g_printerr ("%s: reading %s failed, trying %s next\n",
|
|
|
|
cb->selection, (char *) targets->data, (char *) next->data));
|
2017-11-23 00:59:19 +00:00
|
|
|
targets->next = NULL;
|
|
|
|
g_task_set_task_data (task, next, (GDestroyNotify) g_slist_free);
|
|
|
|
gdk_x11_selection_input_stream_new_async (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)),
|
|
|
|
cb->selection,
|
|
|
|
next->data,
|
|
|
|
cb->timestamp,
|
|
|
|
g_task_get_priority (task),
|
|
|
|
g_task_get_cancellable (task),
|
|
|
|
gdk_x11_clipboard_read_got_stream,
|
|
|
|
task);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
}
|
2017-11-22 08:25:35 +00:00
|
|
|
else
|
2017-11-23 00:59:19 +00:00
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (g_task_get_source_object (task));
|
|
|
|
const char *mime_type = ((GSList *) g_task_get_task_data (task))->data;
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (special_targets); i++)
|
|
|
|
{
|
|
|
|
if (g_str_equal (mime_type, special_targets[i].x_target))
|
|
|
|
{
|
2017-11-25 20:59:39 +00:00
|
|
|
g_assert (special_targets[i].mime_type != NULL);
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)), CLIPBOARD,
|
|
|
|
g_printerr ("%s: reading with converter from %s to %s\n",
|
|
|
|
cb->selection, mime_type, special_targets[i].mime_type));
|
2017-11-23 00:59:19 +00:00
|
|
|
mime_type = g_intern_string (special_targets[i].mime_type);
|
|
|
|
g_task_set_task_data (task, g_slist_prepend (NULL, (gpointer) mime_type), (GDestroyNotify) g_slist_free);
|
|
|
|
stream = special_targets[i].convert (cb, stream, type, format);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)), CLIPBOARD,
|
|
|
|
g_printerr ("%s: reading clipboard as %s now\n", cb->selection, mime_type));
|
2017-11-23 00:59:19 +00:00
|
|
|
g_task_return_pointer (task, stream, g_object_unref);
|
|
|
|
}
|
2017-11-22 08:25:35 +00:00
|
|
|
|
|
|
|
g_object_unref (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_read_async (GdkClipboard *clipboard,
|
|
|
|
GdkContentFormats *formats,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2017-11-20 01:06:21 +00:00
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb = GDK_X11_CLIPBOARD (clipboard);
|
2017-11-23 00:59:19 +00:00
|
|
|
GSList *targets;
|
2017-11-22 08:25:35 +00:00
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
task = g_task_new (clipboard, cancellable, callback, user_data);
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
g_task_set_source_tag (task, gdk_x11_clipboard_read_async);
|
|
|
|
|
2017-11-23 00:59:19 +00:00
|
|
|
targets = gdk_x11_clipboard_formats_to_targets (formats);
|
|
|
|
g_task_set_task_data (task, targets, (GDestroyNotify) g_slist_free);
|
|
|
|
if (targets == NULL)
|
|
|
|
{
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
_("No compatible transfer format found"));
|
|
|
|
return;
|
|
|
|
}
|
2017-11-22 08:25:35 +00:00
|
|
|
|
2018-01-12 00:48:27 +00:00
|
|
|
GDK_DISPLAY_NOTE (gdk_clipboard_get_display (clipboard), CLIPBOARD,
|
|
|
|
g_printerr ("%s: new read for %s (%u other options)\n",
|
|
|
|
cb->selection, (char *) targets->data, g_slist_length (targets->next)));
|
2017-11-22 08:25:35 +00:00
|
|
|
gdk_x11_selection_input_stream_new_async (gdk_clipboard_get_display (GDK_CLIPBOARD (cb)),
|
|
|
|
cb->selection,
|
2017-11-23 00:59:19 +00:00
|
|
|
targets->data,
|
2017-11-22 08:25:35 +00:00
|
|
|
cb->timestamp,
|
|
|
|
io_priority,
|
|
|
|
cancellable,
|
|
|
|
gdk_x11_clipboard_read_got_stream,
|
|
|
|
task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GInputStream *
|
|
|
|
gdk_x11_clipboard_read_finish (GdkClipboard *clipboard,
|
|
|
|
GAsyncResult *result,
|
2019-04-02 11:37:54 +00:00
|
|
|
const char **out_mime_type,
|
2017-11-22 08:25:35 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, G_OBJECT (clipboard)), NULL);
|
|
|
|
task = G_TASK (result);
|
|
|
|
g_return_val_if_fail (g_task_get_source_tag (task) == gdk_x11_clipboard_read_async, NULL);
|
|
|
|
|
2017-12-10 00:04:32 +00:00
|
|
|
if (out_mime_type)
|
2017-11-22 08:25:35 +00:00
|
|
|
{
|
2017-12-10 00:04:32 +00:00
|
|
|
GSList *targets;
|
2017-11-22 08:25:35 +00:00
|
|
|
|
2017-12-10 00:04:32 +00:00
|
|
|
targets = g_task_get_task_data (task);
|
|
|
|
*out_mime_type = targets ? targets->data : NULL;
|
2017-11-22 08:25:35 +00:00
|
|
|
}
|
2017-11-20 01:06:21 +00:00
|
|
|
|
2017-12-10 00:04:32 +00:00
|
|
|
return g_task_propagate_pointer (task, error);
|
2017-11-20 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:52:07 +00:00
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_class_init (GdkX11ClipboardClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
2017-11-20 01:06:21 +00:00
|
|
|
GdkClipboardClass *clipboard_class = GDK_CLIPBOARD_CLASS (class);
|
2017-11-19 18:52:07 +00:00
|
|
|
|
|
|
|
object_class->finalize = gdk_x11_clipboard_finalize;
|
2017-11-20 01:06:21 +00:00
|
|
|
|
2017-11-25 20:59:39 +00:00
|
|
|
clipboard_class->claim = gdk_x11_clipboard_claim;
|
2017-12-01 05:20:28 +00:00
|
|
|
clipboard_class->store_async = gdk_x11_clipboard_store_async;
|
|
|
|
clipboard_class->store_finish = gdk_x11_clipboard_store_finish;
|
2017-11-22 08:25:35 +00:00
|
|
|
clipboard_class->read_async = gdk_x11_clipboard_read_async;
|
|
|
|
clipboard_class->read_finish = gdk_x11_clipboard_read_finish;
|
2017-11-19 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_x11_clipboard_init (GdkX11Clipboard *cb)
|
|
|
|
{
|
|
|
|
cb->timestamp = CurrentTime;
|
|
|
|
}
|
|
|
|
|
2017-11-19 17:06:13 +00:00
|
|
|
GdkClipboard *
|
|
|
|
gdk_x11_clipboard_new (GdkDisplay *display,
|
|
|
|
const gchar *selection)
|
|
|
|
{
|
|
|
|
GdkX11Clipboard *cb;
|
|
|
|
|
|
|
|
cb = g_object_new (GDK_TYPE_X11_CLIPBOARD,
|
|
|
|
"display", display,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
cb->selection = g_strdup (selection);
|
|
|
|
cb->xselection = gdk_x11_get_xatom_by_name_for_display (display, selection);
|
|
|
|
|
2017-12-14 03:20:48 +00:00
|
|
|
gdk_x11_display_request_selection_notification (display, selection);
|
2017-12-13 00:53:17 +00:00
|
|
|
g_signal_connect (display, "xevent", G_CALLBACK (gdk_x11_clipboard_xevent), cb);
|
2017-11-25 20:59:39 +00:00
|
|
|
gdk_x11_clipboard_claim_remote (cb, CurrentTime);
|
2017-11-19 17:06:13 +00:00
|
|
|
|
|
|
|
return GDK_CLIPBOARD (cb);
|
|
|
|
}
|
|
|
|
|