forked from dacap/clip
-
Notifications
You must be signed in to change notification settings - Fork 6
/
clip_x11.cpp
1123 lines (967 loc) · 31.9 KB
/
clip_x11.cpp
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
// Clip Library
// Copyright (c) 2018-2022 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "clip.h"
#include "clip_lock_impl.h"
#include <xcb/xcb.h>
#include <atomic>
#include <algorithm>
#include <cassert>
#include <condition_variable>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#if CLIP_ENABLE_IMAGE && HAVE_PNG_H
#include "clip_x11_png.h"
#endif
#define CLIP_SUPPORT_SAVE_TARGETS 1
namespace clip {
namespace {
enum CommonAtom {
ATOM,
INCR,
TARGETS,
CLIPBOARD,
#ifdef HAVE_PNG_H
MIME_IMAGE_PNG,
#endif
#ifdef CLIP_SUPPORT_SAVE_TARGETS
ATOM_PAIR,
SAVE_TARGETS,
MULTIPLE,
CLIPBOARD_MANAGER,
#endif
};
const char* kCommonAtomNames[] = {
"ATOM",
"INCR",
"TARGETS",
"CLIPBOARD",
#ifdef HAVE_PNG_H
"image/png",
#endif
#ifdef CLIP_SUPPORT_SAVE_TARGETS
"ATOM_PAIR",
"SAVE_TARGETS",
"MULTIPLE",
"CLIPBOARD_MANAGER",
#endif
};
const int kBaseForCustomFormats = 100;
class Manager {
public:
typedef std::shared_ptr<std::vector<uint8_t>> buffer_ptr;
typedef std::vector<xcb_atom_t> atoms;
typedef std::function<bool()> notify_callback;
Manager()
: m_lock(m_mutex, std::defer_lock)
, m_connection(xcb_connect(nullptr, nullptr))
, m_window(0)
, m_incr_process(false) {
if (!m_connection)
return;
const xcb_setup_t* setup = xcb_get_setup(m_connection);
if (!setup)
return;
xcb_screen_t* screen = xcb_setup_roots_iterator(setup).data;
if (!screen)
return;
uint32_t event_mask =
// Just in case that some program reports SelectionNotify events
// with XCB_EVENT_MASK_PROPERTY_CHANGE mask.
XCB_EVENT_MASK_PROPERTY_CHANGE |
// To receive DestroyNotify event and stop the message loop.
XCB_EVENT_MASK_STRUCTURE_NOTIFY;
m_window = xcb_generate_id(m_connection);
xcb_create_window(m_connection, 0,
m_window,
screen->root,
0, 0, 1, 1, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
XCB_CW_EVENT_MASK,
&event_mask);
m_thread = std::thread(
[this]{
process_x11_events();
});
}
~Manager() {
#ifdef CLIP_SUPPORT_SAVE_TARGETS
if (!m_data.empty() &&
m_window &&
m_window == get_x11_selection_owner()) {
// If the CLIPBOARD_MANAGER atom is not 0, we assume that there
// is a clipboard manager available were we can leave our data.
xcb_atom_t x11_clipboard_manager = get_atom(CLIPBOARD_MANAGER);
if (x11_clipboard_manager) {
// We have to lock the m_lock mutex that will be used to wait
// the m_cv condition in get_data_from_selection_owner().
if (try_lock()) {
// Start the SAVE_TARGETS mechanism so the X11
// CLIPBOARD_MANAGER will save our clipboard data
// from now on.
get_data_from_selection_owner(
{ get_atom(SAVE_TARGETS) },
[]() -> bool { return true; },
x11_clipboard_manager);
}
}
}
#endif
if (m_window) {
xcb_destroy_window(m_connection, m_window);
xcb_flush(m_connection);
}
if (m_thread.joinable())
m_thread.join();
if (m_connection)
xcb_disconnect(m_connection);
}
bool try_lock() {
bool res = m_lock.try_lock();
if (!res) {
// TODO make this configurable (the same for Windows retries)
for (int i=0; i<5 && !res; ++i) {
res = m_lock.try_lock();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
return res;
}
void unlock() {
m_lock.unlock();
}
// Clear our data
void clear_data() {
m_data.clear();
#if CLIP_ENABLE_IMAGE
m_image.reset();
#endif
}
void clear() {
clear_data();
// As we want to clear the clipboard content, we set us as the new
// clipboard owner (with an empty clipboard). If this fails, we'll
// try to send a XCB_SELECTION_CLEAR request to the real owner
// (but that can fail anyway because it's a request that the owner
// could ignore).
if (set_x11_selection_owner())
return;
// Clear the clipboard data from the selection owner
const xcb_window_t owner = get_x11_selection_owner();
if (m_window != owner) {
xcb_selection_clear_event_t event;
event.response_type = XCB_SELECTION_CLEAR;
event.pad0 = 0;
event.sequence = 0;
event.time = XCB_CURRENT_TIME;
event.owner = owner;
event.selection = get_atom(CLIPBOARD);
xcb_send_event(m_connection, false,
owner,
XCB_EVENT_MASK_NO_EVENT,
(const char*)&event);
xcb_flush(m_connection);
}
}
bool is_convertible(format f) const {
const atoms atoms = get_format_atoms(f);
const xcb_window_t owner = get_x11_selection_owner();
// If we are the owner, we just can check the m_data map
if (owner == m_window) {
for (xcb_atom_t atom : atoms) {
auto it = m_data.find(atom);
if (it != m_data.end())
return true;
}
}
// Ask to the selection owner the available formats/atoms/targets.
else if (owner) {
return
get_data_from_selection_owner(
{ get_atom(TARGETS) },
[this, &atoms]() -> bool {
assert(m_reply_data);
if (!m_reply_data)
return false;
const xcb_atom_t* sel_atoms = (const xcb_atom_t*)&(*m_reply_data)[0];
int sel_natoms = m_reply_data->size() / sizeof(xcb_atom_t);
auto atoms_begin = atoms.begin();
auto atoms_end = atoms.end();
for (int i=0; i<sel_natoms; ++i) {
if (std::find(atoms_begin,
atoms_end,
sel_atoms[i]) != atoms_end) {
return true;
}
}
return false;
});
}
return false;
}
bool set_data(format f, const char* buf, size_t len) {
if (!set_x11_selection_owner())
return false;
const atoms atoms = get_format_atoms(f);
if (atoms.empty())
return false;
buffer_ptr shared_data_buf = std::make_shared<std::vector<uint8_t>>(len);
std::copy(buf,
buf+len,
shared_data_buf->begin());
for (xcb_atom_t atom : atoms)
m_data[atom] = shared_data_buf;
return true;
}
bool get_data(format f, char* buf, size_t len) const {
const atoms atoms = get_format_atoms(f);
const xcb_window_t owner = get_x11_selection_owner();
if (owner == m_window) {
for (xcb_atom_t atom : atoms) {
auto it = m_data.find(atom);
if (it != m_data.end()) {
size_t n = std::min(len, it->second->size());
std::copy(it->second->begin(),
it->second->begin()+n,
buf);
if (f == text_format()) {
// Add an extra null char
if (n < len)
buf[n] = 0;
}
return true;
}
}
}
else if (owner) {
if (get_data_from_selection_owner(
atoms,
[this, buf, len, f]() -> bool {
size_t n = std::min(len, m_reply_data->size());
std::copy(m_reply_data->begin(),
m_reply_data->begin()+n,
buf);
if (f == text_format()) {
if (n < len)
buf[n] = 0; // Include a null character
}
return true;
})) {
return true;
}
}
return false;
}
size_t get_data_length(format f) const {
size_t len = 0;
const atoms atoms = get_format_atoms(f);
const xcb_window_t owner = get_x11_selection_owner();
if (owner == m_window) {
for (xcb_atom_t atom : atoms) {
auto it = m_data.find(atom);
if (it != m_data.end()) {
len = it->second->size();
break;
}
}
}
else if (owner) {
if (!get_data_from_selection_owner(
atoms,
[this, &len]() -> bool {
len = m_reply_data->size();
return true;
})) {
// Error getting data length
return 0;
}
}
if (f == text_format() && len > 0) {
++len; // Add an extra byte for the null char
}
return len;
}
#if CLIP_ENABLE_IMAGE
bool set_image(const image& image) {
if (!set_x11_selection_owner())
return false;
m_image = image;
#ifdef HAVE_PNG_H
// Put a nullptr in the m_data for image/png format and then we'll
// encode the png data when the image is requested in this format.
m_data[get_atom(MIME_IMAGE_PNG)] = buffer_ptr();
#endif
return true;
}
bool get_image(image& output_img) const {
const xcb_window_t owner = get_x11_selection_owner();
if (owner == m_window) {
if (m_image.is_valid()) {
output_img = m_image;
return true;
}
}
#ifdef HAVE_PNG_H
else if (owner &&
get_data_from_selection_owner(
{ get_atom(MIME_IMAGE_PNG) },
[this, &output_img]() -> bool {
return x11::read_png(&(*m_reply_data)[0],
m_reply_data->size(),
&output_img, nullptr);
})) {
return true;
}
#endif
return false;
}
bool get_image_spec(image_spec& spec) const {
const xcb_window_t owner = get_x11_selection_owner();
if (owner == m_window) {
if (m_image.is_valid()) {
spec = m_image.spec();
return true;
}
}
#ifdef HAVE_PNG_H
else if (owner &&
get_data_from_selection_owner(
{ get_atom(MIME_IMAGE_PNG) },
[this, &spec]() -> bool {
return x11::read_png(&(*m_reply_data)[0],
m_reply_data->size(),
nullptr, &spec);
})) {
return true;
}
#endif
return false;
}
#endif // CLIP_ENABLE_IMAGE
format register_format(const std::string& name) {
xcb_atom_t atom = get_atom(name.c_str());
m_custom_formats.push_back(atom);
return (format)(m_custom_formats.size()-1) + kBaseForCustomFormats;
}
private:
void process_x11_events() {
bool stop = false;
xcb_generic_event_t* event;
while (!stop && (event = xcb_wait_for_event(m_connection))) {
int type = (event->response_type & ~0x80);
switch (type) {
case XCB_DESTROY_NOTIFY:
// To stop the message loop we can just destroy the window
stop = true;
break;
// Someone else has new content in the clipboard, so is
// notifying us that we should delete our data now.
case XCB_SELECTION_CLEAR:
handle_selection_clear_event(
(xcb_selection_clear_event_t*)event);
break;
// Someone is requesting the clipboard content from us.
case XCB_SELECTION_REQUEST:
handle_selection_request_event(
(xcb_selection_request_event_t*)event);
break;
// We've requested the clipboard content and this is the
// answer.
case XCB_SELECTION_NOTIFY:
handle_selection_notify_event(
(xcb_selection_notify_event_t*)event);
break;
case XCB_PROPERTY_NOTIFY:
handle_property_notify_event(
(xcb_property_notify_event_t*)event);
break;
}
free(event);
}
}
void handle_selection_clear_event(xcb_selection_clear_event_t* event) {
if (event->selection == get_atom(CLIPBOARD)) {
std::lock_guard<std::mutex> lock(m_mutex);
clear_data(); // Clear our clipboard data
}
}
void handle_selection_request_event(xcb_selection_request_event_t* event) {
std::lock_guard<std::mutex> lock(m_mutex);
if (event->target == get_atom(TARGETS)) {
atoms targets;
targets.push_back(get_atom(TARGETS));
#ifdef CLIP_SUPPORT_SAVE_TARGETS
targets.push_back(get_atom(SAVE_TARGETS));
targets.push_back(get_atom(MULTIPLE));
#endif
for (const auto& it : m_data)
targets.push_back(it.first);
// Set the "property" of "requestor" with the clipboard
// formats ("targets", atoms) that we provide.
xcb_change_property(
m_connection,
XCB_PROP_MODE_REPLACE,
event->requestor,
event->property,
get_atom(ATOM),
8*sizeof(xcb_atom_t),
targets.size(),
&targets[0]);
}
#ifdef CLIP_SUPPORT_SAVE_TARGETS
else if (event->target == get_atom(SAVE_TARGETS)) {
// Do nothing
}
else if (event->target == get_atom(MULTIPLE)) {
xcb_get_property_reply_t* reply =
get_and_delete_property(event->requestor,
event->property,
get_atom(ATOM_PAIR),
false);
if (reply) {
for (xcb_atom_t
*ptr=(xcb_atom_t*)xcb_get_property_value(reply),
*end=ptr + (xcb_get_property_value_length(reply)/sizeof(xcb_atom_t));
ptr<end; ) {
xcb_atom_t target = *ptr++;
xcb_atom_t property = *ptr++;
if (!set_requestor_property_with_clipboard_content(
event->requestor,
property,
target)) {
xcb_change_property(
m_connection,
XCB_PROP_MODE_REPLACE,
event->requestor,
event->property,
XCB_ATOM_NONE, 0, 0, nullptr);
}
}
free(reply);
}
}
#endif // CLIP_SUPPORT_SAVE_TARGETS
else {
if (!set_requestor_property_with_clipboard_content(
event->requestor,
event->property,
event->target)) {
// If the requested "target" type is not present in our
// clipboard, we continue normally sending a SelectionNotify
// to the "requestor" anyway because some text editors
// (e.g. Emacs) request the TIMESTAMP target (without asking
// if it's present in TARGETS) after asking for UTF8_STRING.
//
// Sending the SelectionNotify will wake up the "requestor"
// that is asking for the clipboard content. In this way we
// avoid a "Timed out waiting for reply from selection owner"
// error in Emacs (and probably other text editors).
}
}
// Notify the "requestor" that we've already updated the property.
xcb_selection_notify_event_t notify;
notify.response_type = XCB_SELECTION_NOTIFY;
notify.pad0 = 0;
notify.sequence = 0;
notify.time = event->time;
notify.requestor = event->requestor;
notify.selection = event->selection;
notify.target = event->target;
notify.property = event->property;
xcb_send_event(m_connection, false,
event->requestor,
XCB_EVENT_MASK_NO_EVENT, // SelectionNotify events go without mask
(const char*)¬ify);
xcb_flush(m_connection);
}
bool set_requestor_property_with_clipboard_content(const xcb_atom_t requestor,
const xcb_atom_t property,
const xcb_atom_t target) {
auto it = m_data.find(target);
if (it == m_data.end()) {
// Nothing to do (unsupported target)
return false;
}
// This can be null of the data was set from an image but we
// didn't encode the image yet (e.g. to image/png format).
if (!it->second) {
encode_data_on_demand(*it);
// Return nothing, the given "target" cannot be constructed
// (maybe by some encoding error).
if (!it->second)
return false;
}
// Set the "property" of "requestor" with the
// clipboard content in the requested format ("target").
xcb_change_property(
m_connection,
XCB_PROP_MODE_REPLACE,
requestor,
property,
target,
8,
it->second->size(),
&(*it->second)[0]);
return true;
}
void handle_selection_notify_event(xcb_selection_notify_event_t* event) {
assert(event->requestor == m_window);
if (event->target == get_atom(TARGETS))
m_target_atom = get_atom(ATOM);
else
m_target_atom = event->target;
xcb_get_property_reply_t* reply =
get_and_delete_property(event->requestor,
event->property,
m_target_atom);
if (reply) {
// In this case, We're going to receive the clipboard content in
// chunks of data with several PropertyNotify events.
if (reply->type == get_atom(INCR)) {
free(reply);
reply = get_and_delete_property(event->requestor,
event->property,
get_atom(INCR));
if (reply) {
if (xcb_get_property_value_length(reply) == 4) {
uint32_t n = *(uint32_t*)xcb_get_property_value(reply);
m_reply_data = std::make_shared<std::vector<uint8_t>>(n);
m_reply_offset = 0;
m_incr_process = true;
m_incr_received = true;
}
free(reply);
}
}
else {
// Simple case, the whole clipboard content in just one reply
// (without the INCR method).
m_reply_data.reset();
m_reply_offset = 0;
copy_reply_data(reply);
call_callback(reply);
free(reply);
}
}
}
void handle_property_notify_event(xcb_property_notify_event_t* event) {
if (m_incr_process &&
event->state == XCB_PROPERTY_NEW_VALUE &&
event->atom == get_atom(CLIPBOARD)) {
xcb_get_property_reply_t* reply =
get_and_delete_property(event->window,
event->atom,
m_target_atom);
if (reply) {
m_incr_received = true;
// When the length is 0 it means that the content was
// completely sent by the selection owner.
if (xcb_get_property_value_length(reply) > 0) {
copy_reply_data(reply);
}
else {
// Now that m_reply_data has the complete clipboard content,
// we can call the m_callback.
call_callback(reply);
m_incr_process = false;
}
free(reply);
}
}
}
xcb_get_property_reply_t* get_and_delete_property(xcb_window_t window,
xcb_atom_t property,
xcb_atom_t atom,
bool delete_prop = true) {
xcb_get_property_cookie_t cookie =
xcb_get_property(m_connection,
delete_prop,
window,
property,
atom,
0, 0x1fffffff); // 0x1fffffff = INT32_MAX / 4
xcb_generic_error_t* err = nullptr;
xcb_get_property_reply_t* reply =
xcb_get_property_reply(m_connection, cookie, &err);
if (err) {
// TODO report error
free(err);
}
return reply;
}
// Concatenates the new data received in "reply" into "m_reply_data"
// buffer.
void copy_reply_data(xcb_get_property_reply_t* reply) {
const uint8_t* src = (const uint8_t*)xcb_get_property_value(reply);
// n = length of "src" in bytes
size_t n = xcb_get_property_value_length(reply);
size_t req = m_reply_offset+n;
if (!m_reply_data) {
m_reply_data = std::make_shared<std::vector<uint8_t>>(req);
}
// The "m_reply_data" size can be smaller because the size
// specified in INCR property is just a lower bound.
else if (req > m_reply_data->size()) {
m_reply_data->resize(req);
}
std::copy(src, src+n, m_reply_data->begin()+m_reply_offset);
m_reply_offset += n;
}
// Calls the current m_callback() to handle the clipboard content
// received from the owner.
void call_callback(xcb_get_property_reply_t* reply) {
m_callback_result = false;
if (m_callback)
m_callback_result = m_callback();
m_cv.notify_one();
m_reply_data.reset();
}
bool get_data_from_selection_owner(const atoms& atoms,
const notify_callback&& callback,
xcb_atom_t selection = 0) const {
if (!selection)
selection = get_atom(CLIPBOARD);
// Put the callback on "m_callback" so we can call it on
// SelectionNotify event.
m_callback = std::move(callback);
// Clear data if we are not the selection owner.
if (m_window != get_x11_selection_owner())
m_data.clear();
// Ask to the selection owner for its content on each known
// text format/atom.
for (xcb_atom_t atom : atoms) {
xcb_convert_selection(m_connection,
m_window, // Send us the result
selection, // Clipboard selection
atom, // The clipboard format that we're requesting
get_atom(CLIPBOARD), // Leave result in this window's property
XCB_CURRENT_TIME);
xcb_flush(m_connection);
// We use the "m_incr_received" to wait several timeouts in case
// that we've received the INCR SelectionNotify or
// PropertyNotify events.
do {
m_incr_received = false;
// Wait a response for 100 milliseconds
std::cv_status status =
m_cv.wait_for(m_lock,
std::chrono::milliseconds(get_x11_wait_timeout()));
if (status == std::cv_status::no_timeout) {
// If the condition variable was notified, it means that the
// callback was called correctly.
return m_callback_result;
}
} while (m_incr_received);
}
// Reset callback
m_callback = notify_callback();
return false;
}
atoms get_atoms(const char** names,
const int n) const {
atoms result(n, 0);
std::vector<xcb_intern_atom_cookie_t> cookies(n);
for (int i=0; i<n; ++i) {
auto it = m_atoms.find(names[i]);
if (it != m_atoms.end())
result[i] = it->second;
else
cookies[i] = xcb_intern_atom(
m_connection, 0,
std::strlen(names[i]), names[i]);
}
for (int i=0; i<n; ++i) {
if (result[i] == 0) {
xcb_intern_atom_reply_t* reply =
xcb_intern_atom_reply(m_connection,
cookies[i],
nullptr);
if (reply) {
result[i] = m_atoms[names[i]] = reply->atom;
free(reply);
}
}
}
return result;
}
xcb_atom_t get_atom(const char* name) const {
auto it = m_atoms.find(name);
if (it != m_atoms.end())
return it->second;
xcb_atom_t result = 0;
xcb_intern_atom_cookie_t cookie =
xcb_intern_atom(m_connection, 0,
std::strlen(name), name);
xcb_intern_atom_reply_t* reply =
xcb_intern_atom_reply(m_connection,
cookie,
nullptr);
if (reply) {
result = m_atoms[name] = reply->atom;
free(reply);
}
return result;
}
xcb_atom_t get_atom(CommonAtom i) const {
if (m_common_atoms.empty()) {
m_common_atoms =
get_atoms(kCommonAtomNames,
sizeof(kCommonAtomNames) / sizeof(kCommonAtomNames[0]));
}
return m_common_atoms[i];
}
const atoms& get_text_format_atoms() const {
if (m_text_atoms.empty()) {
const char* names[] = {
// Prefer utf-8 formats first
"UTF8_STRING",
"text/plain;charset=utf-8",
"text/plain;charset=UTF-8",
"GTK_TEXT_BUFFER_CONTENTS", // Required for gedit (and maybe gtk+ apps)
// ANSI C strings?
"STRING",
"TEXT",
"text/plain",
};
m_text_atoms = get_atoms(names, sizeof(names) / sizeof(names[0]));
}
return m_text_atoms;
}
#if CLIP_ENABLE_IMAGE
const atoms& get_image_format_atoms() const {
if (m_image_atoms.empty()) {
#ifdef HAVE_PNG_H
m_image_atoms.push_back(get_atom(MIME_IMAGE_PNG));
#endif
}
return m_image_atoms;
}
#endif // CLIP_ENABLE_IMAGE
atoms get_format_atoms(const format f) const {
atoms atoms;
if (f == text_format()) {
atoms = get_text_format_atoms();
}
#if CLIP_ENABLE_IMAGE
else if (f == image_format()) {
atoms = get_image_format_atoms();
}
#endif // CLIP_ENABLE_IMAGE
else {
xcb_atom_t atom = get_format_atom(f);
if (atom)
atoms.push_back(atom);
}
return atoms;
}
#if !defined(NDEBUG)
// This can be used to print debugging messages.
std::string get_atom_name(xcb_atom_t atom) const {
std::string result;
xcb_get_atom_name_cookie_t cookie =
xcb_get_atom_name(m_connection, atom);
xcb_generic_error_t* err = nullptr;
xcb_get_atom_name_reply_t* reply =
xcb_get_atom_name_reply(m_connection, cookie, &err);
if (err) {
free(err);
}
if (reply) {
int len = xcb_get_atom_name_name_length(reply);
if (len > 0) {
result.resize(len);
char* name = xcb_get_atom_name_name(reply);
if (name)
std::copy(name, name+len, result.begin());
}
free(reply);
}
return result;
}
#endif
bool set_x11_selection_owner() const {
xcb_void_cookie_t cookie =
xcb_set_selection_owner_checked(m_connection,
m_window,
get_atom(CLIPBOARD),
XCB_CURRENT_TIME);
xcb_generic_error_t* err =
xcb_request_check(m_connection,
cookie);
if (err) {
free(err);
return false;
}
return true;
}
xcb_window_t get_x11_selection_owner() const {
xcb_window_t result = 0;
xcb_get_selection_owner_cookie_t cookie =
xcb_get_selection_owner(m_connection,
get_atom(CLIPBOARD));
xcb_get_selection_owner_reply_t* reply =
xcb_get_selection_owner_reply(m_connection, cookie, nullptr);
if (reply) {
result = reply->owner;
free(reply);
}
return result;
}
xcb_atom_t get_format_atom(const format f) const {
int i = f - kBaseForCustomFormats;
if (i >= 0 && i < int(m_custom_formats.size()))
return m_custom_formats[i];
else
return 0;
}
void encode_data_on_demand(std::pair<const xcb_atom_t, buffer_ptr>& e) {
#if defined(CLIP_ENABLE_IMAGE) && defined(HAVE_PNG_H)
if (e.first == get_atom(MIME_IMAGE_PNG)) {
assert(m_image.is_valid());
if (!m_image.is_valid())
return;
std::vector<uint8_t> output;
if (x11::write_png(m_image, output)) {
e.second =
std::make_shared<std::vector<uint8_t>>(
std::move(output));
}
// else { TODO report png conversion errors }
}
#endif // defined(CLIP_ENABLE_IMAGE) && defined(HAVE_PNG_H)
}
// Access to the whole Manager
std::mutex m_mutex;
// Lock used in the main thread using the Manager (i.e. by lock::impl)
mutable std::unique_lock<std::mutex> m_lock;
// Connection to X11 server
xcb_connection_t* m_connection;
// Temporal background window used to own the clipboard and process
// all events related about the clipboard in a background thread
xcb_window_t m_window;
// Used to wait/notify the arrival of the SelectionNotify event when
// we requested the clipboard content from other selection owner.
mutable std::condition_variable m_cv;
// Thread used to run a background message loop to wait X11 events
// about clipboard. The X11 selection owner will be a hidden window
// created by us just for the clipboard purpose/communication.
std::thread m_thread;
// Internal callback used when a SelectionNotify is received (or the
// whole data content is received by the INCR method). So this
// callback can use the notification by different purposes (e.g. get
// the data length only, or get/process the data content, etc.).
mutable notify_callback m_callback;
// Result returned by the m_callback. Used as return value in the
// get_data_from_selection_owner() function. For example, if the
// callback must read a "image/png" file from the clipboard data and
// fails, the callback can return false and finally the get_image()
// will return false (i.e. there is data, but it's not a valid image
// format).
std::atomic<bool> m_callback_result;
// Cache of known atoms
mutable std::map<std::string, xcb_atom_t> m_atoms;