summaryrefslogtreecommitdiff
path: root/gtk/gtkclipboard-wayland.c
blob: 328ba4df417cc9b68a158914be3c17d5075b54bd (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* GTK - The GIMP Toolkit
 * Copyright (C) 2000 Red Hat, Inc.
 * Copyright (C) 2004 Nokia Corporation
 * Copyright (C) 2006-2008 Imendio AB
 * Copyright (C) 2011-2012 Intel Corporation
 *
 * 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 "gtkclipboard-waylandprivate.h"

#ifdef GDK_WINDOWING_WAYLAND

#include <string.h>

#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkintl.h"
#include "gtkselectionprivate.h"

static void gtk_clipboard_wayland_owner_change     (GtkClipboard             *clipboard,
                                                    GdkEventOwnerChange      *event);
static gboolean gtk_clipboard_wayland_set_contents (GtkClipboard             *clipboard,
                                                    const GtkTargetEntry     *targets,
                                                    guint                     n_targets,
                                                    GtkClipboardGetFunc       get_func,
                                                    GtkClipboardClearFunc     clear_func,
                                                    gpointer                  user_data,
                                                    gboolean                  have_owner);
static void gtk_clipboard_wayland_clear            (GtkClipboard             *clipboard);
static void gtk_clipboard_wayland_request_contents (GtkClipboard             *clipboard,
                                                    GdkAtom                   target,
                                                    GtkClipboardReceivedFunc  callback,
                                                    gpointer                  user_data);
static void gtk_clipboard_wayland_set_can_store    (GtkClipboard             *clipboard,
                                                    const GtkTargetEntry     *targets,
                                                    gint                      n_targets);
static void gtk_clipboard_wayland_store            (GtkClipboard             *clipboard);

G_DEFINE_TYPE (GtkClipboardWayland, gtk_clipboard_wayland, GTK_TYPE_CLIPBOARD);

static void
gtk_clipboard_wayland_class_init (GtkClipboardWaylandClass *klass)
{
  GtkClipboardClass *clipboard_class = GTK_CLIPBOARD_CLASS (klass);

  clipboard_class->set_contents = gtk_clipboard_wayland_set_contents;
  clipboard_class->clear = gtk_clipboard_wayland_clear;
  clipboard_class->request_contents = gtk_clipboard_wayland_request_contents;
  clipboard_class->set_can_store = gtk_clipboard_wayland_set_can_store;
  clipboard_class->store = gtk_clipboard_wayland_store;
  clipboard_class->owner_change = gtk_clipboard_wayland_owner_change;
}

static void
gtk_clipboard_wayland_init (GtkClipboardWayland *clipboard)
{
}

struct _SetContentClosure {
    GtkClipboard *clipboard;
    GtkClipboardGetFunc get_func;
    GtkClipboardClearFunc clear_func;
    guint info;
    gboolean have_owner;
    gpointer userdata;
    GtkTargetPair *targets;
    gint n_targets;
};

static gchar *
_offer_cb (GdkDevice   *device,
           const gchar *mime_type,
           gssize      *len,
           gpointer     userdata)
{
  SetContentClosure *closure = (SetContentClosure *)userdata;
  GtkSelectionData selection_data = { 0, };
  guint info = 0;
  gint i;

  selection_data.target = gdk_atom_intern (mime_type, FALSE);

  for (i = 0; i < closure->n_targets; i++)
    {
      if (closure->targets[i].target == selection_data.target)
        {
          info = closure->targets[i].info;
          break;
        }
    }

  closure->get_func (closure->clipboard,
                     &selection_data,
                     info,
                     closure->userdata);

  *len = gtk_selection_data_get_length (&selection_data);

  /* The caller of this callback will free this data - the GtkClipboardGetFunc
   * uses gtk_selection_data_set which copies*/
  return (gchar *)selection_data.data;
}

static void
clipboard_owner_destroyed (gpointer data,
			   GObject *owner)
{
  GtkClipboardWayland *clipboard = GTK_CLIPBOARD_WAYLAND (data);
  GtkClipboard *gtkclipboard = GTK_CLIPBOARD (data);
  SetContentClosure *last_closure = clipboard->last_closure;

  last_closure->userdata = NULL;
  last_closure->get_func = NULL;
  last_closure->clear_func = NULL;
  last_closure->have_owner = FALSE;

  gtk_clipboard_clear (gtkclipboard);
}

static gboolean
gtk_clipboard_wayland_set_contents (GtkClipboard         *gtkclipboard,
                                    const GtkTargetEntry *targets,
                                    guint                 n_targets,
                                    GtkClipboardGetFunc   get_func,
                                    GtkClipboardClearFunc clear_func,
                                    gpointer              user_data,
                                    gboolean              have_owner)
{
  GtkClipboardWayland *clipboard = GTK_CLIPBOARD_WAYLAND (gtkclipboard);
  GdkDeviceManager *device_manager;
  GdkDevice *device;
  gint i, j;
  gchar **mimetypes;
  SetContentClosure *closure, *last_closure;

  if (gtkclipboard->selection != GDK_SELECTION_CLIPBOARD)
    return FALSE;

  last_closure = clipboard->last_closure;
  if (!last_closure ||
      (!last_closure->have_owner && have_owner) ||
      (last_closure->userdata != user_data)) {
    gtk_clipboard_clear (gtkclipboard);

    closure = g_new0 (SetContentClosure, 1);
    closure->clipboard = gtkclipboard;
    closure->userdata = user_data;
    closure->have_owner = have_owner;

    if (have_owner)
      g_object_weak_ref (G_OBJECT (user_data), clipboard_owner_destroyed, clipboard);
  } else {
    closure = last_closure;
    g_free (closure->targets);
  }

  closure->get_func = get_func;
  closure->clear_func = clear_func;
  closure->targets = g_new0 (GtkTargetPair, n_targets);

  device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
  device = gdk_device_manager_get_client_pointer (device_manager);

  mimetypes = g_new (gchar *, n_targets);

  for (i = 0, j = 0; i < n_targets; i++)
    {
      if (strcmp(targets[i].target, "COMPOUND_TEXT") == 0)
	continue;
      if (strcmp(targets[i].target, "UTF8_STRING") == 0)
	continue;
      if (strcmp(targets[i].target, "TEXT") == 0)
	continue;
      if (strcmp(targets[i].target, "STRING") == 0)
	continue;
      if (strcmp(targets[i].target, "GTK_TEXT_BUFFER_CONTENTS") == 0)
	continue;

      mimetypes[j] = targets[i].target;
      closure->targets[j].target = gdk_atom_intern (targets[i].target, FALSE);
      closure->targets[j].flags = targets[i].flags;
      closure->targets[j].info = targets[i].info;
      j++;
    }

  closure->n_targets = j;

  gdk_wayland_device_offer_selection_content (device,
                                              (const gchar **)mimetypes,
                                              j,
                                              _offer_cb,
                                              closure);
  clipboard->last_closure = closure;

  g_free (mimetypes);
  return TRUE;
}

static void
gtk_clipboard_wayland_clear (GtkClipboard *gtkclipboard)
{
  GtkClipboardWayland *clipboard = GTK_CLIPBOARD_WAYLAND (gtkclipboard);
  GdkDeviceManager *device_manager;
  GdkDevice *device;

  if (!clipboard->last_closure)
    return;

  device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
  device = gdk_device_manager_get_client_pointer (device_manager);

  gdk_wayland_device_clear_selection_content (device);

  if (clipboard->last_closure->clear_func)
    {
      clipboard->last_closure->clear_func (gtkclipboard,
                                           clipboard->last_closure->userdata);
    }

  if (clipboard->last_closure->have_owner)
    g_object_weak_unref (G_OBJECT (clipboard->last_closure->userdata),
                         clipboard_owner_destroyed, clipboard);
  g_free (clipboard->last_closure->targets);
  g_free (clipboard->last_closure);

  clipboard->last_closure = NULL;
}

typedef struct {
    GtkClipboard *clipboard;
    GCallback cb;
    gpointer userdata;
    GdkAtom target;
} ClipboardRequestClosure;

static void
_request_generic_cb (GdkDevice   *device,
                     const gchar *data,
                     gsize        len,
                     gpointer     userdata)
{
  ClipboardRequestClosure *closure = (ClipboardRequestClosure *)userdata;
  GtkClipboardReceivedFunc cb = (GtkClipboardReceivedFunc)closure->cb;
  GtkSelectionData selection_data;

  selection_data.selection = GDK_SELECTION_CLIPBOARD;
  selection_data.target = closure->target;
  selection_data.type = closure->target;
  selection_data.length = len;
  selection_data.data = (guchar *)data;

  cb (closure->clipboard, &selection_data, closure->userdata);

  g_free (closure);
}

static void
gtk_clipboard_wayland_request_contents (GtkClipboard            *gtkclipboard,
                                        GdkAtom                  target,
                                        GtkClipboardReceivedFunc callback,
                                        gpointer                 user_data)
{
  GdkDeviceManager *device_manager;
  GdkDevice *device;
  ClipboardRequestClosure *closure;
  gchar *mime_type;

  device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
  device = gdk_device_manager_get_client_pointer (device_manager);

  /* When GTK+ requests text, it tries UTF8_STRING first and then
   * falls back to COMPOUND_TEXT and then STRING.  We rewrite
   * UTF8_STRING to text/plain;charset=utf-8, and if that doesn't
   * work, just fail the fallback targets.  Maybe we could do this in
   * a generic way that just compares the target against the targets
   * advertised by the data offer. */
  if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
    target = gdk_atom_intern_static_string ("text/plain;charset=utf-8");
  else if (target == gdk_atom_intern_static_string ("COMPOUND_TEXT") ||
	   target == GDK_TARGET_STRING)
    {
      GtkSelectionData selection_data;

      selection_data.selection = GDK_NONE;
      selection_data.target = GDK_NONE;
      selection_data.type = GDK_NONE;
      selection_data.length = 0;
      selection_data.data = NULL;

      callback (gtkclipboard, &selection_data, user_data);
      return;
    }

  closure = g_new0 (ClipboardRequestClosure, 1);
  closure->clipboard = gtkclipboard;
  closure->cb = (GCallback)callback;
  closure->userdata = user_data;
  closure->target = target;

  /* TODO: Do we need to check that target is valid ? */
  mime_type = gdk_atom_name (target);

  gdk_wayland_device_request_selection_content (device,
                                                mime_type,
                                                _request_generic_cb,
                                                closure);

  g_free (mime_type);
}

static void
gtk_clipboard_wayland_owner_change (GtkClipboard        *clipboard,
                                    GdkEventOwnerChange *event)
{

}

static void
gtk_clipboard_wayland_set_can_store (GtkClipboard         *clipboard,
                                     const GtkTargetEntry *targets,
                                     gint                  n_targets)
{
  /* FIXME: Implement */
}

static void
gtk_clipboard_wayland_store (GtkClipboard *clipboard)
{
  /* FIXME: Implement */
}

#endif /* GDK_WINDOWING_WAYLAND */