summaryrefslogtreecommitdiff
path: root/gdk/win32/gdkclipboard-win32.c
blob: 4544b3a0804812638ff5a508e09072007b083153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 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_to_formats (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)
{
  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)
{
  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 (clipboard, 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)
{
  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_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);
}