Debian Codegraph @

subprojects/gtk-frdp/src/frdp-channel-clipboard.c

raw symbols SCIP
1 /* frdp-channel-clipboard.c
2 *
3 * Copyright (C) 2023 Marek Kasik <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "frdp-channel-clipboard.h"
20
21 #include <freerdp/freerdp.h>
22 #include <freerdp/client/cliprdr.h>
23
24 #define FUSE_USE_VERSION 35
25 #include <fuse_lowlevel.h>
26
27 #ifdef HAVE_FREERDP3
28 #define COMMON(x) common.x
29 #else
30 #define COMMON(x) x
31 #endif
32
33 #define FRDP_CLIPBOARD_FORMAT_PNG 0xD011
34 #define FRDP_CLIPBOARD_FORMAT_JPEG 0xD012
35 #define FRDP_CLIPBOARD_FORMAT_TEXT_URILIST 0xD014
36
37 typedef struct
38 {
39 FrdpChannelClipboard *self;
40 volatile gint finalized;
41 } FrdpClipboardDuration;
42
43 typedef struct
44 {
45 guchar *data;
46 guint32 length;
47 gboolean handled;
48 } FrdpClipboardResponseData;
49
50 typedef struct
51 {
52 guint count;
53 guint *requested_ids;
54 FrdpClipboardResponseData *responses;
55 } FrdpClipboardRequest;
56
57 typedef enum
58 {
59 FUSE_GETATTR_OP,
60 FUSE_LOOKUP_OP,
61 FUSE_READ_OP
62 } FrdpFuseOp;
63
64 typedef struct
65 {
66 gssize index;
67 fuse_req_t request;
68 FrdpFuseOp op;
69 } FrdpRemoteFileRequest;
70
71 typedef struct
72 {
73 gchar *uri;
74 FILEDESCRIPTORW *descriptor;
75 } FrdpLocalFileInfo;
76
77 typedef struct
78 {
79 guint clip_data_id;
80 gsize local_files_count;
81 FrdpLocalFileInfo *local_files_infos;
82 } FrdpLocalLockData;
83
84 typedef struct _FrdpRemoteFileInfo FrdpRemoteFileInfo;
85
86 struct _FrdpRemoteFileInfo
87 {
88 gchar *uri;
89 gchar *path;
90 gchar *filename;
91
92 guint stream_id;
93
94 gboolean is_directory;
95 gboolean is_readonly;
96
97 fuse_ino_t inode;
98 gssize parent_index; /* -1 means root directory */
99 GList *children;
100
101 gboolean has_size;
102 uint64_t size;
103 };
104
105 typedef struct
106 {
107 CliprdrClientContext *cliprdr_client_context;
108
109 gboolean file_streams_supported;
110
111 gboolean remote_data_in_clipboard;
112
113 GtkClipboard *gtk_clipboard;
114 guint clipboard_owner_changed_id;
115
116 GList *requests;
117
118 gsize remote_files_count;
119 FrdpRemoteFileInfo *remote_files_infos;
120 GHashTable *remote_files_requests;
121
122 gsize local_files_count;
123 FrdpLocalFileInfo *local_files_infos;
124
125 guint next_stream_id;
126 guint fgdw_id;
127
128 struct fuse_session *fuse_session;
129 GThread *fuse_session_thread;
130 gchar *fuse_directory;
131 GMutex fuse_mutex;
132
133 fuse_ino_t current_inode;
134
135 GList *locked_data; /* List of locked arrays of files - list of (FrdpLocalLockData *) */
136 GMutex lock_mutex;
137 gboolean pending_lock; /* Lock was requested right after format list has been sent */
138 guint pending_lock_id; /* Id for the pending lock */
139 gboolean awaiting_data_request; /* Format list has been send but data were not requested yet */
140
141 guint remote_clip_data_id; /* clipDataId for copying from remote side */
142
143 FrdpClipboardDuration *duration;
144 } FrdpChannelClipboardPrivate;
145
146 G_DEFINE_TYPE_WITH_PRIVATE (FrdpChannelClipboard, frdp_channel_clipboard, FRDP_TYPE_CHANNEL)
147
148 enum
149 {
150 PROP_0 = 0,
151 PROP_CLIPRDR_CLIENT_CONTEXT,
152 LAST_PROP
153 };
154
155 static void frdp_channel_clipboard_set_client_context (FrdpChannelClipboard *self,
156 CliprdrClientContext *context);
157 static guint send_client_format_list (FrdpChannelClipboard *self);
158
159 static void _gtk_clipboard_clear_func (GtkClipboard *clipboard,
160 gpointer user_data);
161 static void clipboard_owner_change_cb (GtkClipboard *clipboard,
162 GdkEventOwnerChange *event,
163 gpointer user_data);
164
165 static void frdp_local_lock_data_free (FrdpLocalLockData *lock_data);
166 static void lock_current_local_files (FrdpChannelClipboard *self,
167 guint clip_data_id);
168
169 static void
170 frdp_channel_clipboard_get_property (GObject *object,
171 guint property_id,
172 GValue *value,
173 GParamSpec *pspec)
174 {
175 FrdpChannelClipboard *self = FRDP_CHANNEL_CLIPBOARD (object);
176 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
177
178 switch (property_id)
179 {
180 case PROP_CLIPRDR_CLIENT_CONTEXT:
181 g_value_set_pointer (value, priv->cliprdr_client_context);
182 break;
183 default:
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185 break;
186 }
187 }
188
189 static void
190 frdp_channel_clipboard_set_property (GObject *object,
191 guint property_id,
192 const GValue *value,
193 GParamSpec *pspec)
194 {
195 FrdpChannelClipboard *self = FRDP_CHANNEL_CLIPBOARD (object);
196
197 switch (property_id)
198 {
199 case PROP_CLIPRDR_CLIENT_CONTEXT:
200 frdp_channel_clipboard_set_client_context (self, g_value_get_pointer (value));
201 break;
202 default:
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204 break;
205 }
206 }
207
208 static void
209 frdp_channel_clipboard_finalize (GObject *object)
210 {
211 FrdpChannelClipboard *self = (FrdpChannelClipboard *) object;
212 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
213
214 g_atomic_int_set (&priv->duration->finalized, 1);
215
216 g_signal_handler_disconnect (priv->gtk_clipboard,
217 priv->clipboard_owner_changed_id);
218
219 g_hash_table_unref (priv->remote_files_requests);
220 fuse_session_unmount (priv->fuse_session);
221 fuse_session_exit (priv->fuse_session);
222
223 if (priv->remote_data_in_clipboard)
224 gtk_clipboard_clear (priv->gtk_clipboard);
225
226 g_clear_pointer (&priv->fuse_directory, g_free);
227
228 g_mutex_lock (&priv->lock_mutex);
229
230 g_list_free_full (priv->locked_data, (GDestroyNotify) frdp_local_lock_data_free);
231 priv->locked_data = NULL;
232
233 g_mutex_unlock (&priv->lock_mutex);
234
235 g_thread_join (priv->fuse_session_thread);
236 g_mutex_clear (&priv->fuse_mutex);
237 g_mutex_clear (&priv->lock_mutex);
238
239 G_OBJECT_CLASS (frdp_channel_clipboard_parent_class)->finalize (object);
240 }
241
242 static void
243 frdp_channel_clipboard_class_init (FrdpChannelClipboardClass *klass)
244 {
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
246
247 gobject_class->get_property = frdp_channel_clipboard_get_property;
248 gobject_class->set_property = frdp_channel_clipboard_set_property;
249 gobject_class->finalize = frdp_channel_clipboard_finalize;
250
251 g_object_class_install_property (gobject_class,
252 PROP_CLIPRDR_CLIENT_CONTEXT,
253 g_param_spec_pointer ("cliprdr-client-context",
254 "cliprdr-client-context",
255 "Context for clipboard client",
256 G_PARAM_READWRITE));
257 }
258
259 static gssize
260 get_remote_file_info_index (FrdpChannelClipboard *self,
261 fuse_ino_t inode)
262 {
263 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
264 gssize result = -1, i;
265
266 for (i = 0; i < priv->remote_files_count; i++) {
267 if (priv->remote_files_infos[i].inode == inode) {
268 result = i;
269 break;
270 }
271 }
272
273 return result;
274 }
275
276 static void
277 get_root_attributes (struct stat *attr)
278 {
279 memset (attr, 0, sizeof (struct stat));
280
281 attr->st_ino = FUSE_ROOT_ID;
282 attr->st_mode = S_IFDIR | 0755;
283 attr->st_nlink = 2;
284 attr->st_uid = getuid ();
285 attr->st_gid = getgid ();
286 attr->st_atime = attr->st_mtime = attr->st_ctime = time (NULL);
287 }
288
289 static void
290 get_file_attributes (FrdpRemoteFileInfo info,
291 struct stat *attr)
292 {
293 memset (attr, 0, sizeof (struct stat));
294
295 attr->st_ino = info.inode;
296 if (info.is_directory) {
297 attr->st_mode = S_IFDIR | (info.is_readonly ? 0555 : 0755);
298 attr->st_nlink = 2;
299 } else {
300 attr->st_mode = S_IFREG | (info.is_readonly ? 0444 : 0644);
301 attr->st_nlink = 1;
302 attr->st_size = info.size;
303 }
304 attr->st_uid = getuid ();
305 attr->st_gid = getgid ();
306 attr->st_atime = attr->st_mtime = attr->st_ctime = time (NULL);
307 }
308
309 static void
310 request_size (FrdpChannelClipboard *self,
311 fuse_req_t request,
312 gsize index,
313 FrdpFuseOp op)
314 {
315 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = { 0 };
316 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
317 FrdpRemoteFileRequest *size_request;
318
319 file_contents_request.streamId = priv->next_stream_id++;
320 file_contents_request.listIndex = index;
321 file_contents_request.dwFlags = FILECONTENTS_SIZE;
322 file_contents_request.cbRequested = 8;
323 file_contents_request.nPositionHigh = 0;
324 file_contents_request.nPositionLow = 0;
325 file_contents_request.haveClipDataId = TRUE;
326 file_contents_request.clipDataId = priv->remote_clip_data_id;
327
328 size_request = g_new0 (FrdpRemoteFileRequest, 1);
329 size_request->index = index;
330 size_request->request = request;
331 size_request->op = op;
332
333 g_hash_table_insert (priv->remote_files_requests, GUINT_TO_POINTER (file_contents_request.streamId), size_request);
334
335 priv->cliprdr_client_context->ClientFileContentsRequest (priv->cliprdr_client_context, &file_contents_request);
336 }
337
338 static void
339 fuse_lookup (fuse_req_t request,
340 fuse_ino_t parent_inode,
341 const char *name)
342 {
343 FrdpChannelClipboard *self = fuse_req_userdata (request);
344 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
345 struct fuse_entry_param entry = {0};
346 gboolean found = FALSE;
347 gssize parent_index;
348 gsize i, child_index;
349 GList *iter;
350
351 g_mutex_lock (&priv->fuse_mutex);
352
353 if (parent_inode == FUSE_ROOT_ID) {
354 for (i = 0; i < priv->remote_files_count && !found; i++) {
355 if (priv->remote_files_infos[i].parent_index == -1 &&
356 g_str_equal (name, priv->remote_files_infos[i].filename)) {
357 found = TRUE;
358 if (priv->remote_files_infos[i].has_size ||
359 priv->remote_files_infos[i].is_directory) {
360 entry.ino = priv->remote_files_infos[i].inode;
361 get_file_attributes (priv->remote_files_infos[i], &entry.attr);
362 entry.attr_timeout = 1.0;
363 entry.entry_timeout = 1.0;
364
365 fuse_reply_entry (request, &entry);
366 } else {
367 request_size (self, request, i, FUSE_LOOKUP_OP);
368 }
369 }
370 }
371 } else {
372 parent_index = get_remote_file_info_index (self, parent_inode);
373 if (parent_index >= 0 && priv->remote_files_infos[parent_index].is_directory) {
374 for (iter = priv->remote_files_infos[parent_index].children; iter != NULL && !found; iter = iter->next) {
375 child_index = *((gsize *) iter->data);
376 if (g_str_equal (name, priv->remote_files_infos[child_index].filename)) {
377 found = TRUE;
378 if (priv->remote_files_infos[child_index].has_size ||
379 priv->remote_files_infos[child_index].is_directory) {
380 entry.ino = priv->remote_files_infos[child_index].inode;
381 get_file_attributes (priv->remote_files_infos[child_index], &entry.attr);
382 entry.attr_timeout = 1.0;
383 entry.entry_timeout = 1.0;
384
385 fuse_reply_entry (request, &entry);
386 } else {
387 request_size (self, request, child_index, FUSE_LOOKUP_OP);
388 }
389 }
390 }
391 }
392 }
393
394 if (!found)
395 fuse_reply_err (request, ENOENT);
396
397 g_mutex_unlock (&priv->fuse_mutex);
398 }
399
400 static void
401 fuse_getattr (fuse_req_t request,
402 fuse_ino_t inode,
403 struct fuse_file_info *file_info)
404 {
405 FrdpChannelClipboard *self = fuse_req_userdata (request);
406 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
407 struct stat attr = {0};
408 gssize index;
409
410 g_mutex_lock (&priv->fuse_mutex);
411
412 if (inode == FUSE_ROOT_ID) {
413 get_root_attributes (&attr);
414 fuse_reply_attr (request, &attr, 1);
415 } else {
416 index = get_remote_file_info_index (self, inode);
417 if (index >= 0) {
418 if (priv->remote_files_infos[index].has_size ||
419 priv->remote_files_infos[index].is_directory) {
420 get_file_attributes (priv->remote_files_infos[index], &attr);
421 fuse_reply_attr (request, &attr, 1);
422 } else {
423 request_size (self, request, index, FUSE_GETATTR_OP);
424 }
425 } else {
426 fuse_reply_err (request, ENOENT);
427 }
428 }
429
430 g_mutex_unlock (&priv->fuse_mutex);
431 }
432
433 static void
434 fuse_open (fuse_req_t request,
435 fuse_ino_t inode,
436 struct fuse_file_info *file_info)
437 {
438 FrdpChannelClipboard *self = fuse_req_userdata (request);
439 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
440 gssize index;
441
442 g_mutex_lock (&priv->fuse_mutex);
443
444 if (inode == FUSE_ROOT_ID) {
445 fuse_reply_err (request, EISDIR);
446 } else {
447 index = get_remote_file_info_index (self, inode);
448 if (index >= 0) {
449 if (priv->remote_files_infos[index].is_directory) {
450 fuse_reply_err (request, EISDIR);
451 } else {
452 file_info->direct_io = 1;
453 fuse_reply_open (request, file_info);
454 }
455 } else {
456 fuse_reply_err (request, ENOENT);
457 }
458 }
459
460 g_mutex_unlock (&priv->fuse_mutex);
461 }
462
463 static void
464 fuse_read (fuse_req_t request,
465 fuse_ino_t inode,
466 size_t size,
467 off_t offset,
468 struct fuse_file_info *file_info)
469 {
470 FrdpChannelClipboard *self = fuse_req_userdata (request);
471 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
472 gssize index;
473
474 g_mutex_lock (&priv->fuse_mutex);
475
476 index = get_remote_file_info_index (self, inode);
477 if (index >= 0) {
478 if (priv->remote_files_infos[index].is_directory) {
479 fuse_reply_err (request, EISDIR);
480 } else {
481 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = { 0 };
482 FrdpRemoteFileRequest *data_request;
483
484 size = MIN (size, 8 * 1024 * 1024);
485 g_assert (size > 0);
486
487 file_contents_request.streamId = priv->next_stream_id++;
488 file_contents_request.listIndex = index;
489 file_contents_request.dwFlags = FILECONTENTS_RANGE;
490 file_contents_request.cbRequested = size;
491 file_contents_request.nPositionHigh = offset >> 32;
492 file_contents_request.nPositionLow = offset & 0xffffffff;
493 file_contents_request.haveClipDataId = TRUE;
494 file_contents_request.clipDataId = priv->remote_clip_data_id;
495
496 data_request = g_new0 (FrdpRemoteFileRequest, 1);
497 data_request->index = index;
498 data_request->request = request;
499 data_request->op = FUSE_READ_OP;
500
501 g_hash_table_insert (priv->remote_files_requests, GUINT_TO_POINTER (file_contents_request.streamId), data_request);
502
503 priv->cliprdr_client_context->ClientFileContentsRequest (priv->cliprdr_client_context, &file_contents_request);
504 }
505 } else {
506 fuse_reply_err (request, ENOENT);
507 }
508
509 g_mutex_unlock (&priv->fuse_mutex);
510 }
511
512 static void
513 fuse_opendir (fuse_req_t request,
514 fuse_ino_t inode,
515 struct fuse_file_info *file_info)
516 {
517 FrdpChannelClipboard *self = fuse_req_userdata (request);
518 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
519 gssize index;
520
521 g_mutex_lock (&priv->fuse_mutex);
522
523 if (inode == FUSE_ROOT_ID) {
524 fuse_reply_open (request, file_info);
525 } else {
526 index = get_remote_file_info_index (self, inode);
527 if (index >= 0) {
528 if (priv->remote_files_infos[index].is_directory) {
529 fuse_reply_open (request, file_info);
530 } else {
531 fuse_reply_err (request, ENOTDIR);
532 }
533 } else {
534 fuse_reply_err (request, ENOENT);
535 }
536 }
537
538 g_mutex_unlock (&priv->fuse_mutex);
539 }
540
541 static void
542 fuse_readdir (fuse_req_t request,
543 fuse_ino_t inode,
544 size_t size,
545 off_t offset,
546 struct fuse_file_info *file_info)
547 {
548 FrdpChannelClipboard *self = fuse_req_userdata (request);
549 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
550 struct stat attr = {0};
551 gboolean done = FALSE;
552 gssize index, i, j;
553 GList *iter;
554 gsize written = 0, entry_size, child_index;
555 char *buffer;
556
557 buffer = g_malloc0 (size);
558
559 g_mutex_lock (&priv->fuse_mutex);
560
561 if (inode == FUSE_ROOT_ID) {
562 j = -1;
563 for (i = 0; i < priv->remote_files_count; i++) {
564 if (priv->remote_files_infos[i].parent_index == -1) {
565 j++;
566 if (j <= offset && offset > 0)
567 continue;
568
569 get_file_attributes (priv->remote_files_infos[i], &attr);
570
571 entry_size = fuse_add_direntry (request, buffer + written,
572 size - written,
573 priv->remote_files_infos[i].filename, &attr, j + 1);
574
575 if (entry_size > size - written)
576 break;
577
578 written += entry_size;
579
580 if (i == priv->remote_files_count - 1)
581 done = TRUE;
582 }
583 }
584 fuse_reply_buf (request, buffer, written);
585 } else {
586 index = get_remote_file_info_index (self, inode);
587 if (index >= 0) {
588 if (priv->remote_files_infos[index].is_directory) {
589 for (iter = priv->remote_files_infos[index].children, i = 0; iter != NULL; iter = iter->next, i++) {
590 child_index = *((gsize *) iter->data);
591
592 if (i <= offset && offset > 0)
593 continue;
594
595 get_file_attributes (priv->remote_files_infos[child_index], &attr);
596
597 entry_size = fuse_add_direntry (request, buffer + written,
598 size - written,
599 priv->remote_files_infos[child_index].filename, &attr, i + 1);
600
601 if (entry_size > size - written)
602 break;
603
604 written += entry_size;
605
606 if (iter == NULL)
607 done = TRUE;
608 }
609
610 fuse_reply_buf (request, buffer, written);
611 } else {
612 fuse_reply_err (request, ENOTDIR);
613 }
614 } else {
615 fuse_reply_err (request, ENOENT);
616 }
617 }
618
619 if (done)
620 fuse_reply_buf (request, NULL, 0);
621
622 g_mutex_unlock (&priv->fuse_mutex);
623
624 g_free (buffer);
625 }
626
627 static const struct fuse_lowlevel_ops fuse_ops =
628 {
629 .lookup = fuse_lookup,
630 .getattr = fuse_getattr,
631 .open = fuse_open,
632 .read = fuse_read,
633 .opendir = fuse_opendir,
634 .readdir = fuse_readdir,
635 };
636
637 static gpointer
638 fuse_session_thread_func (gpointer data)
639 {
640 FrdpChannelClipboard *self = data;
641 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
642 gint result;
643
644 g_mutex_lock (&priv->fuse_mutex);
645 fuse_session_mount (priv->fuse_session,
646 priv->fuse_directory);
647
648 fuse_daemonize (1);
649 g_mutex_unlock (&priv->fuse_mutex);
650
651 result = fuse_session_loop (priv->fuse_session);
652
653 g_mutex_lock (&priv->fuse_mutex);
654 fuse_session_unmount (priv->fuse_session);
655 g_mutex_unlock (&priv->fuse_mutex);
656
657 return NULL;
658 }
659
660 static void
661 frdp_channel_clipboard_init (FrdpChannelClipboard *self)
662 {
663 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
664 struct fuse_args args = {0};
665 gchar *argv[2];
666
667 priv->gtk_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
668 priv->clipboard_owner_changed_id = g_signal_connect (priv->gtk_clipboard, "owner-change", G_CALLBACK (clipboard_owner_change_cb), self);
669 priv->fgdw_id = FRDP_CLIPBOARD_FORMAT_TEXT_URILIST;
670 priv->current_inode = FUSE_ROOT_ID + 1;
671 priv->locked_data = NULL;
672 priv->pending_lock = FALSE;
673 priv->remote_clip_data_id = 0;
674
675 argv[0] = "gnome-connections";
676 argv[1] = "-d";
677 args.argc = 1; /* Set to 2 to see debug logs of Fuse */
678 args.argv = argv;
679
680 priv->remote_files_requests = g_hash_table_new (g_direct_hash, g_direct_equal);
681
682 g_mutex_init (&priv->fuse_mutex);
683 g_mutex_init (&priv->lock_mutex);
684
685 priv->fuse_directory = g_mkdtemp (g_strdup_printf ("%s/clipboard-XXXXXX/", g_get_user_runtime_dir ()));
686
687 priv->fuse_session = fuse_session_new (&args, &fuse_ops, sizeof (fuse_ops), self);
688 if (priv->fuse_session != NULL) {
689 priv->fuse_session_thread = g_thread_new ("RDP FUSE session thread",
690 fuse_session_thread_func,
691 self);
692 } else {
693 g_warning ("Could not initiate FUSE session\n");
694 }
695
696 priv->duration = g_new0 (FrdpClipboardDuration, 1);
697 priv->duration->self = self;
698 g_atomic_int_set (&priv->duration->finalized, 0);
699 }
700
701 static void
702 clipboard_owner_change_cb (GtkClipboard *clipboard,
703 GdkEventOwnerChange *event,
704 gpointer user_data)
705 {
706 FrdpChannelClipboard *self = (FrdpChannelClipboard *) user_data;
707 FrdpChannelClipboardPrivate *priv;
708
709 if (self != NULL) {
710 priv = frdp_channel_clipboard_get_instance_private (self);
711
712 if ((gtk_clipboard_wait_is_text_available (clipboard) ||
713 gtk_clipboard_wait_is_image_available (clipboard) ||
714 gtk_clipboard_wait_is_uris_available (clipboard)) &&
715 !priv->remote_data_in_clipboard) {
716 send_client_format_list (self);
717 }
718 }
719 }
720
721 static guint
722 send_client_capabilities (FrdpChannelClipboard *self)
723 {
724 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
725 CLIPRDR_GENERAL_CAPABILITY_SET general_capability_set = { 0 };
726 CLIPRDR_CAPABILITIES capabilities = { 0 };
727
728 capabilities.cCapabilitiesSets = 1;
729 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET *) &(general_capability_set);
730
731 general_capability_set.capabilitySetType = CB_CAPSTYPE_GENERAL;
732 general_capability_set.capabilitySetLength = 12;
733 general_capability_set.version = CB_CAPS_VERSION_2;
734 general_capability_set.generalFlags = CB_USE_LONG_FORMAT_NAMES |
735 CB_STREAM_FILECLIP_ENABLED |
736 CB_CAN_LOCK_CLIPDATA |
737 CB_FILECLIP_NO_FILE_PATHS |
738 CB_HUGE_FILE_SUPPORT_ENABLED;
739
740 return priv->cliprdr_client_context->ClientCapabilities (priv->cliprdr_client_context, &capabilities);
741 }
742
743 static guint
744 send_client_format_list (FrdpChannelClipboard *self)
745 {
746 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
747 CLIPRDR_FORMAT_LIST format_list = { 0 };
748 CLIPRDR_FORMAT *formats = NULL;
749 guint32 formats_count = 0;
750 GdkAtom *targets = NULL;
751 gchar *atom_name;
752 guint ret = CHANNEL_RC_NOT_INITIALIZED, k;
753 gint targets_count = 0;
754 gint i, j = 0;
755
756 /* TODO - change to gtk_clipboard_request_targets() */
757 if (gtk_clipboard_wait_for_targets (priv->gtk_clipboard,
758 &targets,
759 &targets_count)) {
760 formats_count = targets_count;
761 formats = g_new0 (CLIPRDR_FORMAT, formats_count);
762
763 for (i = 0; i < targets_count; i++) {
764 atom_name = gdk_atom_name (targets[i]);
765
766 if (g_strcmp0 (atom_name, "UTF8_STRING") == 0) {
767 formats[j].formatId = CF_UNICODETEXT;
768 formats[j++].formatName = NULL;
769 } else if (g_strcmp0 (atom_name, "TEXT") == 0) {
770 formats[j].formatId = CF_TEXT;
771 formats[j++].formatName = NULL;
772 } else if (g_strcmp0 (atom_name, "image/png") == 0) {
773 formats[j].formatId = FRDP_CLIPBOARD_FORMAT_PNG;
774 formats[j++].formatName = NULL;
775 } else if (g_strcmp0 (atom_name, "image/jpeg") == 0) {
776 formats[j].formatId = FRDP_CLIPBOARD_FORMAT_JPEG;
777 formats[j++].formatName = NULL;
778 } else if (g_strcmp0 (atom_name, "image/bmp") == 0) {
779 formats[j].formatId = CF_DIB;
780 formats[j++].formatName = NULL;
781 } else if (g_strcmp0 (atom_name, "text/uri-list") == 0) {
782 formats[j].formatId = priv->fgdw_id;
783 formats[j++].formatName = g_strdup ("FileGroupDescriptorW");
784 }
785
786 g_free (atom_name);
787 }
788 }
789
790 format_list.COMMON(msgType) = CB_FORMAT_LIST;
791 format_list.numFormats = j;
792 format_list.formats = formats;
793
794 priv->awaiting_data_request = TRUE;
795 ret = priv->cliprdr_client_context->ClientFormatList (priv->cliprdr_client_context, &format_list);
796
797 if (formats != NULL) {
798 for (k = 0; k < formats_count; k++) {
799 g_free (formats[k].formatName);
800 }
801 g_free (formats);
802 }
803
804 return ret;
805 }
806
807 static guint
808 server_capabilities (CliprdrClientContext *context,
809 const CLIPRDR_CAPABILITIES *capabilities)
810 {
811 FrdpChannelClipboard *self;
812 FrdpChannelClipboardPrivate *priv;
813 CLIPRDR_CAPABILITY_SET *capability;
814 guint i;
815
816 if (context != NULL) {
817 self = (FrdpChannelClipboard *) context->custom;
818 priv = frdp_channel_clipboard_get_instance_private (self);
819
820 for (i = 0; i < capabilities->cCapabilitiesSets; i++) {
821 capability = &(capabilities->capabilitySets[i]);
822
823 if (capability->capabilitySetType == CB_CAPSTYPE_GENERAL) {
824 CLIPRDR_GENERAL_CAPABILITY_SET *general_capability = (CLIPRDR_GENERAL_CAPABILITY_SET *) capability;
825
826 /* Windows 7 does not send files if these flags are disabled. */
827 if (general_capability->generalFlags & CB_USE_LONG_FORMAT_NAMES &&
828 general_capability->generalFlags & CB_STREAM_FILECLIP_ENABLED &&
829 general_capability->generalFlags & CB_FILECLIP_NO_FILE_PATHS) {
830 priv->file_streams_supported = TRUE;
831 }
832 }
833 }
834 }
835
836 return CHANNEL_RC_OK;
837 }
838
839 static guint
840 send_data_request (FrdpChannelClipboard *self,
841 guint32 format_id)
842 {
843 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
844 CLIPRDR_FORMAT_DATA_REQUEST *new_request;
845
846 new_request = g_new0 (CLIPRDR_FORMAT_DATA_REQUEST, 1);
847 new_request->requestedFormatId = format_id;
848
849 return priv->cliprdr_client_context->ClientFormatDataRequest (priv->cliprdr_client_context, new_request);
850 }
851
852 static FrdpClipboardRequest *
853 frdp_clipboard_request_new (guint count)
854 {
855 FrdpClipboardRequest *request;
856
857 request = g_new0 (FrdpClipboardRequest, 1);
858 request->count = count;
859 request->requested_ids = g_new0 (guint, count);
860 request->responses = g_new0 (FrdpClipboardResponseData, count);
861
862 return request;
863 }
864
865 static FrdpClipboardRequest *
866 frdp_clipboard_request_send (FrdpChannelClipboard *self,
867 guint format_id)
868 {
869 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
870 FrdpClipboardRequest *request = NULL;
871 guint i;
872
873 if (format_id == priv->fgdw_id) {
874 request = frdp_clipboard_request_new (1);
875 request->requested_ids[0] = priv->fgdw_id;
876 } else if (format_id == CF_UNICODETEXT) {
877 request = frdp_clipboard_request_new (1);
878 request->requested_ids[0] = CF_UNICODETEXT;
879 } else if (format_id == CF_DIB) {
880 request = frdp_clipboard_request_new (1);
881 request->requested_ids[0] = CF_DIB;
882 }
883
884 if (request != NULL) {
885 priv->requests = g_list_append (priv->requests, request);
886 for (i = 0; i < request->count; i++) {
887 send_data_request (self, request->requested_ids[i]);
888 }
889 }
890
891 return request;
892 }
893
894 static gboolean
895 frdp_clipboard_request_done (FrdpClipboardRequest *request)
896 {
897 guint i;
898
899 for (i = 0; i < request->count; i++) {
900 if (!request->responses[i].handled) {
901 return FALSE;
902 }
903 }
904
905 return TRUE;
906 }
907
908 static void
909 frdp_clipboard_request_free (FrdpClipboardRequest *request)
910 {
911 guint i;
912
913 g_free (request->requested_ids);
914 for (i = 0; i < request->count; i++)
915 g_free (request->responses[i].data);
916 g_free (request->responses);
917 g_free (request);
918 }
919
920 static guint
921 send_client_format_list_response (FrdpChannelClipboard *self,
922 gboolean status)
923 {
924 CLIPRDR_FORMAT_LIST_RESPONSE response = { 0 };
925 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
926
927 response.COMMON(msgType) = CB_FORMAT_LIST_RESPONSE;
928 response.COMMON(msgFlags) = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
929 response.COMMON(dataLen) = 0;
930
931 return priv->cliprdr_client_context->ClientFormatListResponse (priv->cliprdr_client_context, &response);
932 }
933
934 static void
935 replace_ascii_character (gchar *text,
936 gchar old_character,
937 gchar new_character)
938 {
939 guint i;
940
941 for (i = 0; text[i] != '\0'; i++) {
942 if (text[i] == old_character)
943 text[i] = new_character;
944 }
945 }
946
947 static WCHAR *
948 convert_to_unicode (const gchar *text)
949 {
950 WCHAR *result = NULL;
951
952 if (text != NULL) {
953 #ifdef HAVE_FREERDP3
954 result = ConvertUtf8ToWCharAlloc (text, NULL);
955 #else
956 ConvertToUnicode (CP_UTF8, 0, (LPCSTR) text, -1, &result, 0);
957 #endif
958 }
959
960 return result;
961 }
962
963 static gchar *
964 convert_from_unicode (const WCHAR *text,
965 gint text_length)
966 {
967 gchar *result = NULL;
968
969 if (text != NULL) {
970 #ifdef HAVE_FREERDP3
971 result = ConvertWCharNToUtf8Alloc (text, text_length, NULL);
972 #else
973 ConvertFromUnicode (CP_UTF8, 0, text, text_length, &result, 0, NULL, NULL);
974 #endif
975 }
976
977 return result;
978 }
979
980 /* TODO: Rewrite this using async methods of GtkCLipboard once we move to Gtk4 */
981 static void
982 _gtk_clipboard_get_func (GtkClipboard *clipboard,
983 GtkSelectionData *selection_data,
984 guint info,
985 gpointer user_data)
986 {
987 CLIPRDR_LOCK_CLIPBOARD_DATA lock_clipboard_data = { 0 };
988 FrdpChannelClipboard *self = (FrdpChannelClipboard *) user_data;
989 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
990 FrdpClipboardRequest *current_request;
991 gchar *data = NULL;
992 gint length;
993
994 lock_clipboard_data.COMMON(msgType) = CB_LOCK_CLIPDATA;
995 lock_clipboard_data.COMMON(msgFlags) = 0;
996 lock_clipboard_data.COMMON(dataLen) = 4;
997 lock_clipboard_data.clipDataId = ++priv->remote_clip_data_id;
998 priv->cliprdr_client_context->ClientLockClipboardData (priv->cliprdr_client_context, &lock_clipboard_data);
999
1000 current_request = frdp_clipboard_request_send (self, info);
1001 if (current_request != NULL) {
1002
1003 while (!frdp_clipboard_request_done (current_request))
1004 gtk_main_iteration ();
1005
1006 if (info == CF_UNICODETEXT) {
1007 /* TODO - convert CR LF to CR */
1008 data = convert_from_unicode ((WCHAR *) current_request->responses[0].data, current_request->responses[0].length / sizeof (WCHAR));
1009 if (data != NULL) {
1010 length = strlen (data);
1011 gtk_selection_data_set (selection_data,
1012 gdk_atom_intern ("UTF8_STRING", FALSE),
1013 8,
1014 (guchar *) data,
1015 length);
1016 }
1017 } else if (info == CF_DIB) {
1018 /* This has been inspired by function transmute_cf_dib_to_image_bmp() from gtk */
1019 BITMAPINFOHEADER *bi = (BITMAPINFOHEADER *) current_request->responses[0].data;
1020 BITMAPFILEHEADER *bf;
1021
1022 length = current_request->responses[0].length + sizeof (BITMAPFILEHEADER);
1023 data = g_malloc (length);
1024
1025 bf = (BITMAPFILEHEADER *) data;
1026 bf->bfType = 0x4d42; /* "BM" */
1027 bf->bfSize = length;
1028 bf->bfReserved1 = 0;
1029 bf->bfReserved2 = 0;
1030 bf->bfOffBits = (sizeof (BITMAPFILEHEADER) + bi->biSize/* + bi->biClrUsed * sizeof (RGBQUAD)*/);
1031
1032 memcpy (data + sizeof (BITMAPFILEHEADER),
1033 current_request->responses[0].data,
1034 current_request->responses[0].length);
1035
1036 gtk_selection_data_set (selection_data,
1037 gdk_atom_intern ("image/bmp", FALSE),
1038 8,
1039 (guchar *) data,
1040 length);
1041 } else if (info == priv->fgdw_id) {
1042 for (guint j = 0; j < current_request->count; j++) {
1043 if (current_request->requested_ids[j] == priv->fgdw_id) {
1044 FILEDESCRIPTORW *files = (FILEDESCRIPTORW *) (current_request->responses[j].data + 4);
1045 GList *iter, *uri_list = NULL;
1046 gchar *path, **uri_array, *tmps, *slash, *dir;
1047 guint i, count = current_request->responses[j].length / sizeof (FILEDESCRIPTORW);
1048
1049 g_mutex_lock (&priv->fuse_mutex);
1050
1051 priv->remote_files_count = count;
1052 priv->remote_files_infos = g_new0 (FrdpRemoteFileInfo, priv->remote_files_count);
1053
1054 for (i = 0; i < count; i++) {
1055 path = convert_from_unicode ((WCHAR *) files[i].cFileName, 260 / sizeof (WCHAR));
1056
1057 replace_ascii_character (path, '\\', '/');
1058
1059 priv->remote_files_infos[i].path = g_strdup (path);
1060 priv->remote_files_infos[i].is_directory = (files[i].dwFlags & FD_ATTRIBUTES) && (files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1061 priv->remote_files_infos[i].is_readonly = (files[i].dwFlags & FD_ATTRIBUTES) && (files[i].dwFileAttributes & FILE_ATTRIBUTE_READONLY);
1062 priv->remote_files_infos[i].inode = priv->current_inode++;
1063 priv->remote_files_infos[i].uri = g_strdup_printf ("file://%s/%s%s", priv->fuse_directory, path, priv->remote_files_infos[i].is_directory ? "/" : "");
1064 if (files[i].dwFlags & FD_FILESIZE) {
1065 priv->remote_files_infos[i].size = ((guint64) files[i].nFileSizeHigh << 32) + files[i].nFileSizeLow;
1066 priv->remote_files_infos[i].has_size = TRUE;
1067 }
1068 priv->remote_files_infos[i].parent_index = -1;
1069
1070 g_free (path);
1071 }
1072
1073 for (i = 0; i < count; i++) {
1074 slash = NULL;
1075
1076 tmps = g_strdup (priv->remote_files_infos[i].uri);
1077 if (priv->remote_files_infos[i].is_directory) {
1078 if (g_str_has_suffix (tmps, "/"))
1079 tmps[strlen (tmps) - 1] = '\0';
1080 }
1081 slash = g_strrstr (tmps, "/");
1082
1083 dir = NULL;
1084 if (slash != NULL) {
1085
1086 if (strlen (slash) > 1) {
1087 priv->remote_files_infos[i].filename = g_strdup (slash + 1);
1088 slash[1] = '\0';
1089 dir = g_strdup (tmps);
1090 }
1091
1092 if (dir != NULL) {
1093 if (g_str_equal (dir, priv->fuse_directory)) {
1094 } else {
1095 for (j = 0; j < count; j++) {
1096 if (g_str_equal (dir, priv->remote_files_infos[j].uri)) {
1097 gsize *child_index;
1098 priv->remote_files_infos[i].parent_index = j;
1099
1100 child_index = g_new (gsize, 1);
1101 *child_index = i;
1102 priv->remote_files_infos[j].children = g_list_append (priv->remote_files_infos[j].children, child_index);
1103 priv->remote_files_infos[i].parent_index = j;
1104 break;
1105 }
1106 }
1107 }
1108 g_free (dir);
1109 }
1110 }
1111 g_free (tmps);
1112 }
1113
1114 /* Set URIs for topmost items only, the rest will be pasted as part of those. */
1115 for (i = 0; i < priv->remote_files_count; i++) {
1116 if (priv->remote_files_infos[i].parent_index < 0) {
1117 uri_list = g_list_prepend (uri_list, priv->remote_files_infos[i].uri);
1118 }
1119 }
1120
1121 g_mutex_unlock (&priv->fuse_mutex);
1122
1123 uri_array = g_new0 (gchar *, g_list_length (uri_list) + 1);
1124 for (iter = uri_list, i = 0; iter != NULL; iter = iter->next, i++)
1125 uri_array[i] = iter->data;
1126
1127 gtk_selection_data_set_uris (selection_data, uri_array);
1128
1129 g_free (uri_array);
1130 g_list_free (uri_list);
1131 }
1132 }
1133 }
1134
1135 priv->requests = g_list_remove (priv->requests, current_request);
1136 frdp_clipboard_request_free (current_request);
1137 }
1138 }
1139
1140 static void
1141 clear_local_files_infos (FrdpChannelClipboard *self)
1142 {
1143 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1144 guint i;
1145
1146 if (priv->local_files_infos != NULL) {
1147 for (i = 0; i < priv->local_files_count; i++) {
1148 g_free (priv->local_files_infos[i].descriptor);
1149 g_free (priv->local_files_infos[i].uri);
1150 }
1151 g_clear_pointer (&priv->local_files_infos, g_free);
1152 }
1153
1154 priv->local_files_count = 0;
1155 }
1156
1157 static void
1158 _gtk_clipboard_clear_func (GtkClipboard *clipboard,
1159 gpointer user_data)
1160 {
1161 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlock_clipboard_data = { 0 };
1162 FrdpChannelClipboard *self = (FrdpChannelClipboard *) user_data;
1163 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1164 guint i;
1165
1166 g_mutex_lock (&priv->fuse_mutex);
1167
1168 if (priv->remote_files_infos != NULL) {
1169 for (i = 0; i < priv->remote_files_count; i++) {
1170 g_free (priv->remote_files_infos[i].uri);
1171 g_free (priv->remote_files_infos[i].path);
1172 g_free (priv->remote_files_infos[i].filename);
1173 g_list_free_full (priv->remote_files_infos[i].children, g_free);
1174 }
1175 g_clear_pointer (&priv->remote_files_infos, g_free);
1176 }
1177 priv->remote_files_count = 0;
1178
1179 g_mutex_unlock (&priv->fuse_mutex);
1180
1181 unlock_clipboard_data.COMMON(msgType) = CB_UNLOCK_CLIPDATA;
1182 unlock_clipboard_data.COMMON(msgFlags) = 0;
1183 unlock_clipboard_data.COMMON(dataLen) = 4;
1184 unlock_clipboard_data.clipDataId = priv->remote_clip_data_id;
1185 priv->cliprdr_client_context->ClientUnlockClipboardData (priv->cliprdr_client_context, &unlock_clipboard_data);
1186
1187 clear_local_files_infos (self);
1188
1189 priv->remote_data_in_clipboard = FALSE;
1190 }
1191
1192 static guint
1193 server_format_list (CliprdrClientContext *context,
1194 const CLIPRDR_FORMAT_LIST *format_list)
1195 {
1196 FrdpChannelClipboard *self;
1197 FrdpChannelClipboardPrivate *priv;
1198 GtkTargetEntry *entries;
1199 GtkTargetList *list;
1200 gboolean contains_file_group_descriptor_w = FALSE;
1201 GdkAtom atom;
1202 guint i;
1203 gint count = 0;
1204
1205 if (context != NULL) {
1206 self = (FrdpChannelClipboard *) context->custom;
1207 priv = frdp_channel_clipboard_get_instance_private (self);
1208
1209 list = gtk_target_list_new (NULL, 0);
1210
1211 for (i = 0; i < format_list->numFormats; i++) {
1212 if (g_strcmp0 (format_list->formats[i].formatName, "FileGroupDescriptorW") == 0) {
1213 contains_file_group_descriptor_w = TRUE;
1214 priv->fgdw_id = format_list->formats[i].formatId;
1215 }
1216 }
1217
1218 if (contains_file_group_descriptor_w) {
1219 atom = gdk_atom_intern ("text/uri-list", FALSE);
1220 gtk_target_list_add (list, atom, 0, priv->fgdw_id);
1221 } else {
1222 atom = GDK_NONE;
1223 for (i = 0; i < format_list->numFormats; i++) {
1224 if (format_list->formats[i].formatId == CF_TEXT) {
1225 atom = gdk_atom_intern ("TEXT", FALSE);
1226 } else if (format_list->formats[i].formatId == CF_UNICODETEXT) {
1227 atom = gdk_atom_intern ("UTF8_STRING", FALSE);
1228 } else if (format_list->formats[i].formatId == CF_DIB) {
1229 atom = gdk_atom_intern ("image/bmp", FALSE);
1230 } else if (format_list->formats[i].formatId == FRDP_CLIPBOARD_FORMAT_PNG) {
1231 atom = gdk_atom_intern ("image/png", FALSE);
1232 }
1233
1234 if (atom != GDK_NONE)
1235 gtk_target_list_add (list, atom, 0, format_list->formats[i].formatId);
1236 }
1237 }
1238
1239 entries = gtk_target_table_new_from_list (list, &count);
1240 if (!gtk_clipboard_set_with_data (priv->gtk_clipboard,
1241 entries,
1242 count,
1243 _gtk_clipboard_get_func,
1244 _gtk_clipboard_clear_func,
1245 self)) {
1246 g_warning ("Setting of clipboard entries failed");
1247 } else {
1248 priv->remote_data_in_clipboard = TRUE;
1249 }
1250
1251 send_client_format_list_response (self, TRUE);
1252 }
1253
1254 return CHANNEL_RC_OK;
1255 }
1256
1257 static guint
1258 monitor_ready (CliprdrClientContext *context,
1259 const CLIPRDR_MONITOR_READY *monitor_ready)
1260 {
1261 FrdpChannelClipboard *clipboard;
1262 guint return_value = CHANNEL_RC_OK;
1263
1264 if (context != NULL) {
1265 clipboard = (FrdpChannelClipboard *) context->custom;
1266
1267 if ((return_value = send_client_capabilities (clipboard)) != CHANNEL_RC_OK)
1268 return return_value;
1269
1270 if ((return_value = send_client_format_list (clipboard)) != CHANNEL_RC_OK)
1271 return return_value;
1272 }
1273
1274 return return_value;
1275 }
1276
1277 static guint
1278 server_format_list_response (CliprdrClientContext *context,
1279 const CLIPRDR_FORMAT_LIST_RESPONSE *response)
1280 {
1281 return CHANNEL_RC_OK;
1282 }
1283
1284 static FrdpLocalFileInfo *
1285 frdp_local_file_info_new (GFile *file,
1286 GFileInfo *file_info,
1287 GFile *root)
1288 {
1289 FrdpLocalFileInfo *frdp_file_info = NULL;
1290 GFileType file_type;
1291 goffset file_size;
1292 WCHAR *file_name = NULL;
1293 gchar *relative_path;
1294
1295 if (file_info != NULL && file != NULL) {
1296 frdp_file_info = g_new (FrdpLocalFileInfo, 1);
1297 frdp_file_info->uri = g_file_get_uri (file);
1298 frdp_file_info->descriptor = g_new0 (FILEDESCRIPTORW, 1);
1299
1300 relative_path = g_file_get_relative_path (root, file);
1301 replace_ascii_character (relative_path, '/', '\\');
1302
1303 file_name = convert_to_unicode (relative_path);
1304 if (file_name != NULL) {
1305 memcpy (frdp_file_info->descriptor->cFileName, file_name, strlen (relative_path) * 2);
1306 g_free (file_name);
1307 }
1308 g_free (relative_path);
1309
1310 file_size = g_file_info_get_size (file_info);
1311 file_type = g_file_info_get_file_type (file_info);
1312
1313 frdp_file_info->descriptor->dwFlags = FD_ATTRIBUTES | FD_FILESIZE;
1314 if (file_type == G_FILE_TYPE_DIRECTORY) {
1315 frdp_file_info->descriptor->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1316 frdp_file_info->descriptor->nFileSizeLow = 0;
1317 frdp_file_info->descriptor->nFileSizeHigh = 0;
1318 } else {
1319 frdp_file_info->descriptor->dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1320 frdp_file_info->descriptor->nFileSizeLow = file_size & 0xffffffff;
1321 frdp_file_info->descriptor->nFileSizeHigh = file_size >> 32 & 0xffffffff;
1322 }
1323 }
1324
1325 return frdp_file_info;
1326 }
1327
1328 static void
1329 enumerate_directory (GFile *directory,
1330 GList **infos,
1331 GFile *root)
1332 {
1333 FrdpLocalFileInfo *frdp_file_info;
1334 GFileEnumerator *enumerator;
1335 GFileInfo *file_info;
1336 GError *error = NULL;
1337 GFile *file;
1338 GList *list = NULL;
1339
1340 enumerator = g_file_enumerate_children (directory,
1341 G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
1342 G_FILE_QUERY_INFO_NONE,
1343 NULL,
1344 &error);
1345
1346 while (TRUE) {
1347 if (!g_file_enumerator_iterate (enumerator, &file_info, &file, NULL, &error)) {
1348 g_warning ("Enumeration of files failed: %s", error->message);
1349 g_error_free (error);
1350 break;
1351 }
1352
1353 if (file_info == NULL || file == NULL)
1354 break;
1355
1356 frdp_file_info = frdp_local_file_info_new (file, file_info, root);
1357 list = g_list_append (list, frdp_file_info);
1358
1359 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
1360 enumerate_directory (file, &list, root);
1361 }
1362
1363 g_object_unref (enumerator);
1364
1365 *infos = g_list_concat (*infos, list);
1366 }
1367
1368 static guint
1369 send_data_response (FrdpChannelClipboard *self,
1370 const BYTE *data,
1371 size_t size)
1372 {
1373 CLIPRDR_FORMAT_DATA_RESPONSE response = { 0 };
1374 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1375
1376 if (size > UINT32_MAX)
1377 return ERROR_INVALID_PARAMETER;
1378
1379 response.COMMON(msgFlags) = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1380 response.COMMON(dataLen) = (guint32) size;
1381 response.requestedFormatData = data;
1382
1383 return priv->cliprdr_client_context->ClientFormatDataResponse (priv->cliprdr_client_context, &response);
1384 }
1385
1386 static void
1387 clipboard_content_received (GtkClipboard *clipboard,
1388 GtkSelectionData *selection_data,
1389 gpointer user_data)
1390 {
1391 FrdpClipboardDuration *duration = (FrdpClipboardDuration *) user_data;
1392 FrdpChannelClipboard *self;
1393 FrdpChannelClipboardPrivate *priv;
1394 GdkPixbuf *pixbuf;
1395 GdkAtom data_type;
1396 guchar *data, *text;
1397 GError *error = NULL;
1398 gsize text_length, buffer_size = 0;
1399 guint i;
1400 gint length;
1401
1402 if (g_atomic_int_get (&duration->finalized))
1403 return;
1404
1405 self = (FrdpChannelClipboard *) duration->self;
1406 priv = frdp_channel_clipboard_get_instance_private (self);
1407
1408 length = gtk_selection_data_get_length (selection_data);
1409 data_type = gtk_selection_data_get_data_type (selection_data);
1410
1411 if (length > 0) {
1412 if (data_type == gdk_atom_intern ("UTF8_STRING", FALSE)) {
1413 text = gtk_selection_data_get_text (selection_data);
1414 text_length = strlen ((gchar *) text);
1415
1416 data = (guchar *) convert_to_unicode ((gchar *) text);
1417 if (data != NULL) {
1418 send_data_response (self, data, (text_length + 1) * sizeof (WCHAR));
1419 g_free (data);
1420 }
1421
1422 g_free (text);
1423 } else if (data_type == gdk_atom_intern ("image/png", FALSE)) {
1424 pixbuf = gtk_selection_data_get_pixbuf (selection_data);
1425 if (gdk_pixbuf_save_to_buffer (pixbuf, (gchar **) &data, &buffer_size, "png", NULL, NULL))
1426 send_data_response (self, data, buffer_size);
1427 g_object_unref (pixbuf);
1428 } else if (data_type == gdk_atom_intern ("image/jpeg", FALSE)) {
1429 pixbuf = gtk_selection_data_get_pixbuf (selection_data);
1430 if (gdk_pixbuf_save_to_buffer (pixbuf, (gchar **) &data, &buffer_size, "jpeg", NULL, NULL))
1431 send_data_response (self, data, buffer_size);
1432 g_object_unref (pixbuf);
1433 } else if (data_type == gdk_atom_intern ("image/bmp", FALSE)) {
1434 pixbuf = gtk_selection_data_get_pixbuf (selection_data);
1435 if (gdk_pixbuf_save_to_buffer (pixbuf, (gchar **) &data, &buffer_size, "bmp", NULL, NULL)) {
1436 send_data_response (self, data + sizeof (BITMAPFILEHEADER), buffer_size - sizeof (BITMAPFILEHEADER));
1437 }
1438 g_object_unref (pixbuf);
1439 } else if (data_type == gdk_atom_intern ("text/uri-list", FALSE)) {
1440 FrdpLocalFileInfo *frdp_file_info;
1441 FILEDESCRIPTORW *descriptors;
1442 GFileInfo *file_info;
1443 guint32 *size;
1444 GFile *file, *root = NULL;
1445 GList *list = NULL, *iter;
1446 gchar **uris;
1447
1448 uris = gtk_selection_data_get_uris (selection_data);
1449
1450 if (uris != NULL && uris[0] != NULL) {
1451 file = g_file_new_for_uri (uris[0]);
1452 root = g_file_get_parent (file);
1453 g_object_unref (file);
1454 }
1455
1456 for (i = 0; uris[i] != NULL; i++) {
1457 file = g_file_new_for_uri (uris[i]);
1458 file_info = g_file_query_info (file,
1459 G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
1460 G_FILE_QUERY_INFO_NONE,
1461 NULL,
1462 &error);
1463
1464 if (file_info != NULL) {
1465 frdp_file_info = frdp_local_file_info_new (file, file_info, root);
1466 list = g_list_append (list, frdp_file_info);
1467
1468 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
1469 enumerate_directory (file, &list, root);
1470
1471 g_object_unref (file_info);
1472 } else {
1473 g_warning ("Error getting file info: %s", error->message);
1474 }
1475
1476 g_object_unref (file);
1477 }
1478
1479 if (root != NULL)
1480 g_object_unref (root);
1481
1482 length = g_list_length (list);
1483 data = g_malloc (length * sizeof (FILEDESCRIPTORW) + 4);
1484
1485 size = (guint32 *) data;
1486 size[0] = length;
1487
1488 g_strfreev (uris);
1489
1490 priv->local_files_count = length;
1491 priv->local_files_infos = g_new0 (FrdpLocalFileInfo, priv->local_files_count);
1492
1493 descriptors = (FILEDESCRIPTORW *) (data + 4);
1494 for (iter = list, i = 0; iter != NULL; iter = iter->next, i++) {
1495 frdp_file_info = iter->data;
1496 memcpy (&(descriptors[i]), frdp_file_info->descriptor, sizeof (FILEDESCRIPTORW));
1497 priv->local_files_infos[i].descriptor = frdp_file_info->descriptor;
1498 priv->local_files_infos[i].uri = frdp_file_info->uri;
1499 }
1500 g_list_free_full (list, g_free);
1501
1502 if (priv->awaiting_data_request && priv->pending_lock) {
1503 lock_current_local_files (self, priv->pending_lock_id);
1504
1505 priv->awaiting_data_request = FALSE;
1506 }
1507
1508 send_data_response (self, data, priv->local_files_count * sizeof (FILEDESCRIPTORW) + 4);
1509 }
1510 } else {
1511 g_warning ("No data received from local clipboard for sending to remote side!");
1512 }
1513 }
1514
1515 static guint
1516 server_format_data_request (CliprdrClientContext *context,
1517 const CLIPRDR_FORMAT_DATA_REQUEST *format_data_request)
1518 {
1519 FrdpChannelClipboard *self = (FrdpChannelClipboard *) context->custom;
1520 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1521 guint format;
1522
1523 format = format_data_request->requestedFormatId;
1524
1525 /* TODO: Add more formats (e.g. CF_DIBV5, CF_TEXT, CF_OEMTEXT) */
1526 switch (format) {
1527 case CF_UNICODETEXT:
1528 gtk_clipboard_request_contents (priv->gtk_clipboard,
1529 gdk_atom_intern ("UTF8_STRING", FALSE),
1530 clipboard_content_received,
1531 priv->duration);
1532 break;
1533 case FRDP_CLIPBOARD_FORMAT_PNG:
1534 gtk_clipboard_request_contents (priv->gtk_clipboard,
1535 gdk_atom_intern ("image/png", FALSE),
1536 clipboard_content_received,
1537 priv->duration);
1538 break;
1539 case FRDP_CLIPBOARD_FORMAT_JPEG:
1540 gtk_clipboard_request_contents (priv->gtk_clipboard,
1541 gdk_atom_intern ("image/jpeg", FALSE),
1542 clipboard_content_received,
1543 priv->duration);
1544 break;
1545 case CF_DIB:
1546 gtk_clipboard_request_contents (priv->gtk_clipboard,
1547 gdk_atom_intern ("image/bmp", FALSE),
1548 clipboard_content_received,
1549 priv->duration);
1550 break;
1551 default:
1552 if (format == priv->fgdw_id) {
1553 gtk_clipboard_request_contents (priv->gtk_clipboard,
1554 gdk_atom_intern ("text/uri-list", FALSE),
1555 clipboard_content_received,
1556 priv->duration);
1557 break;
1558 } else {
1559 g_warning ("Requesting clipboard data of type %d not implemented.", format);
1560 }
1561 }
1562
1563 return CHANNEL_RC_OK;
1564 }
1565
1566 static guint
1567 server_format_data_response (CliprdrClientContext *context,
1568 const CLIPRDR_FORMAT_DATA_RESPONSE *response)
1569 {
1570 FrdpChannelClipboard *self;
1571 FrdpChannelClipboardPrivate *priv;
1572 FrdpClipboardRequest *current_request;
1573 guint j;
1574 gint subrequest_index = -1;
1575
1576 if (context != NULL) {
1577 self = (FrdpChannelClipboard *) context->custom;
1578 priv = frdp_channel_clipboard_get_instance_private (self);
1579
1580 if (response->COMMON(msgType) == CB_FORMAT_DATA_RESPONSE) {
1581 if (priv->requests != NULL) {
1582 current_request = priv->requests->data;
1583 for (j = 0; j < current_request->count; j++) {
1584 if (!current_request->responses[j].handled) {
1585 subrequest_index = j;
1586 break;
1587 }
1588 }
1589
1590 if (subrequest_index >= 0 && subrequest_index < current_request->count) {
1591 current_request->responses[subrequest_index].handled = TRUE;
1592 if (response->COMMON(msgFlags) & CB_RESPONSE_OK) {
1593 current_request->responses[subrequest_index].length = response->COMMON(dataLen);
1594 current_request->responses[subrequest_index].data = g_new (guchar, response->COMMON(dataLen));
1595 memcpy (current_request->responses[subrequest_index].data, response->requestedFormatData, response->COMMON(dataLen));
1596 } else {
1597 g_warning ("Clipboard data request failed!");
1598 }
1599 }
1600 } else {
1601 g_warning ("Response without request!");
1602 }
1603 }
1604 }
1605
1606 return CHANNEL_RC_OK;
1607 }
1608
1609 static guint
1610 server_file_contents_request (CliprdrClientContext *context,
1611 const CLIPRDR_FILE_CONTENTS_REQUEST *file_contents_request)
1612 {
1613 FrdpChannelClipboard *self = (FrdpChannelClipboard *) context->custom;
1614 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1615 CLIPRDR_FILE_CONTENTS_RESPONSE response = { 0 };
1616 FrdpLocalFileInfo local_file_info;
1617 FrdpLocalLockData *ldata;
1618 GFileInputStream *stream;
1619 GFileInfo *file_info;
1620 GFileType file_type;
1621 gboolean local_file_info_set = FALSE, clip_data_id_found = FALSE;
1622 guint64 *size;
1623 goffset offset;
1624 guchar *data = NULL;
1625 gssize bytes_read;
1626 GList *iter;
1627 GFile *file;
1628
1629 response.COMMON(msgType) = CB_FILECONTENTS_RESPONSE;
1630 response.COMMON(msgFlags) = CB_RESPONSE_FAIL;
1631 response.streamId = file_contents_request->streamId;
1632
1633 g_mutex_lock (&priv->lock_mutex);
1634
1635 if (file_contents_request->haveClipDataId) {
1636 for (iter = priv->locked_data; iter != NULL; iter = iter->next) {
1637 ldata = (FrdpLocalLockData *) iter->data;
1638
1639 if (ldata->clip_data_id == file_contents_request->clipDataId) {
1640 clip_data_id_found = TRUE;
1641 if (file_contents_request->listIndex < ldata->local_files_count) {
1642 local_file_info = ldata->local_files_infos[file_contents_request->listIndex];
1643 local_file_info_set = TRUE;
1644 }
1645 break;
1646 }
1647 }
1648 }
1649
1650 if (!local_file_info_set && !clip_data_id_found) {
1651 if (file_contents_request->listIndex < priv->local_files_count) {
1652 local_file_info = priv->local_files_infos[file_contents_request->listIndex];
1653 local_file_info_set = TRUE;
1654 }
1655 }
1656
1657 /* TODO: Make it async. Signal progress if FD_SHOWPROGRESSUI is present. */
1658 if (local_file_info_set) {
1659 file = g_file_new_for_uri (local_file_info.uri);
1660
1661 if (file_contents_request->dwFlags & FILECONTENTS_SIZE) {
1662 file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
1663 size = g_new (guint64, 1);
1664 *size = g_file_info_get_size (file_info);
1665
1666 response.requestedData = (guchar *) size;
1667 response.cbRequested = 8;
1668 response.COMMON(dataLen) = 8;
1669 response.COMMON(msgFlags) = CB_RESPONSE_OK;
1670
1671 g_object_unref (file_info);
1672 } else if (file_contents_request->dwFlags & FILECONTENTS_RANGE) {
1673 file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
1674 file_type = g_file_info_get_file_type (file_info);
1675
1676 if (file_type != G_FILE_TYPE_DIRECTORY) {
1677 offset = ((guint64) file_contents_request->nPositionHigh << 32) + file_contents_request->nPositionLow;
1678
1679 stream = g_file_read (file, NULL, NULL);
1680
1681 if (g_seekable_can_seek (G_SEEKABLE (stream)) && g_seekable_seek (G_SEEKABLE (stream), offset, G_SEEK_SET, NULL, NULL)) {
1682 data = g_new (guchar, file_contents_request->cbRequested);
1683 bytes_read = g_input_stream_read (G_INPUT_STREAM (stream), data, file_contents_request->cbRequested, NULL, NULL);
1684
1685 response.requestedData = data;
1686 response.cbRequested = bytes_read;
1687 response.COMMON(dataLen) = bytes_read;
1688 response.COMMON(msgFlags) = CB_RESPONSE_OK;
1689 }
1690 } else {
1691 g_warning ("Content of a directory was requested!");
1692 }
1693
1694 g_object_unref (stream);
1695 g_object_unref (file_info);
1696 }
1697
1698 g_object_unref (file);
1699 } else {
1700 g_warning ("Requested index is outside of the file list!");
1701 }
1702
1703 g_mutex_unlock (&priv->lock_mutex);
1704
1705 return priv->cliprdr_client_context->ClientFileContentsResponse (priv->cliprdr_client_context, &response);
1706 }
1707
1708 static guint
1709 server_file_contents_response (CliprdrClientContext *context,
1710 const CLIPRDR_FILE_CONTENTS_RESPONSE *file_contents_response)
1711 {
1712 FrdpChannelClipboard *self;
1713 FrdpChannelClipboardPrivate *priv;
1714 struct fuse_entry_param entry = {0};
1715 FrdpRemoteFileRequest *request;
1716 struct stat attr = {0};
1717
1718 if (context != NULL && file_contents_response->COMMON(msgFlags) & CB_RESPONSE_OK) {
1719 self = (FrdpChannelClipboard *) context->custom;
1720 priv = frdp_channel_clipboard_get_instance_private (self);
1721
1722 request = g_hash_table_lookup (priv->remote_files_requests,
1723 GUINT_TO_POINTER (file_contents_response->streamId));
1724 if (request != NULL) {
1725 g_mutex_lock (&priv->fuse_mutex);
1726 switch (request->op) {
1727 case FUSE_LOOKUP_OP:
1728 priv->remote_files_infos[request->index].size = *((guint64 *) file_contents_response->requestedData);
1729 priv->remote_files_infos[request->index].has_size = TRUE;
1730
1731 entry.ino = priv->remote_files_infos[request->index].inode;
1732 get_file_attributes (priv->remote_files_infos[request->index], &entry.attr);
1733 entry.attr_timeout = 1.0;
1734 entry.entry_timeout = 1.0;
1735
1736 fuse_reply_entry (request->request, &entry);
1737 break;
1738
1739 case FUSE_GETATTR_OP:
1740 priv->remote_files_infos[request->index].size = *((guint64 *) file_contents_response->requestedData);
1741 priv->remote_files_infos[request->index].has_size = TRUE;
1742
1743 get_file_attributes (priv->remote_files_infos[request->index], &attr);
1744 fuse_reply_attr (request->request, &attr, 1);
1745 break;
1746
1747 case FUSE_READ_OP:
1748 fuse_reply_buf (request->request,
1749 (const char *) file_contents_response->requestedData,
1750 file_contents_response->cbRequested);
1751 break;
1752
1753 default:
1754 g_assert_not_reached ();
1755 }
1756
1757 g_hash_table_remove (priv->remote_files_requests,
1758 GUINT_TO_POINTER (file_contents_response->streamId));
1759 g_free (request);
1760 g_mutex_unlock (&priv->fuse_mutex);
1761 }
1762 } else {
1763 if (file_contents_response->COMMON(msgFlags) & CB_RESPONSE_FAIL) {
1764 g_warning ("Server file response has failed!");
1765 }
1766 }
1767
1768 return CHANNEL_RC_OK;
1769 }
1770
1771 static void
1772 lock_current_local_files (FrdpChannelClipboard *self,
1773 guint clip_data_id)
1774 {
1775 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1776 FrdpLocalLockData *lock_data;
1777 guint i;
1778
1779 g_mutex_lock (&priv->lock_mutex);
1780
1781 /* TODO: Implement flock */
1782 if (priv->local_files_count > 0) {
1783 lock_data = g_new (FrdpLocalLockData, 1);
1784 lock_data->clip_data_id = clip_data_id;
1785 lock_data->local_files_count = priv->local_files_count;
1786 lock_data->local_files_infos = g_new (FrdpLocalFileInfo, lock_data->local_files_count);
1787 for (i = 0; i < lock_data->local_files_count; i++) {
1788 lock_data->local_files_infos[i].descriptor = priv->local_files_infos[i].descriptor;
1789 lock_data->local_files_infos[i].uri = g_strdup (priv->local_files_infos[i].uri);
1790 }
1791
1792 priv->locked_data = g_list_append (priv->locked_data, lock_data);
1793 if (priv->pending_lock_id == clip_data_id)
1794 priv->pending_lock = FALSE;
1795 }
1796
1797 g_mutex_unlock (&priv->lock_mutex);
1798 }
1799
1800 static guint
1801 server_lock_clipboard_data (CliprdrClientContext *context,
1802 const CLIPRDR_LOCK_CLIPBOARD_DATA *lock_clipboard_data)
1803 {
1804 FrdpChannelClipboard *self = (FrdpChannelClipboard *) context->custom;
1805 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1806
1807 if (priv->awaiting_data_request) {
1808 priv->pending_lock = TRUE;
1809 priv->pending_lock_id = lock_clipboard_data->clipDataId;
1810 } else {
1811 lock_current_local_files (self, lock_clipboard_data->clipDataId);
1812 }
1813
1814 return CHANNEL_RC_OK;
1815 }
1816
1817 static void
1818 frdp_local_lock_data_free (FrdpLocalLockData *lock_data)
1819 {
1820 guint i;
1821
1822 for (i = 0; i < lock_data->local_files_count; i++)
1823 g_free (lock_data->local_files_infos[i].uri);
1824 g_free (lock_data->local_files_infos);
1825 g_free (lock_data);
1826 }
1827
1828 static guint
1829 server_unlock_clipboard_data (CliprdrClientContext *context,
1830 const CLIPRDR_UNLOCK_CLIPBOARD_DATA *unlock_clipboard_data)
1831 {
1832 FrdpChannelClipboard *self = (FrdpChannelClipboard *) context->custom;
1833 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1834 FrdpLocalLockData *lock_data;
1835 GList *iter;
1836
1837 g_mutex_lock (&priv->lock_mutex);
1838
1839 for (iter = priv->locked_data; iter != NULL; iter = iter->next) {
1840 lock_data = iter->data;
1841
1842 if (lock_data->clip_data_id == unlock_clipboard_data->clipDataId) {
1843 frdp_local_lock_data_free (lock_data);
1844
1845 priv->locked_data = g_list_delete_link (priv->locked_data, iter);
1846 break;
1847 }
1848 }
1849
1850 g_mutex_unlock (&priv->lock_mutex);
1851
1852 return CHANNEL_RC_OK;
1853 }
1854
1855 static void
1856 frdp_channel_clipboard_set_client_context (FrdpChannelClipboard *self,
1857 CliprdrClientContext *context)
1858 {
1859 FrdpChannelClipboardPrivate *priv = frdp_channel_clipboard_get_instance_private (self);
1860
1861 priv->cliprdr_client_context = context;
1862
1863 context->custom = self;
1864 context->MonitorReady = monitor_ready;
1865 context->ServerCapabilities = server_capabilities;
1866 context->ServerFormatList = server_format_list;
1867 context->ServerFormatListResponse = server_format_list_response;
1868 context->ServerFormatDataRequest = server_format_data_request;
1869 context->ServerFormatDataResponse = server_format_data_response;
1870 context->ServerFileContentsRequest = server_file_contents_request;
1871 context->ServerFileContentsResponse = server_file_contents_response;
1872
1873 /* These don't lock/unlock files currently but store lists of files with their clipDataId. */
1874 context->ServerLockClipboardData = server_lock_clipboard_data;
1875 context->ServerUnlockClipboardData = server_unlock_clipboard_data;
1876 }