mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2025-01-01 00:11:29 +00:00
a4b42f0b73
And with it, remove the selections section from the docs. So selections are gone for good now.
832 lines
23 KiB
C
832 lines
23 KiB
C
/*
|
|
* Copyright © 2010 Intel Corporation
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Library 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
|
|
* Library General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Library General Public
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
#include <gio/gunixinputstream.h>
|
|
#include <gio/gunixoutputstream.h>
|
|
#include <glib-unix.h>
|
|
|
|
#include "gdkwayland.h"
|
|
#include "gdkprivate-wayland.h"
|
|
#include "gdkdisplay-wayland.h"
|
|
#include "gdkcontentformatsprivate.h"
|
|
#include "gdkdndprivate.h"
|
|
#include "gdkproperty.h"
|
|
|
|
#include <string.h>
|
|
|
|
typedef struct _SelectionBuffer SelectionBuffer;
|
|
typedef struct _SelectionData SelectionData;
|
|
typedef struct _StoredSelection StoredSelection;
|
|
typedef struct _AsyncWriteData AsyncWriteData;
|
|
typedef struct _DataOfferData DataOfferData;
|
|
|
|
struct _SelectionBuffer
|
|
{
|
|
GInputStream *stream;
|
|
GCancellable *cancellable;
|
|
GByteArray *data;
|
|
GList *requestors;
|
|
GdkAtom selection;
|
|
GdkAtom target;
|
|
gint ref_count;
|
|
};
|
|
|
|
struct _StoredSelection
|
|
{
|
|
GdkWindow *source;
|
|
GCancellable *cancellable;
|
|
guchar *data;
|
|
gsize data_len;
|
|
GdkAtom type;
|
|
gint fd;
|
|
};
|
|
|
|
struct _DataOfferData
|
|
{
|
|
GDestroyNotify destroy_notify;
|
|
gpointer offer_data;
|
|
GdkContentFormats *targets;
|
|
};
|
|
|
|
struct _AsyncWriteData
|
|
{
|
|
GOutputStream *stream;
|
|
GdkWaylandSelection *selection;
|
|
gsize index;
|
|
};
|
|
|
|
struct _SelectionData
|
|
{
|
|
DataOfferData *offer;
|
|
};
|
|
|
|
enum {
|
|
ATOM_DND,
|
|
N_ATOMS
|
|
};
|
|
|
|
static GdkAtom atoms[N_ATOMS] = { 0 };
|
|
|
|
struct _GdkWaylandSelection
|
|
{
|
|
/* Destination-side data */
|
|
SelectionData selections[N_ATOMS];
|
|
GHashTable *offers; /* Currently alive offers, Hashtable of wl_data_offer->DataOfferData */
|
|
|
|
/* Source-side data */
|
|
StoredSelection stored_selection;
|
|
|
|
struct wl_data_source *dnd_source; /* Owned by the GdkDragContext */
|
|
};
|
|
|
|
static void async_write_data_write (AsyncWriteData *write_data);
|
|
|
|
static inline glong
|
|
get_buffer_size (void)
|
|
{
|
|
return sysconf (_SC_PAGESIZE);
|
|
}
|
|
|
|
static DataOfferData *
|
|
data_offer_data_new (gpointer offer,
|
|
GDestroyNotify destroy_notify)
|
|
{
|
|
DataOfferData *info;
|
|
|
|
info = g_slice_new0 (DataOfferData);
|
|
info->offer_data = offer;
|
|
info->destroy_notify = destroy_notify;
|
|
info->targets = gdk_content_formats_new (NULL, 0);
|
|
|
|
return info;
|
|
}
|
|
|
|
static void
|
|
data_offer_data_free (DataOfferData *info)
|
|
{
|
|
info->destroy_notify (info->offer_data);
|
|
gdk_content_formats_unref (info->targets);
|
|
g_slice_free (DataOfferData, info);
|
|
}
|
|
|
|
GdkWaylandSelection *
|
|
gdk_wayland_selection_new (void)
|
|
{
|
|
GdkWaylandSelection *selection;
|
|
|
|
/* init atoms */
|
|
atoms[ATOM_DND] = gdk_atom_intern_static_string ("GdkWaylandSelection");
|
|
|
|
selection = g_new0 (GdkWaylandSelection, 1);
|
|
|
|
selection->offers =
|
|
g_hash_table_new_full (NULL, NULL, NULL,
|
|
(GDestroyNotify) data_offer_data_free);
|
|
selection->stored_selection.fd = -1;
|
|
return selection;
|
|
}
|
|
|
|
void
|
|
gdk_wayland_selection_free (GdkWaylandSelection *selection)
|
|
{
|
|
g_hash_table_destroy (selection->offers);
|
|
g_free (selection->stored_selection.data);
|
|
|
|
if (selection->stored_selection.cancellable)
|
|
{
|
|
g_cancellable_cancel (selection->stored_selection.cancellable);
|
|
g_object_unref (selection->stored_selection.cancellable);
|
|
}
|
|
|
|
if (selection->stored_selection.fd > 0)
|
|
close (selection->stored_selection.fd);
|
|
|
|
if (selection->dnd_source)
|
|
wl_data_source_destroy (selection->dnd_source);
|
|
|
|
g_free (selection);
|
|
}
|
|
|
|
static void
|
|
data_offer_offer (void *data,
|
|
struct wl_data_offer *wl_data_offer,
|
|
const char *type)
|
|
{
|
|
GdkWaylandSelection *selection = data;
|
|
GdkContentFormatsBuilder *builder;
|
|
DataOfferData *info;
|
|
|
|
info = g_hash_table_lookup (selection->offers, wl_data_offer);
|
|
|
|
if (!info || gdk_content_formats_contain_mime_type (info->targets, type))
|
|
return;
|
|
|
|
GDK_NOTE (EVENTS,
|
|
g_message ("data offer offer, offer %p, type = %s", wl_data_offer, type));
|
|
|
|
builder = gdk_content_formats_builder_new ();
|
|
gdk_content_formats_builder_add_formats (builder, info->targets);
|
|
gdk_content_formats_builder_add_mime_type (builder, type);
|
|
gdk_content_formats_unref (info->targets);
|
|
info->targets = gdk_content_formats_builder_free (builder);
|
|
}
|
|
|
|
static inline GdkDragAction
|
|
_wl_to_gdk_actions (uint32_t dnd_actions)
|
|
{
|
|
GdkDragAction actions = 0;
|
|
|
|
if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY)
|
|
actions |= GDK_ACTION_COPY;
|
|
if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
|
|
actions |= GDK_ACTION_MOVE;
|
|
if (dnd_actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
|
|
actions |= GDK_ACTION_ASK;
|
|
|
|
return actions;
|
|
}
|
|
|
|
static void
|
|
data_offer_source_actions (void *data,
|
|
struct wl_data_offer *wl_data_offer,
|
|
uint32_t source_actions)
|
|
{
|
|
GdkDragContext *drop_context;
|
|
GdkDisplay *display;
|
|
GdkDevice *device;
|
|
GdkSeat *seat;
|
|
|
|
display = gdk_display_get_default ();
|
|
seat = gdk_display_get_default_seat (display);
|
|
device = gdk_seat_get_pointer (seat);
|
|
drop_context = gdk_wayland_device_get_drop_context (device);
|
|
|
|
drop_context->actions = _wl_to_gdk_actions (source_actions);
|
|
|
|
GDK_NOTE (EVENTS,
|
|
g_message ("data offer source actions, offer %p, actions %d", wl_data_offer, source_actions));
|
|
|
|
if (gdk_drag_context_get_dest_window (drop_context))
|
|
_gdk_wayland_drag_context_emit_event (drop_context, GDK_DRAG_MOTION,
|
|
GDK_CURRENT_TIME);
|
|
}
|
|
|
|
static void
|
|
data_offer_action (void *data,
|
|
struct wl_data_offer *wl_data_offer,
|
|
uint32_t action)
|
|
{
|
|
GdkDragContext *drop_context;
|
|
GdkDisplay *display;
|
|
GdkDevice *device;
|
|
GdkSeat *seat;
|
|
|
|
display = gdk_display_get_default ();
|
|
seat = gdk_display_get_default_seat (display);
|
|
device = gdk_seat_get_pointer (seat);
|
|
drop_context = gdk_wayland_device_get_drop_context (device);
|
|
|
|
drop_context->action = _wl_to_gdk_actions (action);
|
|
|
|
if (gdk_drag_context_get_dest_window (drop_context))
|
|
_gdk_wayland_drag_context_emit_event (drop_context, GDK_DRAG_MOTION,
|
|
GDK_CURRENT_TIME);
|
|
}
|
|
|
|
static const struct wl_data_offer_listener data_offer_listener = {
|
|
data_offer_offer,
|
|
data_offer_source_actions,
|
|
data_offer_action
|
|
};
|
|
|
|
static SelectionData *
|
|
selection_lookup_offer_by_atom (GdkWaylandSelection *selection,
|
|
GdkAtom selection_atom)
|
|
{
|
|
if (selection_atom == atoms[ATOM_DND])
|
|
return &selection->selections[ATOM_DND];
|
|
else
|
|
return NULL;
|
|
}
|
|
|
|
void
|
|
gdk_wayland_selection_ensure_offer (GdkDisplay *display,
|
|
struct wl_data_offer *wl_offer)
|
|
{
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
DataOfferData *info;
|
|
|
|
info = g_hash_table_lookup (selection->offers, wl_offer);
|
|
|
|
if (!info)
|
|
{
|
|
info = data_offer_data_new (wl_offer,
|
|
(GDestroyNotify) wl_data_offer_destroy);
|
|
g_hash_table_insert (selection->offers, wl_offer, info);
|
|
wl_data_offer_add_listener (wl_offer,
|
|
&data_offer_listener,
|
|
selection);
|
|
}
|
|
}
|
|
|
|
GdkContentFormats *
|
|
gdk_wayland_selection_steal_offer (GdkDisplay *display,
|
|
gpointer wl_offer)
|
|
{
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
GdkContentFormats *formats;
|
|
DataOfferData *info;
|
|
|
|
info = g_hash_table_lookup (selection->offers, wl_offer);
|
|
if (info == NULL)
|
|
return NULL;
|
|
|
|
g_hash_table_steal (selection->offers, wl_offer);
|
|
formats = info->targets;
|
|
g_slice_free (DataOfferData, info);
|
|
|
|
return formats;
|
|
}
|
|
|
|
void
|
|
gdk_wayland_selection_set_offer (GdkDisplay *display,
|
|
GdkAtom selection_atom,
|
|
gpointer wl_offer)
|
|
{
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
struct wl_data_offer *prev_offer;
|
|
SelectionData *selection_data;
|
|
DataOfferData *info;
|
|
|
|
info = g_hash_table_lookup (selection->offers, wl_offer);
|
|
|
|
prev_offer = gdk_wayland_selection_get_offer (display, selection_atom);
|
|
|
|
if (prev_offer)
|
|
g_hash_table_remove (selection->offers, prev_offer);
|
|
|
|
selection_data = selection_lookup_offer_by_atom (selection, selection_atom);
|
|
|
|
if (selection_data)
|
|
{
|
|
selection_data->offer = info;
|
|
}
|
|
}
|
|
|
|
gpointer
|
|
gdk_wayland_selection_get_offer (GdkDisplay *display,
|
|
GdkAtom selection_atom)
|
|
{
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
const SelectionData *data;
|
|
|
|
data = selection_lookup_offer_by_atom (selection, selection_atom);
|
|
|
|
if (data && data->offer)
|
|
return data->offer->offer_data;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
GdkContentFormats *
|
|
gdk_wayland_selection_get_targets (GdkDisplay *display,
|
|
GdkAtom selection_atom)
|
|
{
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
const SelectionData *data;
|
|
|
|
data = selection_lookup_offer_by_atom (selection, selection_atom);
|
|
|
|
if (data && data->offer)
|
|
return data->offer->targets;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static AsyncWriteData *
|
|
async_write_data_new (GdkWaylandSelection *selection)
|
|
{
|
|
AsyncWriteData *write_data;
|
|
|
|
write_data = g_slice_new0 (AsyncWriteData);
|
|
write_data->selection = selection;
|
|
write_data->stream =
|
|
g_unix_output_stream_new (selection->stored_selection.fd, TRUE);
|
|
|
|
selection->stored_selection.fd = -1;
|
|
|
|
return write_data;
|
|
}
|
|
|
|
static void
|
|
async_write_data_free (AsyncWriteData *write_data)
|
|
{
|
|
g_object_unref (write_data->stream);
|
|
g_slice_free (AsyncWriteData, write_data);
|
|
}
|
|
|
|
static void
|
|
async_write_data_cb (GObject *object,
|
|
GAsyncResult *res,
|
|
gpointer user_data)
|
|
{
|
|
AsyncWriteData *write_data = user_data;
|
|
GError *error = NULL;
|
|
gsize bytes_written;
|
|
|
|
bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (object),
|
|
res, &error);
|
|
if (error)
|
|
{
|
|
if (error->domain != G_IO_ERROR ||
|
|
error->code != G_IO_ERROR_CANCELLED)
|
|
g_warning ("Error writing selection data: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
async_write_data_free (write_data);
|
|
return;
|
|
}
|
|
|
|
write_data->index += bytes_written;
|
|
|
|
if (write_data->index <
|
|
write_data->selection->stored_selection.data_len)
|
|
{
|
|
/* Write the next chunk */
|
|
async_write_data_write (write_data);
|
|
}
|
|
else
|
|
async_write_data_free (write_data);
|
|
}
|
|
|
|
static void
|
|
async_write_data_write (AsyncWriteData *write_data)
|
|
{
|
|
GdkWaylandSelection *selection = write_data->selection;
|
|
gsize buf_len;
|
|
guchar *buf;
|
|
|
|
buf = selection->stored_selection.data;
|
|
buf_len = selection->stored_selection.data_len;
|
|
|
|
g_output_stream_write_async (write_data->stream,
|
|
&buf[write_data->index],
|
|
buf_len - write_data->index,
|
|
G_PRIORITY_DEFAULT,
|
|
selection->stored_selection.cancellable,
|
|
async_write_data_cb,
|
|
write_data);
|
|
}
|
|
|
|
static gboolean
|
|
gdk_wayland_selection_check_write (GdkWaylandSelection *selection)
|
|
{
|
|
AsyncWriteData *write_data;
|
|
|
|
if (selection->stored_selection.fd < 0)
|
|
return FALSE;
|
|
|
|
/* Cancel any previous ongoing async write */
|
|
if (selection->stored_selection.cancellable)
|
|
{
|
|
g_cancellable_cancel (selection->stored_selection.cancellable);
|
|
g_object_unref (selection->stored_selection.cancellable);
|
|
}
|
|
|
|
selection->stored_selection.cancellable = g_cancellable_new ();
|
|
|
|
write_data = async_write_data_new (selection);
|
|
async_write_data_write (write_data);
|
|
selection->stored_selection.fd = -1;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void
|
|
gdk_wayland_selection_store (GdkWindow *window,
|
|
GdkAtom type,
|
|
GdkPropMode mode,
|
|
const guchar *data,
|
|
gint len)
|
|
{
|
|
GdkDisplay *display = gdk_window_get_display (window);
|
|
GdkWaylandSelection *selection = gdk_wayland_display_get_selection (display);
|
|
GArray *array;
|
|
|
|
if (type == gdk_atom_intern_static_string ("NULL"))
|
|
return;
|
|
|
|
array = g_array_new (TRUE, FALSE, sizeof (guchar));
|
|
g_array_append_vals (array, data, len);
|
|
|
|
if (selection->stored_selection.data)
|
|
{
|
|
if (mode != GDK_PROP_MODE_REPLACE &&
|
|
type != selection->stored_selection.type)
|
|
{
|
|
gchar *type_str, *stored_str;
|
|
|
|
type_str = gdk_atom_name (type);
|
|
stored_str = gdk_atom_name (selection->stored_selection.type);
|
|
|
|
g_warning (G_STRLOC ": Attempted to append/prepend selection data with "
|
|
"type %s into the current selection with type %s",
|
|
type_str, stored_str);
|
|
g_free (type_str);
|
|
g_free (stored_str);
|
|
return;
|
|
}
|
|
|
|
/* In these cases we also replace the stored data, so we
|
|
* apply the inverse operation into the just given data.
|
|
*/
|
|
if (mode == GDK_PROP_MODE_APPEND)
|
|
g_array_prepend_vals (array, selection->stored_selection.data,
|
|
selection->stored_selection.data_len - 1);
|
|
else if (mode == GDK_PROP_MODE_PREPEND)
|
|
g_array_append_vals (array, selection->stored_selection.data,
|
|
selection->stored_selection.data_len - 1);
|
|
|
|
g_free (selection->stored_selection.data);
|
|
}
|
|
|
|
selection->stored_selection.source = window;
|
|
selection->stored_selection.data_len = array->len;
|
|
selection->stored_selection.data = (guchar *) g_array_free (array, FALSE);
|
|
selection->stored_selection.type = type;
|
|
|
|
gdk_wayland_selection_check_write (selection);
|
|
}
|
|
|
|
static void
|
|
data_source_target (void *data,
|
|
struct wl_data_source *source,
|
|
const char *mime_type)
|
|
{
|
|
GDK_NOTE (EVENTS,
|
|
g_message ("data source target, source = %p, mime_type = %s",
|
|
source, mime_type));
|
|
}
|
|
|
|
static void
|
|
gdk_wayland_drag_context_write_done (GObject *context,
|
|
GAsyncResult *result,
|
|
gpointer user_data)
|
|
{
|
|
GError *error = NULL;
|
|
|
|
if (!gdk_drag_context_write_finish (GDK_DRAG_CONTEXT (context), result, &error))
|
|
{
|
|
GDK_NOTE(DND, g_printerr ("%p: failed to write stream: %s\n", context, error->message));
|
|
g_error_free (error);
|
|
}
|
|
}
|
|
|
|
static void
|
|
data_source_send (void *data,
|
|
struct wl_data_source *source,
|
|
const char *mime_type,
|
|
int32_t fd)
|
|
{
|
|
GdkDragContext *context;
|
|
GOutputStream *stream;
|
|
|
|
context = gdk_wayland_drag_context_lookup_by_data_source (source);
|
|
if (!context)
|
|
return;
|
|
|
|
GDK_NOTE (DND, g_printerr ("%p: data source send request for %s on fd %d\n",
|
|
source, mime_type, fd));
|
|
|
|
//mime_type = gdk_intern_mime_type (mime_type);
|
|
mime_type = g_intern_string (mime_type);
|
|
stream = g_unix_output_stream_new (fd, TRUE);
|
|
|
|
gdk_drag_context_write_async (context,
|
|
mime_type,
|
|
stream,
|
|
G_PRIORITY_DEFAULT,
|
|
NULL,
|
|
gdk_wayland_drag_context_write_done,
|
|
context);
|
|
g_object_unref (stream);
|
|
}
|
|
|
|
static void
|
|
data_source_cancelled (void *data,
|
|
struct wl_data_source *source)
|
|
{
|
|
GdkWaylandSelection *wayland_selection = data;
|
|
GdkDragContext *context;
|
|
GdkDisplay *display;
|
|
GdkAtom atom;
|
|
|
|
GDK_NOTE (EVENTS,
|
|
g_message ("data source cancelled, source = %p", source));
|
|
|
|
display = gdk_display_get_default ();
|
|
|
|
if (source == wayland_selection->dnd_source)
|
|
atom = atoms[ATOM_DND];
|
|
else
|
|
return;
|
|
|
|
context = gdk_wayland_drag_context_lookup_by_data_source (source);
|
|
|
|
if (context)
|
|
gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_ERROR);
|
|
|
|
gdk_wayland_selection_unset_data_source (display, atom);
|
|
}
|
|
|
|
static void
|
|
data_source_dnd_drop_performed (void *data,
|
|
struct wl_data_source *source)
|
|
{
|
|
GdkDragContext *context;
|
|
|
|
context = gdk_wayland_drag_context_lookup_by_data_source (source);
|
|
|
|
if (!context)
|
|
return;
|
|
|
|
g_signal_emit_by_name (context, "drop-performed", GDK_CURRENT_TIME);
|
|
}
|
|
|
|
static void
|
|
data_source_dnd_finished (void *data,
|
|
struct wl_data_source *source)
|
|
{
|
|
GdkDragContext *context;
|
|
|
|
context = gdk_wayland_drag_context_lookup_by_data_source (source);
|
|
|
|
if (!context)
|
|
return;
|
|
|
|
g_signal_emit_by_name (context, "dnd-finished");
|
|
}
|
|
|
|
static void
|
|
data_source_action (void *data,
|
|
struct wl_data_source *source,
|
|
uint32_t action)
|
|
{
|
|
GdkDragContext *context;
|
|
|
|
GDK_NOTE (EVENTS,
|
|
g_message ("data source action, source = %p action=%x",
|
|
source, action));
|
|
|
|
context = gdk_wayland_drag_context_lookup_by_data_source (source);
|
|
|
|
if (!context)
|
|
return;
|
|
|
|
context->action = _wl_to_gdk_actions (action);
|
|
g_signal_emit_by_name (context, "action-changed", context->action);
|
|
}
|
|
|
|
static const struct wl_data_source_listener data_source_listener = {
|
|
data_source_target,
|
|
data_source_send,
|
|
data_source_cancelled,
|
|
data_source_dnd_drop_performed,
|
|
data_source_dnd_finished,
|
|
data_source_action,
|
|
};
|
|
|
|
struct wl_data_source *
|
|
gdk_wayland_selection_get_data_source (GdkWindow *owner,
|
|
GdkAtom selection)
|
|
{
|
|
GdkDisplay *display = gdk_window_get_display (owner);
|
|
GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
|
|
gpointer source = NULL;
|
|
GdkWaylandDisplay *display_wayland;
|
|
|
|
if (selection == atoms[ATOM_DND])
|
|
{
|
|
if (wayland_selection->dnd_source)
|
|
return wayland_selection->dnd_source;
|
|
}
|
|
else
|
|
return NULL;
|
|
|
|
if (!owner)
|
|
return NULL;
|
|
|
|
display_wayland = GDK_WAYLAND_DISPLAY (gdk_window_get_display (owner));
|
|
|
|
source = wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
|
|
wl_data_source_add_listener (source,
|
|
&data_source_listener,
|
|
wayland_selection);
|
|
|
|
if (selection == atoms[ATOM_DND])
|
|
wayland_selection->dnd_source = source;
|
|
|
|
return source;
|
|
}
|
|
|
|
void
|
|
gdk_wayland_selection_unset_data_source (GdkDisplay *display,
|
|
GdkAtom selection)
|
|
{
|
|
GdkWaylandSelection *wayland_selection = gdk_wayland_display_get_selection (display);
|
|
|
|
if (selection == atoms[ATOM_DND])
|
|
{
|
|
wayland_selection->dnd_source = NULL;
|
|
}
|
|
}
|
|
|
|
gint
|
|
_gdk_wayland_display_text_property_to_utf8_list (GdkDisplay *display,
|
|
GdkAtom encoding,
|
|
gint format,
|
|
const guchar *text,
|
|
gint length,
|
|
gchar ***list)
|
|
{
|
|
GPtrArray *array;
|
|
const gchar *ptr;
|
|
gsize chunk_len;
|
|
gchar *copy;
|
|
guint nitems;
|
|
|
|
ptr = (const gchar *) text;
|
|
array = g_ptr_array_new ();
|
|
|
|
while (ptr < (const gchar *) &text[length])
|
|
{
|
|
chunk_len = strlen (ptr);
|
|
|
|
if (g_utf8_validate (ptr, chunk_len, NULL))
|
|
{
|
|
copy = g_strndup (ptr, chunk_len);
|
|
g_ptr_array_add (array, copy);
|
|
}
|
|
|
|
ptr = &ptr[chunk_len + 1];
|
|
}
|
|
|
|
nitems = array->len;
|
|
g_ptr_array_add (array, NULL);
|
|
|
|
if (list)
|
|
*list = (gchar **) g_ptr_array_free (array, FALSE);
|
|
else
|
|
g_ptr_array_free (array, TRUE);
|
|
|
|
return nitems;
|
|
}
|
|
|
|
/* This function has been copied straight from the x11 backend */
|
|
static gchar *
|
|
sanitize_utf8 (const gchar *src,
|
|
gboolean return_latin1)
|
|
{
|
|
gint len = strlen (src);
|
|
GString *result = g_string_sized_new (len);
|
|
const gchar *p = src;
|
|
|
|
while (*p)
|
|
{
|
|
if (*p == '\r')
|
|
{
|
|
p++;
|
|
if (*p == '\n')
|
|
p++;
|
|
|
|
g_string_append_c (result, '\n');
|
|
}
|
|
else
|
|
{
|
|
gunichar ch = g_utf8_get_char (p);
|
|
|
|
if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
|
|
{
|
|
if (return_latin1)
|
|
{
|
|
if (ch <= 0xff)
|
|
g_string_append_c (result, ch);
|
|
else
|
|
g_string_append_printf (result,
|
|
ch < 0x10000 ? "\\u%04x" : "\\U%08x",
|
|
ch);
|
|
}
|
|
else
|
|
{
|
|
char buf[7];
|
|
gint buflen;
|
|
|
|
buflen = g_unichar_to_utf8 (ch, buf);
|
|
g_string_append_len (result, buf, buflen);
|
|
}
|
|
}
|
|
|
|
p = g_utf8_next_char (p);
|
|
}
|
|
}
|
|
|
|
return g_string_free (result, FALSE);
|
|
}
|
|
|
|
gchar *
|
|
_gdk_wayland_display_utf8_to_string_target (GdkDisplay *display,
|
|
const gchar *str)
|
|
{
|
|
/* This is mainly needed when interfacing with old clients through
|
|
* Xwayland, the STRING target could be used, and passed as-is
|
|
* by the compositor.
|
|
*
|
|
* There's already some handling of this atom (aka "mimetype" in
|
|
* this backend) in common code, so we end up in this vfunc.
|
|
*/
|
|
return sanitize_utf8 (str, TRUE);
|
|
}
|
|
|
|
gboolean
|
|
gdk_wayland_selection_set_current_offer_actions (GdkDisplay *display,
|
|
uint32_t action)
|
|
{
|
|
GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
|
|
struct wl_data_offer *offer;
|
|
uint32_t all_actions = 0;
|
|
|
|
offer = gdk_wayland_selection_get_offer (display, atoms[ATOM_DND]);
|
|
|
|
if (!offer)
|
|
return FALSE;
|
|
|
|
if (action != 0)
|
|
all_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
|
|
WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE |
|
|
WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
|
|
|
|
if (display_wayland->data_device_manager_version >=
|
|
WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION)
|
|
wl_data_offer_set_actions (offer, all_actions, action);
|
|
return TRUE;
|
|
}
|