Commit 54a43071 authored by LRN's avatar LRN
Browse files

GDK W32: Another massive clipboard and DnD update

Rename GdkWin32Selection to GdkWin32Clipdrop, since GdkSelection
is mostly gone, and the word "selection" does not reflect the
functionality of this object too well.

Clipboard is now handled by a separate thread, most of the code for
it now lives in gdkclipdrop-win32.c, gdkclipboard-win32.c just uses
clipdrop as a backend.

The DnD source part is also put into a thread.
The DnD target part does not spin the main loop, it just
emits a GDK event and returns a default value if it doesn't get a reply
by the time the event is processed.

Both clipboard and DnD use a new GOutputStream subclass to get data
from GTK and put it into a HGLOBAL.

GdkWin32DragContext is split into GdkWin32DragContext and GdkWin32DropContext,
anticipating a similar change that slated to happen to GdkDragContext.

OLE2 DnD protocol is now used by default, set GDK_WIN32_OLE2_DND envvar to 0
to make GDK use the old LOCAL and DROPFILES protocols.

https://bugzilla.gnome.org/show_bug.cgi?i...
parent 6ad33a27
/* 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-win32.h"
#include "gdkintl.h"
#include "gdkprivate-win32.h"
#include "gdkhdataoutputstream-win32.h"
#include "gdk/gdk-private.h"
#include <string.h>
typedef struct _GdkWin32ClipboardClass GdkWin32ClipboardClass;
typedef struct _RetrievalInfo RetrievalInfo;
struct _GdkWin32Clipboard
{
GdkClipboard parent;
/* Taken from the OS, the OS increments it every time
* clipboard data changes.
* -1 means that clipboard data that we claim to
* have access to is actually just an empty set that
* we made up. Thus any real data from the OS will
* override anything we make up.
*/
gint64 sequence_number;
};
struct _GdkWin32ClipboardClass
{
GdkClipboardClass parent_class;
};
G_DEFINE_TYPE (GdkWin32Clipboard, gdk_win32_clipboard, GDK_TYPE_CLIPBOARD)
static GdkContentFormats *
gdk_win32_clipboard_request_contentformats (GdkWin32Clipboard *cb)
{
BOOL success;
UINT *w32_formats = NULL;
UINT w32_formats_len = 0;
UINT w32_formats_allocated;
gsize i;
GArray *formatpairs;
GdkWin32Clipdrop *clipdrop = _gdk_win32_clipdrop_get ();
DWORD error_code;
SetLastError (0);
success = clipdrop->GetUpdatedClipboardFormats (NULL, 0, &w32_formats_allocated);
error_code = GetLastError ();
if (!success && error_code != ERROR_INSUFFICIENT_BUFFER)
{
g_warning ("Initial call to GetUpdatedClipboardFormats() failed with error %lu", error_code);
return NULL;
}
w32_formats = g_new0 (UINT, w32_formats_allocated);
SetLastError (0);
success = clipdrop->GetUpdatedClipboardFormats (w32_formats, w32_formats_allocated, &w32_formats_len);
error_code = GetLastError ();
if (!success)
{
g_warning ("Second call to GetUpdatedClipboardFormats() failed with error %lu", error_code);
g_free (w32_formats);
return NULL;
}
formatpairs = g_array_sized_new (FALSE,
FALSE,
sizeof (GdkWin32ContentFormatPair),
MIN (w32_formats_len, w32_formats_allocated));
for (i = 0; i < MIN (w32_formats_len, w32_formats_allocated); i++)
_gdk_win32_add_w32format_to_pairs (w32_formats[i], formatpairs, NULL);
g_free (w32_formats);
GDK_NOTE (DND, {
g_print ("... ");
for (i = 0; i < formatpairs->len; i++)
{
const char *mime_type = (const char *) g_array_index (formatpairs, GdkWin32ContentFormatPair, i).contentformat;
g_print ("%s", mime_type);
if (i < formatpairs->len - 1)
g_print (", ");
}
g_print ("\n");
});
if (formatpairs->len > 0)
{
GdkContentFormatsBuilder *builder = gdk_content_formats_builder_new ();
for (i = 0; i < formatpairs->len; i++)
gdk_content_formats_builder_add_mime_type (builder, g_array_index (formatpairs, GdkWin32ContentFormatPair, i).contentformat);
g_array_free (formatpairs, TRUE);
return gdk_content_formats_builder_free (builder);
}
else
{
g_array_free (formatpairs, TRUE);
return NULL;
}
}
void
gdk_win32_clipboard_claim_remote (GdkWin32Clipboard *cb)
{
GdkContentFormats *formats;
/* Claim empty first, in case the format request fails */
formats = gdk_content_formats_new (NULL, 0);
gdk_clipboard_claim_remote (GDK_CLIPBOARD (cb), formats);
gdk_content_formats_unref (formats);
cb->sequence_number = -1;
formats = gdk_win32_clipboard_request_contentformats (cb);
gdk_clipboard_claim_remote (GDK_CLIPBOARD (cb), formats);
gdk_content_formats_unref (formats);
cb->sequence_number = GetClipboardSequenceNumber ();
}
static void
gdk_win32_clipboard_finalize (GObject *object)
{
GdkWin32Clipboard *cb = GDK_WIN32_CLIPBOARD (object);
G_OBJECT_CLASS (gdk_win32_clipboard_parent_class)->finalize (object);
}
static gboolean
gdk_win32_clipboard_claim (GdkClipboard *clipboard,
GdkContentFormats *formats,
gboolean local,
GdkContentProvider *content)
{
if (local)
_gdk_win32_advertise_clipboard_contentformats (NULL, content ? formats : NULL);
return GDK_CLIPBOARD_CLASS (gdk_win32_clipboard_parent_class)->claim (clipboard, formats, local, content);
}
static void
gdk_win32_clipboard_store_async (GdkClipboard *clipboard,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GdkWin32Clipboard *cb = GDK_WIN32_CLIPBOARD (clipboard);
GdkContentProvider *content;
GdkContentFormats *formats;
GTask *store_task;
store_task = g_task_new (clipboard, cancellable, callback, user_data);
g_task_set_priority (store_task, io_priority);
g_task_set_source_tag (store_task, gdk_win32_clipboard_store_async);
content = gdk_clipboard_get_content (clipboard);
if (content == NULL)
{
GDK_NOTE (CLIPBOARD, g_printerr ("storing empty clipboard: SUCCESS!\n"));
g_task_return_boolean (store_task, TRUE);
g_clear_object (&store_task);
return;
}
formats = gdk_content_provider_ref_storable_formats (content);
formats = gdk_content_formats_union_serialize_mime_types (formats);
if (!_gdk_win32_store_clipboard_contentformats (cb, store_task, formats))
{
GDK_NOTE (CLIPBOARD, g_printerr ("clipdrop says there's nothing to store: SUCCESS!\n"));
g_task_return_boolean (store_task, TRUE);
g_clear_object (&store_task);
return;
}
}
static gboolean
gdk_win32_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_win32_clipboard_store_async, FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
static void
gdk_win32_clipboard_read_async (GdkClipboard *clipboard,
GdkContentFormats *contentformats,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GdkWin32Clipboard *cb = GDK_WIN32_CLIPBOARD (clipboard);
GSList *targets;
GTask *task;
GdkWin32Clipdrop *clipdrop = _gdk_win32_clipdrop_get ();
task = g_task_new (clipboard, cancellable, callback, user_data);
g_task_set_priority (task, io_priority);
g_task_set_source_tag (task, gdk_win32_clipboard_read_async);
_gdk_win32_retrieve_clipboard_contentformats (task, contentformats);
return;
}
static GInputStream *
gdk_win32_clipboard_read_finish (GdkClipboard *clipboard,
const char **out_mime_type,
GAsyncResult *result,
GError **error)
{
GTask *task;
GInputStream *stream;
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_win32_clipboard_read_async, NULL);
stream = g_task_propagate_pointer (task, error);
if (stream == NULL)
return stream;
if (out_mime_type)
*out_mime_type = g_object_get_data (G_OBJECT (stream), "gdk-clipboard-stream-contenttype");
return stream;
}
static void
gdk_win32_clipboard_class_init (GdkWin32ClipboardClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GdkClipboardClass *clipboard_class = GDK_CLIPBOARD_CLASS (class);
object_class->finalize = gdk_win32_clipboard_finalize;
clipboard_class->claim = gdk_win32_clipboard_claim;
clipboard_class->store_async = gdk_win32_clipboard_store_async;
clipboard_class->store_finish = gdk_win32_clipboard_store_finish;
clipboard_class->read_async = gdk_win32_clipboard_read_async;
clipboard_class->read_finish = gdk_win32_clipboard_read_finish;
}
static void
gdk_win32_clipboard_init (GdkWin32Clipboard *cb)
{
cb->sequence_number = -1;
}
GdkClipboard *
gdk_win32_clipboard_new (GdkDisplay *display)
{
GdkWin32Clipboard *cb;
cb = g_object_new (GDK_TYPE_WIN32_CLIPBOARD,
"display", display,
NULL);
gdk_win32_clipboard_claim_remote (cb);
return GDK_CLIPBOARD (cb);
}
/* 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/>.
*/
#ifndef __GDK_CLIPBOARD_WIN32_H__
#define __GDK_CLIPBOARD_WIN32_H__
#include "gdk/gdkclipboard.h"
G_BEGIN_DECLS
#define GDK_TYPE_WIN32_CLIPBOARD (gdk_win32_clipboard_get_type ())
#define GDK_WIN32_CLIPBOARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDK_TYPE_WIN32_CLIPBOARD, GdkWin32Clipboard))
#define GDK_IS_WIN32_CLIPBOARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_WIN32_CLIPBOARD))
typedef struct _GdkWin32Clipboard GdkWin32Clipboard;
GType gdk_win32_clipboard_get_type (void) G_GNUC_CONST;
GdkClipboard * gdk_win32_clipboard_new (GdkDisplay *display);
void gdk_win32_clipboard_claim_remote (GdkWin32Clipboard *cb);
G_END_DECLS
#endif /* __GDK_CLIPBOARD_WIN32_H__ */
This diff is collapsed.
/* GDK - The GIMP Drawing Kit
*
* gdkclipdrop-win32.h: Private Win32-specific clipboard/DnD object
*
* Copyright © 2017 LRN
*
* 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/>.
*/
#ifndef __GDK_CLIPDROP_WIN32_H__
#define __GDK_CLIPDROP_WIN32_H__
G_BEGIN_DECLS
#define _gdk_win32_clipdrop_get() (_win32_clipdrop)
#define _gdk_atom_array_index(a, i) (g_array_index (a, GdkAtom, i))
#define _gdk_win32_clipdrop_atom(i) (_gdk_atom_array_index (_gdk_win32_clipdrop_get ()->known_atoms, i))
#define _gdk_cf_array_index(a, i) (g_array_index (a, UINT, i))
#define _gdk_win32_clipdrop_cf(i) (_gdk_cf_array_index (_gdk_win32_clipdrop_get ()->known_clipboard_formats, i))
/* Maps GDK contentformats to w32formats or vice versa, depending on the
* semantics of the array that holds these.
* Also remembers whether the data needs to be transmuted.
*/
typedef struct {
gint w32format;
/* This is assumed to be an interned string, it will be
* compared by simply comparing the pointer.
*/
const gchar *contentformat;
gboolean transmute;
} GdkWin32ContentFormatPair;
/* OLE-based DND state */
typedef enum {
GDK_WIN32_DND_NONE,
GDK_WIN32_DND_PENDING,
GDK_WIN32_DND_DROPPED,
GDK_WIN32_DND_FAILED,
GDK_WIN32_DND_DRAGGING,
} GdkWin32DndState;
enum _GdkWin32AtomIndex
{
/* GdkAtoms: properties, targets and types */
GDK_WIN32_ATOM_INDEX_GDK_SELECTION = 0,
GDK_WIN32_ATOM_INDEX_CLIPBOARD_MANAGER,
GDK_WIN32_ATOM_INDEX_WM_TRANSIENT_FOR,
GDK_WIN32_ATOM_INDEX_TARGETS,
GDK_WIN32_ATOM_INDEX_DELETE,
GDK_WIN32_ATOM_INDEX_SAVE_TARGETS,
GDK_WIN32_ATOM_INDEX_TEXT_PLAIN_UTF8,
GDK_WIN32_ATOM_INDEX_TEXT_PLAIN,
GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST,
GDK_WIN32_ATOM_INDEX_TEXT_HTML,
GDK_WIN32_ATOM_INDEX_IMAGE_PNG,
GDK_WIN32_ATOM_INDEX_IMAGE_JPEG,
GDK_WIN32_ATOM_INDEX_IMAGE_BMP,
GDK_WIN32_ATOM_INDEX_IMAGE_GIF,
/* DND selections */
GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION,
GDK_WIN32_ATOM_INDEX_DROPFILES_DND,
GDK_WIN32_ATOM_INDEX_OLE2_DND,
/* Clipboard formats */
GDK_WIN32_ATOM_INDEX_PNG,
GDK_WIN32_ATOM_INDEX_JFIF,
GDK_WIN32_ATOM_INDEX_GIF,
GDK_WIN32_ATOM_INDEX_CF_DIB,
GDK_WIN32_ATOM_INDEX_CFSTR_SHELLIDLIST,
GDK_WIN32_ATOM_INDEX_CF_TEXT,
GDK_WIN32_ATOM_INDEX_CF_UNICODETEXT,
GDK_WIN32_ATOM_INDEX_LAST
};
typedef enum _GdkWin32AtomIndex GdkWin32AtomIndex;
enum _GdkWin32CFIndex
{
GDK_WIN32_CF_INDEX_PNG = 0,
GDK_WIN32_CF_INDEX_JFIF,
GDK_WIN32_CF_INDEX_GIF,
GDK_WIN32_CF_INDEX_UNIFORMRESOURCELOCATORW,
GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST,
GDK_WIN32_CF_INDEX_HTML_FORMAT,
GDK_WIN32_CF_INDEX_TEXT_HTML,
GDK_WIN32_CF_INDEX_IMAGE_PNG,
GDK_WIN32_CF_INDEX_IMAGE_JPEG,
GDK_WIN32_CF_INDEX_IMAGE_BMP,
GDK_WIN32_CF_INDEX_IMAGE_GIF,
GDK_WIN32_CF_INDEX_TEXT_URI_LIST,
GDK_WIN32_CF_INDEX_TEXT_PLAIN_UTF8,
GDK_WIN32_CF_INDEX_LAST
};
typedef enum _GdkWin32CFIndex GdkWin32CFIndex;
#define GDK_TYPE_WIN32_CLIPDROP (gdk_win32_clipdrop_get_type ())
#define GDK_WIN32_CLIPDROP(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_WIN32_CLIPDROP, GdkWin32Clipdrop))
#define GDK_WIN32_CLIPDROP_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_WIN32_CLIPDROP, GdkWin32ClipdropClass))
#define GDK_IS_WIN32_CLIPDROP(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_WIN32_CLIPDROP))
#define GDK_IS_WIN32_CLIPDROP_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_WIN32_CLIPDROP))
#define GDK_WIN32_CLIPDROP_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_WIN32_CLIPDROP, GdkWin32ClipdropClass))
typedef struct _GdkWin32Clipdrop GdkWin32Clipdrop;
typedef struct _GdkWin32ClipdropClass GdkWin32ClipdropClass;
typedef BOOL (WINAPI * GetUpdatedClipboardFormatsFunc)(
_Out_ PUINT lpuiFormats,
_In_ UINT cFormats,
_Out_ PUINT pcFormatsOut
);
/* This object is just a sink to hold all the clipboard- and dnd-related data
* that otherwise would be in global variables.
*/
struct _GdkWin32Clipdrop
{
GObject *parent_instance;
/* interned strings for well-known image formats */
const gchar **known_pixbuf_formats;
int n_known_pixbuf_formats;
/* GArray of GdkAtoms for various known Selection and DnD strings.
* Size is guaranteed to be at least GDK_WIN32_ATOM_INDEX_LAST
*/
GArray *known_atoms;
/* GArray of UINTs for various known clipboard formats.
* Size is guaranteed to be at least GDK_WIN32_CF_INDEX_LAST.
*/
GArray *known_clipboard_formats;
GdkWin32DndState dnd_target_state;
/* A target-keyed hash table of GArrays of GdkWin32ContentFormatPairs describing compatibility w32formats for a contentformat */
GHashTable *compatibility_w32formats;
/* A format-keyed hash table of GArrays of GdkAtoms describing compatibility contentformats for a w32format */
GHashTable *compatibility_contentformats;
/* By all rights we should be able to just use this function
* normally, as our target platform is Vista-or-later.
* This pointer is manually retrieved only to allow
* GTK to be compiled with old MinGW versions, which
* don't have GetUpdatedClipboardFormats in the import libs.
*/
GetUpdatedClipboardFormatsFunc GetUpdatedClipboardFormats;
/* The thread that tries to open the clipboard and then
* do stuff with it. Since clipboard opening can
* fail, we split the code into a thread, and let
* it try to open the clipboard repeatedly until
* the operation times out.
*/
GThread *clipboard_open_thread;
/* Our primary means of communicating with the thread.
* The communication is one-way only - the thread replies
* by just queueing functions to be called in the main
* thread by using g_idle_add().
*/
GAsyncQueue *clipboard_open_thread_queue;
/* We reply to clipboard render requests via this thing.
* We can't use messages, since the clipboard thread will
* stop spinning the message loop while it waits for us
* to render the data.
*/
GAsyncQueue *clipboard_render_queue;
/* Window handle for the clipboard window tha we
* receive from the clipboard thread. We use that
* to wake up the clipboard window main loop by
* posting a message to it.
*/
HWND clipboard_window;
/* The thread that calls DoDragDrop (), which would
* normally block our main thread, as it runs its own
* Windows message loop.
*/
GThread *dnd_thread;
DWORD dnd_thread_id;
/* We reply to the various dnd thread requests via this thing.
* We can't use messages, since the dnd thread will
* stop spinning the message loop while it waits for us
* to come up with a reply.
*/
GAsyncQueue *dnd_queue;
/* This counter is atomically incremented every time
* the main thread pushes something into the queue,
* and atomically decremented every time the DnD thread
* pops something out of it.
* It can be cheaply atomically checked to see if there's
* anything in the queue. If there is, then the queue
* processing (which requires expensice locks) can happen.
*/
gint dnd_queue_counter;
/* We don't actually support multiple simultaneous drags,
* for obvious reasons (though this might change with
* the advent of multitouch support?), but there may be
* circumstances where we have two drag contexts at
* the same time (one of them will grab the cursor
* and thus cancel the other drag operation, but
* there will be a point of time when both contexts
* are out there). Thus we keep them around in this hash table.
* Key is the context object (which is safe, because the main
* thread keeps a reference on each one of those), value
* is a pointer to a GdkWin32DnDThreadDoDragDrop struct,
* which we can only examine when we're sure that the
* dnd thread is not active.
*/
GHashTable *active_source_drags;
};
struct _GdkWin32ClipdropClass
{
GObjectClass parent_class;
};
GType gdk_win32_clipdrop_get_type (void) G_GNUC_CONST;
void _gdk_win32_clipdrop_init (void);
gboolean _gdk_win32_format_uses_hdata (UINT w32format);
gchar * _gdk_win32_get_clipboard_format_name (UINT fmt,
gboolean *is_predefined);
void _gdk_win32_add_w32format_to_pairs (UINT format,
GArray *array,
GList **list);
gint _gdk_win32_add_contentformat_to_pairs (GdkAtom target,
GArray *array);
void _gdk_win32_clipboard_default_output_done (GObject *clipboard,
GAsyncResult *result,
gpointer user_data);
gboolean _gdk_win32_transmute_contentformat (const gchar *from_contentformat,
UINT to_w32format,
const guchar *data,
gint length,
guchar **set_data,
gint *set_data_length);
gboolean _gdk_win32_transmute_windows_data (UINT from_w32format,
const gchar *to_contentformat,
HANDLE hdata,
guchar **set_data,
gsize *set_data_length);
gboolean _gdk_win32_store_clipboard_contentformats (GdkClipboard *cb,
GTask *task,
GdkContentFormats *contentformats);
void _gdk_win32_retrieve_clipboard_contentformats (GTask *task,
GdkContentFormats *contentformats);
void _gdk_win32_advertise_clipboard_contentformats (GTask *task,
GdkContentFormats *contentformats);
#endif /* __GDK_CLIPDROP_WIN32_H__ */
......@@ -23,6 +23,8 @@
#include "gdk.h"
#include "gdkprivate-win32.h"