-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
clipboard.c
2500 lines (2319 loc) · 82.4 KB
/
clipboard.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2009-2012
* Copyright (C) Laxmikant Rashinkar 2012
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
for help see
http://tronche.com/gui/x/icccm/sec-2.html#s-2
.../kde/kdebase/workspace/klipper/clipboardpoll.cpp
Revision:
Aug 05, 2012:
Laxmikant Rashinkar (LK dot Rashinkar at gmail.com)
added clipboard support for BMP images
http://msdn.microsoft.com/en-us/library/cc241066%28v=prot.20%29.aspx
*/
/*
TARGETS
MULTIPLE
image/tiff
image/jpeg
image/x-MS-bmp
image/x-bmp
image/bmp
image/png
SAVE_TARGETS
TIMESTAMP
wininfo - show window info
xlsatoms - dump atoms
dolphin 1.4 KDE 4.4.5 (debian 6) copy one file
text/uri-list
text/x-moz-url
text/plain
UTF8_STRING
STRING
TEXT
COMPOUND_TEXT
application/x-qiconlist
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS
dolphin 1.6.1 KDE 4.6.5 (kubuntu 11.04) copy one file
text/uri-list
text/x-moz-url
text/plain
UTF8_STRING
STRING
TEXT
COMPOUND_TEXT
application/x-qiconlist
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS
kolourpaint 4.4.5 KDE 4.4.5 copy image area
application/x-kolourpaint-selection-400
application/x-qt-image
image/png
image/bw
image/eps
image/epsf
image/epsi
image/pcx
image/rgb
image/rgba
image/sgi
image/tga
image/bmp
image/ico
image/jp2
image/jpeg
image/jpg
image/ppm
PIXMAP
image/tif
image/tiff
image/xbm
image/xpm
image/xv
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS
kate 3.4.5 KDE 4.4.5 copy text
text/plain
UTF8_STRING
STRING
TEXT
COMPOUND_TEXT
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS
gimp 2.6.10 copy image area
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
image/png
image/bmp
image/x-bmp
image/x-MS-bmp
image/tiff
image/x-icon
image/x-ico
image/x-win-bitmap
image/jpeg
thunar 1.2.1 copy a file
TIMESTAMP
TARGETS
MULTIPLE
x-special/gnome-copied-files
UTF8_STRING
dolphin 1.6.1 KDE 4.6.5 (kubuntu 11.04) copy two files
text/uri-list
/home/jay/temp/jetstream1.txt
/home/jay/temp/jpeg64x64.jpg
0000 66 69 6c 65 3a 2f 2f 2f 68 6f 6d 65 2f 6a 61 79 file:///home/jay
0010 2f 74 65 6d 70 2f 6a 65 74 73 74 72 65 61 6d 31 /temp/jetstream1
0020 2e 74 78 74 0d 0a 66 69 6c 65 3a 2f 2f 2f 68 6f .txt..file:///ho
0030 6d 65 2f 6a 61 79 2f 74 65 6d 70 2f 6a 70 65 67 me/jay/temp/jpeg
0040 36 34 78 36 34 2e 6a 70 67 64x64.jpg
thunar 1.2.1 (kubuntu 11.04) copy two files
x-special/gnome-copied-files
/home/jay/temp/jetstream1.txt
/home/jay/temp/jpeg64x64.jpg
0000 63 6f 70 79 0a 66 69 6c 65 3a 2f 2f 2f 68 6f 6d copy.file:///hom
0010 65 2f 6a 61 79 2f 74 65 6d 70 2f 6a 65 74 73 74 e/jay/temp/jetst
0020 72 65 61 6d 31 2e 74 78 74 0d 0a 66 69 6c 65 3a ream1.txt..file:
0030 2f 2f 2f 68 6f 6d 65 2f 6a 61 79 2f 74 65 6d 70 ///home/jay/temp
0040 2f 6a 70 65 67 36 34 78 36 34 2e 6a 70 67 0d 0a /jpeg64x64.jpg..
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
#include "arch.h"
#include "parse.h"
#include "os_calls.h"
#include "string_calls.h"
#include "chansrv.h"
#include "chansrv_config.h"
#include "clipboard.h"
#include "clipboard_file.h"
#include "clipboard_common.h"
#include "xcommon.h"
#include "chansrv_fuse.h"
#include "ms-rdpeclip.h"
static char g_bmp_image_header[] =
{
/* this is known to work */
//0x42, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
/* THIS IS BEING SENT BY WIN2008 */
0x42, 0x4d, 0x16, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00
};
extern int g_cliprdr_chan_id; /* in chansrv.c */
extern Display *g_display; /* in xcommon.c */
extern int g_x_socket; /* in xcommon.c */
extern tbus g_x_wait_obj; /* in xcommon.c */
extern Screen *g_screen; /* in xcommon.c */
extern int g_screen_num; /* in xcommon.c */
extern struct config_chansrv *g_cfg; /* in chansrv.c */
int g_clip_up = 0;
static Atom g_clipboard_atom = 0; /* CLIPBOARD */
static Atom g_clip_property_atom = 0; /* XRDP_CLIP_PROPERTY_ATOM */
static Atom g_timestamp_atom = 0; /* TIMESTAMP */
static Atom g_multiple_atom = 0; /* MULTIPLE */
static Atom g_targets_atom = 0; /* TARGETS */
static Atom g_primary_atom = 0; /* PRIMARY */
static Atom g_secondary_atom = 0; /* SECONDARY */
static Atom g_get_time_atom = 0; /* XRDP_GET_TIME_ATOM */
static Atom g_utf8_atom = 0; /* UTF8_STRING */
static Atom g_image_bmp_atom = 0; /* image/bmp */
static Atom g_file_atom1 = 0; /* text/uri-list */
static Atom g_file_atom2 = 0; /* x-special/gnome-copied-files */
static Atom g_incr_atom = 0; /* INCR */
static Window g_wnd = 0;
static int g_xfixes_event_base = 0;
static int g_got_selection = 0; /* boolean */
static Time g_selection_time = 0;
static struct stream *g_ins = 0;
/* for image data */
static XSelectionRequestEvent g_saved_selection_req_event;
/* xserver maximum request size in bytes */
static int g_incr_max_req_size = 0;
/* server to client, pasting from linux app to mstsc */
struct clip_s2c g_clip_s2c;
/* client to server, pasting from mstsc to linux app */
struct clip_c2s g_clip_c2s;
/* default version and flags */
static int g_cliprdr_version = CB_CAPS_VERSION_2;
static int g_cliprdr_flags = CB_USE_LONG_FORMAT_NAMES |
CB_STREAM_FILECLIP_ENABLED |
CB_FILECLIP_NO_FILE_PATHS;
/* from client to server */
/* last received CLIPRDR_FORMAT_LIST(CLIPRDR_FORMAT_ANNOUNCE) */
static int g_formatIds[16];
static int g_num_formatIds = 0;
static int g_file_format_id = -1;
static char g_last_atom_name[256] = "";
/*****************************************************************************/
static char *
get_atom_text(Atom atom)
{
char *name;
int failed;
failed = 0;
/* sanity check */
if ((atom < 1) || (atom > 512))
{
failed = 1;
}
if (!failed)
{
name = XGetAtomName(g_display, atom);
if (name == 0)
{
failed = 1;
}
}
if (failed)
{
g_snprintf(g_last_atom_name, 255, "unknown atom 0x%8.8x", (int)atom);
return g_last_atom_name;
}
g_strncpy(g_last_atom_name, name, 255);
XFree(name);
return g_last_atom_name;
}
/*****************************************************************************/
/* this is one way to get the current time from the x server */
static Time
clipboard_get_server_time(void)
{
XEvent xevent;
unsigned char no_text[4];
/* append nothing */
no_text[0] = 0;
XChangeProperty(g_display, g_wnd, g_get_time_atom, XA_STRING, 8,
PropModeAppend, no_text, 0);
/* wait for PropertyNotify */
do
{
XMaskEvent(g_display, PropertyChangeMask, &xevent);
}
while (xevent.type != PropertyNotify);
return xevent.xproperty.time;
}
/*****************************************************************************/
static int
clipboard_find_format_id(int format_id)
{
int index;
for (index = 0; index < g_num_formatIds; index++)
{
if (g_formatIds[index] == format_id)
{
return index;
}
}
return -1;
}
/*****************************************************************************/
/* returns error */
int
clipboard_init(void)
{
struct stream *s;
int size;
int rv;
int input_mask;
int dummy;
int ver_maj;
int ver_min;
Status st;
LOG_DEVEL(LOG_LEVEL_INFO, "clipboard_init:");
if (g_clip_up)
{
return 0;
}
xfuse_init();
xcommon_init();
g_incr_max_req_size = XMaxRequestSize(g_display) * 4 - 24;
g_memset(&g_clip_c2s, 0, sizeof(g_clip_c2s));
g_memset(&g_clip_s2c, 0, sizeof(g_clip_s2c));
rv = 0;
if (rv == 0)
{
g_clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False);
if (g_clipboard_atom == None)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_init: XInternAtom failed");
rv = 3;
}
}
if (rv == 0)
{
if (!XFixesQueryExtension(g_display, &g_xfixes_event_base, &dummy))
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_init: no xfixes");
rv = 5;
}
}
if (rv == 0)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_init: g_xfixes_event_base %d",
g_xfixes_event_base);
st = XFixesQueryVersion(g_display, &ver_maj, &ver_min);
LOG(LOG_LEVEL_DEBUG, "clipboard_init st %d, maj %d min %d", st,
ver_maj, ver_min);
g_clip_property_atom = XInternAtom(g_display, "XRDP_CLIP_PROPERTY_ATOM",
False);
g_get_time_atom = XInternAtom(g_display, "XRDP_GET_TIME_ATOM",
False);
g_timestamp_atom = XInternAtom(g_display, "TIMESTAMP", False);
g_targets_atom = XInternAtom(g_display, "TARGETS", False);
g_multiple_atom = XInternAtom(g_display, "MULTIPLE", False);
g_primary_atom = XInternAtom(g_display, "PRIMARY", False);
g_secondary_atom = XInternAtom(g_display, "SECONDARY", False);
g_utf8_atom = XInternAtom(g_display, "UTF8_STRING", False);
g_image_bmp_atom = XInternAtom(g_display, "image/bmp", False);
g_file_atom1 = XInternAtom(g_display, "text/uri-list", False);
g_file_atom2 = XInternAtom(g_display, "x-special/gnome-copied-files", False);
g_incr_atom = XInternAtom(g_display, "INCR", False);
if (g_image_bmp_atom == None)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_init: g_image_bmp_atom was "
"not allocated");
}
g_wnd = XCreateSimpleWindow(g_display, RootWindowOfScreen(g_screen),
0, 0, 4, 4, 0, 0, 0);
input_mask = StructureNotifyMask | PropertyChangeMask;
XSelectInput(g_display, g_wnd, input_mask);
//XMapWindow(g_display, g_wnd);
XFixesSelectSelectionInput(g_display, g_wnd,
g_clipboard_atom,
XFixesSetSelectionOwnerNotifyMask |
XFixesSelectionWindowDestroyNotifyMask |
XFixesSelectionClientCloseNotifyMask);
}
make_stream(s);
if (rv == 0)
{
/* set clipboard caps first */
init_stream(s, 8192);
/* CLIPRDR_HEADER */
out_uint16_le(s, CB_CLIP_CAPS); /* msgType */
out_uint16_le(s, 0); /* msgFlags */
out_uint32_le(s, 16); /* dataLen */
out_uint16_le(s, 1); /* cCapabilitiesSets */
out_uint16_le(s, 0); /* pad1 */
/* CLIPRDR_GENERAL_CAPABILITY */
out_uint16_le(s, CB_CAPSTYPE_GENERAL); /* capabilitySetType */
out_uint16_le(s, 12); /* lengthCapability */
out_uint32_le(s, g_cliprdr_version); /* version */
out_uint32_le(s, g_cliprdr_flags); /* generalFlags */
out_uint32_le(s, 0); /* extra 4 bytes ? */
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_init: data out, sending "
"CB_CLIP_CAPS (clip_msg_id = 1)");
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
if (rv != 0)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_init: send_channel_data failed "
"rv = %d", rv);
rv = 4;
}
}
if (rv == 0)
{
/* report clipboard ready */
init_stream(s, 8192);
out_uint16_le(s, CB_MONITOR_READY); /* msgType */
out_uint16_le(s, 0); /* msgFlags */
out_uint32_le(s, 0); /* dataLen */
out_uint32_le(s, 0); /* extra 4 bytes ? */
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_init: data out, sending "
"CB_MONITOR_READY (clip_msg_id = 1)");
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
if (rv != 0)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_init: send_channel_data failed "
"rv = %d", rv);
rv = 4;
}
}
free_stream(s);
if (rv == 0)
{
g_clip_up = 1;
make_stream(g_ins);
init_stream(g_ins, 8192);
}
else
{
LOG_DEVEL(LOG_LEVEL_ERROR, "xrdp-chansrv: clipboard_init: error on exit");
}
return rv;
}
/*****************************************************************************/
int
clipboard_deinit(void)
{
LOG_DEVEL(LOG_LEVEL_INFO, "clipboard_deinit:");
if (g_wnd != 0)
{
XDestroyWindow(g_display, g_wnd);
g_wnd = 0;
}
xfuse_deinit();
g_free(g_clip_c2s.data);
g_clip_c2s.data = 0;
g_free(g_clip_s2c.data);
g_clip_s2c.data = 0;
free_stream(g_ins);
g_ins = 0;
g_clip_up = 0;
return 0;
}
/*****************************************************************************/
static int
clipboard_send_data_request(int format_id)
{
struct stream *s;
int size;
int rv;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_request:");
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_request: %d", format_id);
g_clip_c2s.in_request = 1;
make_stream(s);
init_stream(s, 8192);
out_uint16_le(s, CB_FORMAT_DATA_REQUEST); /* 4 CLIPRDR_DATA_REQUEST */
out_uint16_le(s, 0); /* status */
out_uint32_le(s, 4); /* length */
out_uint32_le(s, format_id);
out_uint32_le(s, 0);
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_request: data out, sending "
"CLIPRDR_DATA_REQUEST (clip_msg_id = 4)");
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s);
return rv;
}
/*****************************************************************************/
static int
clipboard_send_format_ack(void)
{
struct stream *s;
int size;
int rv;
make_stream(s);
init_stream(s, 8192);
out_uint16_le(s, CB_FORMAT_LIST_RESPONSE); /* 3 CLIPRDR_FORMAT_ACK */
out_uint16_le(s, CB_RESPONSE_OK); /* 1 status */
out_uint32_le(s, 0); /* length */
out_uint32_le(s, 0); /* extra 4 bytes */
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_ack: data out, sending "
"CLIPRDR_FORMAT_ACK (clip_msg_id = 3)");
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s);
return rv;
}
/*****************************************************************************/
/* returns number of bytes written */
int
clipboard_out_unicode(struct stream *s, const char *text, int num_chars)
{
int index;
int lnum_chars;
twchar *ltext;
if ((num_chars < 1) || (text == 0))
{
return 0;
}
lnum_chars = g_mbstowcs(0, text, num_chars);
if (lnum_chars < 0)
{
return 0;
}
ltext = (twchar *) g_malloc((num_chars + 1) * sizeof(twchar), 1);
g_mbstowcs(ltext, text, num_chars);
index = 0;
while (index < num_chars)
{
out_uint16_le(s, ltext[index]);
index++;
}
g_free(ltext);
return index * 2;
}
/*****************************************************************************/
/* returns number of bytes read */
int
clipboard_in_unicode(struct stream *s, char *text, int *num_chars)
{
int index;
twchar *ltext;
twchar chr;
if ((num_chars == 0) || (*num_chars < 1) || (text == 0))
{
return 0;
}
ltext = (twchar *) g_malloc(512 * sizeof(twchar), 1);
index = 0;
while (s_check_rem(s, 2))
{
in_uint16_le(s, chr);
if (index < 511)
{
ltext[index] = chr;
}
index++;
if (chr == 0)
{
break;
}
}
*num_chars = g_wcstombs(text, ltext, *num_chars);
g_free(ltext);
return index * 2;
}
static char windows_native_format[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/*****************************************************************************/
static int
clipboard_send_format_announce(int xrdp_clip_type)
{
struct stream *s;
int size;
int rv;
char *holdp;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce:");
make_stream(s);
init_stream(s, 8192);
out_uint16_le(s, CB_FORMAT_LIST); /* 2 CLIPRDR_FORMAT_ANNOUNCE */
out_uint16_le(s, 0); /* status */
holdp = s->p;
out_uint32_le(s, 0); /* set later */
if (g_cliprdr_flags & CB_USE_LONG_FORMAT_NAMES)
{
switch (xrdp_clip_type)
{
case XRDP_CB_FILE:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_FILE");
/* canned response for "file" */
out_uint32_le(s, 0x0000c0bc);
clipboard_out_unicode(s, "FileGroupDescriptorW", 21);
out_uint32_le(s, 0x0000c0ba);
clipboard_out_unicode(s, "FileContents", 13);
out_uint32_le(s, 0x0000c0c1);
clipboard_out_unicode(s, "DropEffect", 11);
break;
case XRDP_CB_BITMAP:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_BITMAP");
/* canned response for "bitmap" */
out_uint32_le(s, 0x0000c004);
clipboard_out_unicode(s, "Native", 7);
out_uint32_le(s, 0x00000003);
clipboard_out_unicode(s, "", 1);
out_uint32_le(s, 0x00000008);
clipboard_out_unicode(s, "", 1);
out_uint32_le(s, 0x00000011);
clipboard_out_unicode(s, "", 1);
break;
case XRDP_CB_TEXT:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_TEXT");
/* canned response for "bitmap" */
out_uint32_le(s, 0x0000000d);
clipboard_out_unicode(s, "", 1);
out_uint32_le(s, 0x00000010);
clipboard_out_unicode(s, "", 1);
out_uint32_le(s, 0x00000001);
clipboard_out_unicode(s, "", 1);
out_uint32_le(s, 0x00000007);
clipboard_out_unicode(s, "", 1);
break;
default:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: unknown "
"xrdp_clip_type %d", xrdp_clip_type);
break;
}
}
else /* old method */
{
switch (xrdp_clip_type)
{
case XRDP_CB_FILE:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_FILE");
/* canned response for "file" */
out_uint32_le(s, 0x0000c0bc);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x0000c0ba);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x0000c0c1);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
break;
case XRDP_CB_BITMAP:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_BITMAP");
/* canned response for "bitmap" */
out_uint32_le(s, 0x0000c004);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000003);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000008);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000011);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
break;
case XRDP_CB_TEXT:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: XRDP_CB_TEXT");
/* canned response for "bitmap" */
out_uint32_le(s, 0x0000000d);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000010);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000001);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
out_uint32_le(s, 0x00000007);
out_uint8p(s, windows_native_format, sizeof(windows_native_format));
break;
default:
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: unknown "
"xrdp_clip_type %d", xrdp_clip_type);
break;
}
}
size = (int)(s->p - holdp);
size -= 4;
holdp[0] = (size >> 0) & 0xff;
holdp[1] = (size >> 8) & 0xff;
holdp[2] = (size >> 16) & 0xff;
holdp[3] = (size >> 24) & 0xff;
out_uint32_le(s, 0);
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL_HEXDUMP(LOG_LEVEL_TRACE, "clipboard data:", s->data, size);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_format_announce: data out, sending "
"CLIPRDR_FORMAT_ANNOUNCE (clip_msg_id = 2)");
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s);
return rv;
}
/*****************************************************************************/
static int
clipboard_send_data_response_for_image(const char *data, int data_size)
{
struct stream *s;
int size;
int rv;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response_for_image: data_size %d",
data_size);
make_stream(s);
init_stream(s, 64 + data_size);
out_uint16_le(s, CB_FORMAT_DATA_RESPONSE); /* 5 CLIPRDR_DATA_RESPONSE */
out_uint16_le(s, CB_RESPONSE_OK); /* 1 status */
out_uint32_le(s, data_size); /* length */
out_uint8p(s, data, data_size);
out_uint32_le(s, 0);
s_mark_end(s);
size = (int)(s->end - s->data);
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s);
return rv;
}
/*****************************************************************************/
static int
clipboard_send_data_response_for_text(const char *data, int data_size)
{
struct stream *s;
int size;
int rv;
int num_chars;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response_for_text: data_size %d",
data_size);
LOG_DEVEL_HEXDUMP(LOG_LEVEL_TRACE, "clipboard send data response:", data, data_size);
num_chars = g_mbstowcs(0, data, 0);
if (num_chars < 0)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_send_data_response_for_text: "
"bad string");
num_chars = 0;
}
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response_for_text: data_size %d "
"num_chars %d", data_size, num_chars);
make_stream(s);
init_stream(s, 64 + num_chars * 2);
out_uint16_le(s, CB_FORMAT_DATA_RESPONSE); /* 5 CLIPRDR_DATA_RESPONSE */
out_uint16_le(s, CB_RESPONSE_OK); /* 1 status */
out_uint32_le(s, num_chars * 2 + 2); /* length */
if (clipboard_out_unicode(s, data, num_chars) != num_chars * 2)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_send_data_response_for_text: error "
"clipboard_out_unicode didn't write right number of bytes");
}
out_uint16_le(s, 0); /* nil for string */
out_uint32_le(s, 0);
s_mark_end(s);
size = (int)(s->end - s->data);
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response_for_text: data out, "
"sending CLIPRDR_DATA_RESPONSE (clip_msg_id = 5) size %d "
"num_chars %d", size, num_chars);
rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s);
return rv;
}
/*****************************************************************************/
static int
clipboard_send_data_response(int xrdp_clip_type, const char *data, int data_size)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response:");
if (data != 0)
{
if (xrdp_clip_type == XRDP_CB_FILE)
{
return clipboard_send_data_response_for_file(data, data_size);
}
else if (xrdp_clip_type == XRDP_CB_BITMAP)
{
return clipboard_send_data_response_for_image(data, data_size);
}
else if (xrdp_clip_type == XRDP_CB_TEXT)
{
return clipboard_send_data_response_for_text(data, data_size);
}
else
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_send_data_response: unknown "
"xrdp_clip_type %d", xrdp_clip_type);
}
}
else
{
LOG_DEVEL(LOG_LEVEL_ERROR, "clipboard_send_data_response: data is nil");
}
return 0;
}
/*****************************************************************************/
static int
clipboard_set_selection_owner(void)
{
Window owner;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_set_selection_owner:");
g_selection_time = clipboard_get_server_time();
XSetSelectionOwner(g_display, g_clipboard_atom, g_wnd, g_selection_time);
owner = XGetSelectionOwner(g_display, g_clipboard_atom);
if (owner != g_wnd)
{
g_got_selection = 0;
return 1;
}
g_got_selection = 1;
return 0;
}
/*****************************************************************************/
static int
clipboard_provide_selection_c2s(XSelectionRequestEvent *req, Atom type)
{
XEvent xev;
long val1[2];
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_provide_selection_c2s: bytes %d",
g_clip_c2s.total_bytes);
if (g_clip_c2s.total_bytes < g_incr_max_req_size)
{
XChangeProperty(g_display, req->requestor, req->property,
type, 8, PropModeReplace,
(tui8 *)g_clip_c2s.data, g_clip_c2s.total_bytes);
g_memset(&xev, 0, sizeof(xev));
xev.xselection.type = SelectionNotify;
xev.xselection.send_event = True;
xev.xselection.display = req->display;
xev.xselection.requestor = req->requestor;
xev.xselection.selection = req->selection;
xev.xselection.target = req->target;
xev.xselection.property = req->property;
xev.xselection.time = req->time;
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
}
else
{
/* start the INCR process */
g_clip_c2s.incr_in_progress = 1;
g_clip_c2s.incr_bytes_done = 0;
g_clip_c2s.type = type;
g_clip_c2s.property = req->property;
g_clip_c2s.window = req->requestor;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_provide_selection_c2s: start INCR property %s "
"type %s", get_atom_text(req->property),
get_atom_text(type));
val1[0] = g_clip_c2s.total_bytes;
val1[1] = 0;
XChangeProperty(g_display, req->requestor, req->property,
g_incr_atom, 32, PropModeReplace, (tui8 *)val1, 1);
/* we need events from that other window */
XSelectInput(g_display, req->requestor, PropertyChangeMask);
g_memset(&xev, 0, sizeof(xev));
xev.xselection.type = SelectionNotify;
xev.xselection.send_event = True;
xev.xselection.display = req->display;
xev.xselection.requestor = req->requestor;
xev.xselection.selection = req->selection;
xev.xselection.target = req->target;
xev.xselection.property = req->property;
xev.xselection.time = req->time;
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
}
return 0;
}
/*****************************************************************************/
static int
clipboard_provide_selection(XSelectionRequestEvent *req, Atom type, int format,
char *data, int length)
{
XEvent xev;
int bytes;
bytes = FORMAT_TO_BYTES(format);
bytes *= length;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_provide_selection: bytes %d", bytes);
if (bytes < g_incr_max_req_size)
{
XChangeProperty(g_display, req->requestor, req->property,
type, format, PropModeReplace, (tui8 *)data, length);
g_memset(&xev, 0, sizeof(xev));
xev.xselection.type = SelectionNotify;
xev.xselection.send_event = True;
xev.xselection.display = req->display;
xev.xselection.requestor = req->requestor;
xev.xselection.selection = req->selection;
xev.xselection.target = req->target;
xev.xselection.property = req->property;
xev.xselection.time = req->time;
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
return 0;
}
return 1;
}
/*****************************************************************************/
static int
clipboard_refuse_selection(XSelectionRequestEvent *req)
{
XEvent xev;
g_memset(&xev, 0, sizeof(xev));
xev.xselection.type = SelectionNotify;
xev.xselection.send_event = True;
xev.xselection.display = req->display;
xev.xselection.requestor = req->requestor;
xev.xselection.selection = req->selection;
xev.xselection.target = req->target;
xev.xselection.property = None;
xev.xselection.time = req->time;
XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
return 0;
}
/*****************************************************************************/
/* sent by client or server when its local system clipboard is
updated with new clipboard data; contains Clipboard Format ID
and name pairs of new Clipboard Formats on the clipboard. */
static int
clipboard_process_format_announce(struct stream *s, int clip_msg_status,
int clip_msg_len)
{
int formatId;
int count;
int bytes;
char desc[256];
char *holdp;
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_process_format_announce: "
"CLIPRDR_FORMAT_ANNOUNCE");
LOG_DEVEL(LOG_LEVEL_DEBUG, "clipboard_process_format_announce %d", clip_msg_len);
clipboard_send_format_ack();
xfuse_clear_clip_dir();
g_clip_c2s.converted = 0;
desc[0] = 0;
g_num_formatIds = 0;
while (clip_msg_len > 3)
{
in_uint32_le(s, formatId);
clip_msg_len -= 4;
if (g_cliprdr_flags & CB_USE_LONG_FORMAT_NAMES)
{