wingy-x11 0.31.1

Wingy's X11 backend
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
//! Types related to data transfer (drag-and-drop and clipboard) on X11.

use std::collections::VecDeque;
use std::ffi::OsStr;
use std::io;
use std::os::raw::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use wingy_core::data_transfer::{
    DataTransfer, DataTransferId, DataTransferSend, SendData, TransferType, TypeHint, TypedData,
};
use wingy_core::error::TransferError;
use wingy_core::event::WindowEvent;
use wingy_core::event_loop::AsyncRequestSerial;
use wingy_core::window::WindowId;
use x11_dl::xlib::{XPropertyEvent, XSelectionEvent, XSelectionRequestEvent};
use x11rb::CURRENT_TIME;
use x11rb::errors::ReplyError;
use x11rb::protocol::xfixes::{self, ConnectionExt as _};
use x11rb::protocol::xproto::{self, ConnectionExt};

use crate::atoms::AtomName::NoneAtom as DndNone;
use crate::atoms::*;
use crate::event_loop::{CookieResultExt, X11Error};
use crate::xdisplay::XConnection;
use crate::{XWindow, util};

/// The maximum number of bytes when we are sending data
const INCR_CHUNK_SIZE_BYTES: usize = 1024;

/// TODO: this is copied from wayland. Is this the right approach for X11 as well?
fn encode_uri_list<I>(uri_list: I) -> Vec<u8>
where
    I: IntoIterator,
    I::Item: AsRef<OsStr>,
{
    let mut out = Vec::new();

    for uri in uri_list {
        out.extend_from_slice(OsStr::new(&uri).as_encoded_bytes());
        out.extend_from_slice(b"\r\n");
    }

    out
}

#[derive(Debug, Clone, Copy)]
pub enum DndState {
    Accepted,
    Rejected,
}

#[derive(Debug)]
pub struct SelectionReader {
    type_: SelectionType,
    data: Vec<u8>,
}

impl TypedData for SelectionReader {
    fn try_read(&self) -> Option<Box<dyn io::BufRead>> {
        Some(Box::new(io::Cursor::new(self.data.clone())))
    }

    fn type_(&self) -> &dyn TransferType {
        &self.type_
    }

    fn try_as_string(&self) -> io::Result<String> {
        fn invalid_data<E>(err: E) -> io::Error
        where
            E: Into<Box<dyn std::error::Error + Send + Sync>>,
        {
            io::Error::new(io::ErrorKind::InvalidData, err)
        }

        fn decode_utf16_bytes(bytes: &[u8]) -> io::Result<String> {
            let utf16 = bytes
                .chunks_exact(2)
                .map(|chunk| {
                    let bytes: &[u8; 2] = chunk.try_into().unwrap();
                    u16::from_ne_bytes(*bytes)
                })
                .collect::<Vec<_>>();
            String::from_utf16(&utf16).map_err(invalid_data)
        }

        match self.type_.hint() {
            Some(TypeHint::Plaintext) | Some(TypeHint::Html) => std::str::from_utf8(&self.data)
                .map(|str| str.to_owned())
                .map_err(invalid_data)
                .or_else(|_| decode_utf16_bytes(&self.data)),
            Some(TypeHint::UriList) => String::from_utf8(self.data.clone()).map_err(invalid_data),
            _ => Err(io::ErrorKind::InvalidData.into()),
        }
    }

    fn try_as_uris(&self) -> io::Result<Vec<String>> {
        if self.type_().hint() != Some(TypeHint::UriList) {
            return Err(io::ErrorKind::InvalidData.into());
        }

        Ok(self
            .try_as_string()?
            .split(['\n', '\r'])
            .filter(|s| !s.is_empty())
            .map(ToOwned::to_owned)
            .collect())
    }
}

#[derive(Debug)]
pub struct DragState {
    // Populated by XdndEnter event handler
    pub version: c_long,
    pub transfer_id: DataTransferId,
    pub types: Arc<[SelectionType]>,
    // Populated by Xdnd* event handlers
    pub source_window: xproto::Window,
    // Populated by Xdnd* event handlers
    pub target_window: xproto::Window,
    // Populated by `fetch_data_transfer`
    pub pending_fetch_types: VecDeque<(AsyncRequestSerial, SelectionType)>,
    pub finished: Option<(XWindow, XWindow)>,
    /// Whether the drag operation is accepted (or `None` if the user never indicated that it's
    /// accepted or rejected)
    // Populated by `Window::accept_drag`/`Window::reject_drag`.
    pub accepted: bool,
}

/// Create a new [`DataTransferId`] from a global counter
fn generate_transfer_id() -> DataTransferId {
    static DATA_TRANSFER_ID: AtomicUsize = AtomicUsize::new(0);
    DataTransferId::from_raw(DATA_TRANSFER_ID.fetch_add(1, Ordering::Relaxed).try_into().unwrap())
}

impl Default for DragState {
    fn default() -> Self {
        Self {
            version: Default::default(),
            transfer_id: generate_transfer_id(),
            types: Default::default(),
            source_window: Default::default(),
            target_window: Default::default(),
            pending_fetch_types: Default::default(),
            finished: None,
            accepted: Default::default(),
        }
    }
}

/// There are three standard selections defined in the ICCCM: PRIMARY, SECONDARY, and CLIPBOARD.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(usize)]
pub enum ClipboardSelectionType {
    /// Primary selection, often used for the currently selected text
    Primary,
    /// Secondary selection, defined by the ICCCM but unused in practice, see
    /// <https://www.freedesktop.org/wiki/Specifications/ClipboardsWiki/>.
    Secondary,
    /// Clipboard, used for Ctrl+C Ctrl+V pasting
    Clipboard,
}

impl ClipboardSelectionType {
    pub const COUNT: usize = 3;

    /// An atom representing that selection
    #[must_use]
    pub fn selection_atom(self, atoms: &crate::atoms::Atoms) -> xproto::Atom {
        match self {
            Self::Primary => xproto::AtomEnum::PRIMARY.into(),
            Self::Secondary => xproto::AtomEnum::SECONDARY.into(),
            Self::Clipboard => atoms[CLIPBOARD],
        }
    }

    /// Try to convert an atom representing a selection back into a clipboard type
    #[must_use]
    pub fn from_atom(atoms: &crate::atoms::Atoms, atom: xproto::Atom) -> Option<Self> {
        match atom {
            _ if Self::Primary.selection_atom(atoms) == atom => Some(Self::Primary),
            _ if Self::Secondary.selection_atom(atoms) == atom => Some(Self::Secondary),
            _ if Self::Clipboard.selection_atom(atoms) == atom => Some(Self::Clipboard),
            _ => None,
        }
    }

    /// Iterate through all clipboards
    pub fn iter() -> impl Iterator<Item = Self> {
        [Self::Primary, Self::Secondary, Self::Clipboard].into_iter()
    }

    /// An atom representing a property that can be used for reading (without conflicting with other
    /// clipboards that might also be reading their TARGETS)
    #[must_use]
    pub fn property_atom(self, atoms: &crate::atoms::Atoms) -> xproto::Atom {
        match self {
            Self::Primary => atoms[WINGY_TARGETS_PRIMARY],
            Self::Secondary => atoms[WINGY_TARGETS_SECONDARY],
            Self::Clipboard => atoms[WINGY_TARGETS_CLIPBOARD],
        }
    }
}

/// State for the INCR clipboard protocol. The protocol works as follows:
/// - Paster calls convert_selection
/// - Copier receives a SelectionNotify event and returns empty data and a type of INCR
/// - Paster deletes the property where the empty data was sent
/// - Copier listens for the delete property message and then puts the first chunk of actual data
/// - Paster listens for the new property message and grabs the data, then deletes the property
///   again
/// - Copier puts then next chunk of data
/// - etc.
/// - When the copier puts an empty chunk of data, the sequence is over
#[derive(Debug)]
struct IncrState {
    /// The partial bytes of data
    data: Vec<u8>,
    /// The place where the INCR protocol takes place (is modified with each chunk) e.g.
    /// CLIPBOARD or META_SELECTION (not necessarily the same as the selection)
    property: xproto::Atom,
    /// The data type transmitted e.g. UTF8_STRING
    ty: SelectionType,
    clipboard: ClipboardSelectionType,
}

#[derive(Debug)]
pub struct ClipboardState {
    /// Data for each [`ClipboardSelection`] if held by this application (data is copied from the
    /// application and sent to others)
    owned_data: Option<Box<dyn DataTransferSend>>,
    /// ID for the current clipboard (each time it is changed this is updated; multiple pastes do
    /// not change this)
    transfer_serial: DataTransferId,
    /// The clipboard notify event serial, used for deduplicating events
    x11_serial: u64,
    /// Cached types available for pasting
    types: Arc<[SelectionType]>,
    /// A list of the fetch requests with the first one being in progress
    pending_fetch_types: VecDeque<(AsyncRequestSerial, SelectionType)>,
}

impl Default for ClipboardState {
    fn default() -> Self {
        Self {
            owned_data: Default::default(),
            transfer_serial: generate_transfer_id(),
            x11_serial: u64::MAX,
            types: Default::default(),
            pending_fetch_types: Default::default(),
        }
    }
}

impl ClipboardState {
    pub fn get_types(&self, atoms: &Atoms) -> Selection {
        if let Some(owned_data) = &self.owned_data {
            let mut accepted: Vec<SelectionType> = Vec::new();

            owned_data.for_each_available_type(&mut |ty| {
                accepted.extend(SelectionType::from_dyn(atoms, ty));
                core::ops::ControlFlow::Continue(())
            });

            return Selection::new(accepted.into());
        }
        Selection::new(Arc::clone(&self.types))
    }
    /// Adds a request to the fetch, returning true if the fetch should start immediately
    pub fn add_to_fetch(&mut self, serial: AsyncRequestSerial, ty: SelectionType) -> bool {
        self.pending_fetch_types.push_back((serial, ty));
        self.pending_fetch_types.len() == 1
    }

    /// From the list of types available on the clipboard, find the first available type that
    /// matches the hint.
    pub fn find_type_by_hint(&self, hint: TypeHint, atoms: &Atoms) -> Option<SelectionType> {
        if let Some(owned_data) = &self.owned_data {
            let mut result = None;
            owned_data.for_each_available_type(&mut |ty| {
                if ty.matches(&hint) {
                    result = SelectionType::from_dyn(atoms, ty).next();
                }
                if result.is_some() {
                    core::ops::ControlFlow::Break(())
                } else {
                    core::ops::ControlFlow::Continue(())
                }
            });
            result
        } else {
            self.types.iter().find(|haystack| haystack.hint() == Some(hint)).cloned()
        }
    }
}

#[derive(Debug)]
pub struct DataTransferState {
    xconn: Arc<XConnection>,
    // If `None`, no drag operation is in progress.
    state: Option<DragState>,

    clipboards: [ClipboardState; ClipboardSelectionType::COUNT],
    /// Contains the remnants of the message that are yet to be sent to the current clipboard
    /// recipient using the INCR ICCCM protocol
    clip_incr_send: Vec<IncrState>,
    /// Contains the partially received message from the clipboard using the INCR ICCCM protocol
    clip_incr_receive: Vec<IncrState>,
}

#[derive(Debug)]
pub struct Selection {
    types: Arc<[SelectionType]>,
}

impl Selection {
    pub(crate) fn new(types: Arc<[SelectionType]>) -> Selection {
        Selection { types }
    }
}

pub struct FinishedDataTransfer {
    id: DataTransferId,
    serial: AsyncRequestSerial,
    value: Arc<dyn TypedData>,
    window: WindowId,
}

impl FinishedDataTransfer {
    #[must_use]
    pub fn window(&self) -> WindowId {
        self.window
    }
    #[must_use]
    pub fn into_event(self) -> WindowEvent {
        WindowEvent::DataTransferReceived { id: self.id, serial: self.serial, value: self.value }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SelectionType {
    hint: Option<TypeHint>,
    atom: xproto::Atom,
}

impl SelectionType {
    fn atom_hints(atoms: &Atoms) -> impl Iterator<Item = (xproto::Atom, TypeHint)> {
        [
            // Files
            (atoms[TextUriList], TypeHint::UriList),
            // Plaintext
            (atoms[STRING], TypeHint::Plaintext),
            (atoms[UTF8_STRING], TypeHint::Plaintext),
            (atoms[TextPlain], TypeHint::Plaintext),
            (atoms[TextPlainCharsetUtf8], TypeHint::Plaintext),
            // HTML
            (atoms[TextHtml], TypeHint::Html),
            (atoms[TextHtmlCharsetUtf8], TypeHint::Html),
            // RTF
            (atoms[ApplicationRtf], TypeHint::Rtf),
            // Audio
            (atoms[AudioAac], TypeHint::Audio { extension_hint: Some("aac") }),
            (atoms[AudioAiff], TypeHint::Audio { extension_hint: Some("aif") }),
            (atoms[AudioFlac], TypeHint::Audio { extension_hint: Some("flac") }),
            (atoms[AudioVndWav], TypeHint::Audio { extension_hint: Some("wav") }),
            (atoms[AudioVndWave], TypeHint::Audio { extension_hint: Some("wav") }),
            (atoms[AudioWav], TypeHint::Audio { extension_hint: Some("wav") }),
            (atoms[AudioWave], TypeHint::Audio { extension_hint: Some("wav") }),
            (atoms[AudioXWav], TypeHint::Audio { extension_hint: Some("wav") }),
            (atoms[AudioOgg], TypeHint::Audio { extension_hint: Some("ogg") }),
            (atoms[AudioMpeg], TypeHint::Audio { extension_hint: Some("mp3") }),
            // Image
            (atoms[ImageBmp], TypeHint::Image { extension_hint: Some("bmp") }),
            (atoms[ImageGif], TypeHint::Image { extension_hint: Some("gif") }),
            (atoms[ImageJpeg], TypeHint::Image { extension_hint: Some("jpg") }),
            (atoms[ImagePjpeg], TypeHint::Image { extension_hint: Some("jpg") }),
            (atoms[ImagePng], TypeHint::Image { extension_hint: Some("png") }),
            (atoms[ImageRaw], TypeHint::Image { extension_hint: Some("raw") }),
            (atoms[ImageSvg], TypeHint::Image { extension_hint: Some("svg") }),
            (atoms[ImageTiff], TypeHint::Image { extension_hint: Some("tiff") }),
            (atoms[ImageWebp], TypeHint::Image { extension_hint: Some("webp") }),
            (atoms[ImageXIcon], TypeHint::Image { extension_hint: Some("ico") }),
        ]
        .into_iter()
    }

    pub(crate) fn new(atoms: &Atoms, atom: xproto::Atom) -> Self {
        let hint =
            Self::atom_hints(atoms).find_map(|(haystack, hint)| (haystack == atom).then_some(hint));

        Self { hint, atom }
    }

    fn from_dyn(atoms: &Atoms, type_: &dyn TransferType) -> impl Iterator<Item = Self> {
        let downcast = type_.cast_ref::<Self>().cloned();
        let downcast_failed = downcast.is_none();
        // This filter is a bit hacky, but it's the only way to ensure that we always
        // return the same type.
        let from_hint = downcast_failed
            .then_some(
                Self::atom_hints(atoms)
                    .filter(|(_, haystack)| TransferType::matches(haystack, type_))
                    .map(|(atom, hint)| Self { atom, hint: Some(hint) }),
            )
            .into_iter()
            .flatten();

        downcast.into_iter().chain(from_hint)
    }

    pub fn atom(&self) -> xproto::Atom {
        self.atom
    }
}

impl TransferType for SelectionType {
    fn hint(&self) -> Option<TypeHint> {
        self.hint
    }

    fn matches(&self, other: &dyn TransferType) -> bool {
        if let Some(other_mime) = other.cast_ref::<Self>() {
            *self == *other_mime
        } else {
            self.hint().is_some_and(|hint| other.hint() == Some(hint))
        }
    }
}

impl DataTransfer for Selection {
    fn for_each_available_type<'this>(
        &'this self,
        func: &'_ mut dyn FnMut(&'this dyn TransferType) -> std::ops::ControlFlow<()>,
    ) {
        let _ = self.types.iter().map(|mime| mime as &dyn TransferType).try_for_each(func);
    }
}

impl DataTransferState {
    pub fn new(xconn: Arc<XConnection>) -> Self {
        let conn = xconn.xcb_connection();

        // Listen for all selection owner changes for the clipboard. This is a bit hacky but we must
        // know all of the types when the user wants to paste.
        match conn.xfixes_query_version(5, 0).map_err(ReplyError::from).and_then(|c| c.reply()) {
            Ok(_) => {
                let root = xconn.default_root().root;
                for item in ClipboardSelectionType::iter() {
                    conn.xfixes_select_selection_input(
                        root,
                        item.selection_atom(xconn.atoms()),
                        xfixes::SelectionEventMask::SET_SELECTION_OWNER,
                    )
                    .expect_then_ignore_error("Failed to select selection owner changes");
                }
            },
            Err(err) => error!("Failed to query the XFixes version: {err}"),
        }

        DataTransferState {
            xconn,
            state: Default::default(),
            clipboards: Default::default(),
            clip_incr_send: Default::default(),
            clip_incr_receive: Default::default(),
        }
    }

    pub fn state(&self) -> Option<&DragState> {
        self.state.as_ref()
    }

    pub fn state_mut(&mut self) -> Option<&mut DragState> {
        self.state.as_mut()
    }

    pub fn find_type_by_hint(&self, hint: TypeHint) -> Option<&SelectionType> {
        self.state.as_ref()?.types.iter().find(|haystack| haystack.hint() == Some(hint))
    }

    pub fn init_state(
        &mut self,
        version: c_long,
        source_window: xproto::Window,
        target_window: xproto::Window,
        types: Arc<[SelectionType]>,
    ) -> &DragState {
        self.state.insert(DragState {
            version,
            types,
            source_window,
            target_window,
            ..Default::default()
        })
    }

    pub fn reset_state(&mut self) -> Option<DragState> {
        self.state.take()
    }

    pub unsafe fn send_finished(
        &self,
        this_window: xproto::Window,
        target_window: xproto::Window,
    ) -> Result<(), X11Error> {
        let atoms = self.xconn.atoms();
        let Some(state) = &self.state else {
            return Err(X11Error::UnexpectedNull(
                "Drag-and-drop state was not initialized (called `send_finished` before XdndEnter",
            ));
        };
        let (accepted, action) =
            if state.accepted { (1, atoms[XdndActionCopy]) } else { (0, atoms[DndNone]) };
        self.xconn
            .send_client_msg(
                target_window,
                target_window,
                atoms[XdndFinished] as _,
                None,
                [this_window, accepted, action as _, 0, 0],
            )?
            .ignore_error();

        Ok(())
    }

    pub unsafe fn get_type_list(
        &self,
        source_window: xproto::Window,
    ) -> Result<Vec<xproto::Atom>, util::GetPropertyError> {
        let atoms = self.xconn.atoms();
        self.xconn.get_property(
            source_window,
            atoms[XdndTypeList],
            xproto::Atom::from(xproto::AtomEnum::ATOM),
        )
    }

    pub unsafe fn send_status(
        &self,
        this_window: xproto::Window,
        target_window: xproto::Window,
        status: DndState,
    ) -> Result<(), X11Error> {
        let atoms = self.xconn.atoms();
        let (accepted, action) = match status {
            DndState::Accepted => (1, atoms[XdndActionCopy]),
            DndState::Rejected => (0, atoms[DndNone]),
        };
        self.xconn
            .send_client_msg(
                target_window,
                target_window,
                atoms[XdndStatus] as _,
                None,
                [this_window, accepted, 0, 0, action as _],
            )?
            .ignore_error();

        Ok(())
    }

    pub fn read_data(
        &self,
        window: xproto::Window,
        type_: SelectionType,
    ) -> Result<SelectionReader, util::GetPropertyError> {
        let atoms = self.xconn.atoms();
        let type_atom = type_.atom();
        let bytes = self.xconn.get_property(window, atoms[XdndSelection], type_atom)?;

        Ok(SelectionReader { type_, data: bytes })
    }

    /// Gets the [`ClipboardSelectionType`] associated with the specified [`DataTransferId`].
    pub fn resolve_clipboard_type(
        &self,
        data_transfer: DataTransferId,
    ) -> Option<ClipboardSelectionType> {
        ClipboardSelectionType::iter()
            .find(|&clip| self.clipboards[clip as usize].transfer_serial == data_transfer)
    }

    pub fn get_clipboard(&self, clipboard: ClipboardSelectionType) -> &ClipboardState {
        &self.clipboards[clipboard as usize]
    }

    pub fn get_clipboard_mut(&mut self, clipboard: ClipboardSelectionType) -> &mut ClipboardState {
        &mut self.clipboards[clipboard as usize]
    }

    /// Requests to access the clipboard have many properties. Returned true if successfully
    /// resolved.
    pub fn attach_clipboard_property(&mut self, xev: &XSelectionRequestEvent) -> bool {
        let atoms = self.xconn.atoms();
        // For strange reasons, all the values in the input request are u64 whereas the sending uses
        // a different library with u32s
        let prop = xev.property as xproto::Atom;
        let target = xev.target as xproto::Atom;
        let requestor = xev.requestor as xproto::Window;

        let Some(resolved_clipboard) = ClipboardSelectionType::from_atom(atoms, xev.selection as _)
        else {
            warn!("Received request for unknown target clipboard {}", self.xconn.atom_str(prop));
            return false;
        };
        let Some(owned_data) = &self.get_clipboard(resolved_clipboard).owned_data else {
            warn!("Received request for clipboard with no data {:?}", resolved_clipboard);
            return false;
        };

        let replace = xproto::PropMode::REPLACE;

        // Choose the handler for the type of selection
        if target == atoms[TARGETS] as _ {
            // We must advertise at least TIMESTAMP and TARGETS
            let mut accepted: Vec<xproto::Atom> = vec![atoms[TIMESTAMP] as _, atoms[TARGETS] as _];

            // Add user defined types if possible
            owned_data.for_each_available_type(&mut |ty| {
                accepted.extend(SelectionType::from_dyn(atoms, ty).map(|ty| ty.atom()));
                core::ops::ControlFlow::Continue(())
            });

            self.xconn
                .change_property(requestor, prop, xproto::AtomEnum::ATOM.into(), replace, &accepted)
                .expect_then_ignore_error("unable to change property to set TARGETS");

            let advertised =
                accepted.iter().map(|&atom| self.xconn.atom_str(atom)).collect::<Vec<_>>();
            trace!("Written TARGETS {advertised:?} into property {} ", self.xconn.atom_str(prop));

            true
        } else if target == atoms[TIMESTAMP] as _ {
            self.xconn
                .change_property(
                    requestor,
                    prop,
                    xproto::AtomEnum::INTEGER.into(),
                    replace,
                    &[CURRENT_TIME],
                )
                .expect_then_ignore_error("unable to change property to set TIMESTAMP");
            trace!("Written TIMESTAMP into property {} ", self.xconn.atom_str(prop));

            true
        } else {
            let ty = SelectionType::new(atoms, target);
            let Some(send_data) = owned_data.data_for_type(&ty) else {
                warn!(
                    "Selection request for unknown selection target {}",
                    self.xconn.atom_str(target)
                );
                return false;
            };
            let data = match send_data {
                SendData::Uris(strings) => encode_uri_list(strings),
                SendData::String(str) => str.into_bytes(),
                SendData::Bytes(binary) => binary,
                _ => {
                    warn!("Unknown send data {send_data:?}");
                    return false;
                },
            };

            if data.len() < INCR_CHUNK_SIZE_BYTES {
                // We can skip using the INCR for short messages.
                self.xconn
                    .change_property(requestor, prop, ty.atom(), replace, &data)
                    .expect_then_ignore_error("unable to change property to send clipboard data");
                trace!(
                    "Directly written data of type {} into {} ",
                    self.xconn.atom_str(target),
                    self.xconn.atom_str(prop)
                );
            } else if let Err(e) = self.start_incr_sender(xev, data, ty) {
                warn!("Unable to start INCR sender {e}");
                return false;
            }
            true
        }
    }

    /// Sets up the sending for the INCR
    fn start_incr_sender(
        &mut self,
        xev: &XSelectionRequestEvent,
        data: Vec<u8>,
        ty: SelectionType,
    ) -> Result<(), crate::event_loop::X11Error> {
        let xconn = &self.xconn;
        let atoms = xconn.atoms();

        let property = xev.property as xproto::Atom;
        let requestor = xev.requestor as xproto::Window;
        let Some(clipboard) = ClipboardSelectionType::from_atom(atoms, xev.selection as _) else {
            return Err(X11Error::UnexpectedNull("getting clipboard selection"));
        };

        let current_attributes =
            xconn.xcb_connection().get_window_attributes(requestor)?.reply()?;
        // Add the property change listener to the existing mask so we do not delete bits
        let event_mask =
            Some(current_attributes.your_event_mask | xproto::EventMask::PROPERTY_CHANGE);

        // For INCR we must know when the recipient deletes the property to add some more
        // Therefore we listen for property change events on the other window
        xconn.xcb_connection().change_window_attributes(
            requestor,
            &xproto::ChangeWindowAttributesAux { event_mask, ..Default::default() },
        )?;
        self.clip_incr_send.push(IncrState { data, property, ty, clipboard });
        xconn
            .change_property::<u32>(
                requestor,
                property,
                atoms[INCR],
                xproto::PropMode::REPLACE,
                &[],
            )
            .expect_then_ignore_error("unable to change property to start INCR sender");
        trace!("Starting INCR sender to {requestor} property {}", xconn.atom_str(property));

        Ok(())
    }

    /// Setup an empty buffer to receive data via the INCR protocol
    pub fn start_incr_receiver(&mut self, xev: &XSelectionEvent) {
        let property = xev.property as _;
        let target = xev.target as _;
        let Some(clipboard) =
            ClipboardSelectionType::from_atom(self.xconn.atoms(), xev.selection as _)
        else {
            warn!("Invalid clipboard for starting INCR receiver");
            return;
        };
        let ty = SelectionType::new(self.xconn.atoms(), target);
        trace!(
            "Starting INCR receiver {ty:?} property {} target {}  requestor {}",
            self.xconn.atom_str(property),
            self.xconn.atom_str(target),
            xev.requestor
        );
        self.clip_incr_receive.push(IncrState { data: Vec::new(), property, ty, clipboard });
    }

    /// Check if we are the ones receiving and there is some new value for the clipboard property
    #[must_use]
    pub fn check_incr_receive_data(
        &mut self,
        xev: &XPropertyEvent,
    ) -> Option<FinishedDataTransfer> {
        let atom = xev.atom as xproto::Atom;
        let xwindow = xev.window as xproto::Window;

        // We only care about new values
        if xev.state as u32 != xproto::Property::NEW_VALUE.into() {
            return None;
        }

        let index = self
            .clip_incr_receive
            .iter()
            .position(|IncrState { property, .. }| *property == atom)?;

        let IncrState { data, .. } = &mut self.clip_incr_receive[index];

        let (_, read_bytes) = match self.xconn.get_dynamic_property(atom, xwindow, data) {
            Ok(value) => value,
            Err(e) => {
                error!("Unable to read property to check INCR receive: {e}");
                self.clip_incr_receive.remove(index); // Delete if broken to avoid memory leak
                return None;
            },
        };
        if read_bytes != 0 {
            return None;
        }
        let IncrState { data, property, ty, clipboard } = self.clip_incr_receive.remove(index);
        trace!(
            "We are done receiving from {} ty {}",
            self.xconn.atom_str(property),
            self.xconn.atom_str(ty.atom())
        );

        self.send_clipboard_data(xwindow, clipboard, data)
    }

    /// Start the next clipboard fetch from the queue.
    pub fn do_next_clipboard_fetch(
        &mut self,
        clipboard: ClipboardSelectionType,
        xwindow: xproto::Window,
    ) {
        let selection = clipboard.selection_atom(self.xconn.atoms());
        while let Some((_, ty)) = self.get_clipboard(clipboard).pending_fetch_types.front() {
            match self.xconn.convert_selection(xwindow, selection, ty.atom(), selection) {
                Ok(()) => {
                    trace!("Converting next selection {}", self.xconn.atom_str(ty.atom()));
                    return;
                },
                Err(e) => {
                    self.get_clipboard_mut(clipboard).pending_fetch_types.pop_front();
                    error!("Could not convert next selection so skipping: {e}");
                    continue;
                },
            }
        }
    }

    /// Using the pending clipboard values, send the data to the app with a
    /// [`WindowEvent::DataTransferReceived`].
    #[must_use]
    pub fn send_clipboard_data(
        &mut self,
        xwindow: xproto::Window,
        clipboard: ClipboardSelectionType,
        data: Vec<u8>,
    ) -> Option<FinishedDataTransfer> {
        let Some((serial, type_)) =
            self.get_clipboard_mut(clipboard).pending_fetch_types.pop_front()
        else {
            warn!("Got finished fetch but no fetches were pending");
            return None;
        };
        let value = Arc::new(SelectionReader { type_, data });
        let id = self.get_clipboard(clipboard).transfer_serial;

        self.do_next_clipboard_fetch(clipboard, xwindow);

        let window = crate::event_loop::mkwid(xwindow);
        Some(FinishedDataTransfer { id, serial, value, window })
    }

    /// Check if we are the ones sending and the receiver deleted their property for the clipboard
    /// then update
    pub fn check_incr_send_data(&mut self, xev: &XPropertyEvent) {
        let atom = xev.atom as xproto::Atom;
        let xwindow = xev.window as xproto::Window;

        // We only care about deleted values
        if xev.state as u32 != xproto::Property::DELETE.into() {
            return;
        }

        self.clip_incr_send.retain_mut(|IncrState { data, property, ty, .. }| {
            if *property != atom {
                return true;
            }

            let bytes_to_take = INCR_CHUNK_SIZE_BYTES.min(data.len());
            let sending = &data[..bytes_to_take];
            if let Err(err) = self.xconn.change_property(
                xwindow,
                atom,
                ty.atom(),
                xproto::PropMode::REPLACE,
                sending,
            ) {
                error!("Unable to change property {} for INCR: {}", self.xconn.atom_str(atom), err);
                return false; // Delete broken properties to avoid memory leak
            }
            trace!("Written partial data {:?} to {} ", sending, self.xconn.atom_str(atom),);
            data.drain(..bytes_to_take);
            if bytes_to_take != 0 {
                return true;
            }
            trace!(
                "We are done sending {} ty {}",
                self.xconn.atom_str(*property),
                self.xconn.atom_str(ty.atom())
            );
            false
        });
    }

    /// From the TARGETS request, populate the relevant clipboard
    pub fn populate_targets(&mut self, selection: xproto::Atom, data: Vec<u8>) {
        let targets_bytes = data.chunks_exact(size_of::<xproto::Atom>());
        if !targets_bytes.remainder().is_empty() {
            warn!("Trailing bytes in list of targets from X11");
        }
        let targets: Vec<_> = targets_bytes
            .map(|bytes| xproto::Atom::from_ne_bytes(bytes.try_into().unwrap()))
            .map(|atom| SelectionType::new(self.xconn.atoms(), atom))
            .filter(|ty| ty.hint.is_some())
            .collect();

        let Some(clipboard) = ClipboardSelectionType::from_atom(self.xconn.atoms(), selection)
        else {
            warn!("Got TARGETS for an unknown selection {}", self.xconn.atom_str(selection));
            return;
        };

        let available = targets.iter().map(|t| self.xconn.atom_str(t.atom())).collect::<Vec<_>>();
        trace!("Received available targets {available:?} for clipboard {clipboard:?}");

        self.get_clipboard_mut(clipboard).types = targets.into();
    }

    pub fn request_updated_targets(
        &mut self,
        clipboard: ClipboardSelectionType,
        xwindow: xproto::Window,
        serial: u64,
    ) {
        let clipboard_state = self.get_clipboard_mut(clipboard);

        // Sometimes you get duplicate update events. Skip these.
        if serial == clipboard_state.x11_serial {
            return;
        }

        clipboard_state.transfer_serial = generate_transfer_id();
        clipboard_state.x11_serial = serial;

        // If we own the clipboard, there is nothing to do
        if clipboard_state.owned_data.is_some() {
            return;
        }

        self.clear_clipboard(clipboard);

        let atoms = self.xconn.atoms();
        let selection = clipboard.selection_atom(atoms);
        let property = clipboard.property_atom(atoms);
        if let Err(e) = self.xconn.convert_selection(xwindow, selection, atoms[TARGETS], property) {
            let selection = self.xconn.atom_str(selection);
            let property = self.xconn.atom_str(property);
            error!("Unable to request TARGETS for {selection} into {property}: {e}")
        }
    }

    pub fn request_clipboard_read(&mut self, clipboard: ClipboardSelectionType) -> DataTransferId {
        let clipboard_state = self.get_clipboard(clipboard);
        // TODO: skip the X11 protocol when we own the clipboard.
        if clipboard_state.owned_data.is_some() {
            trace!("Clipboard is owned by current application");
        }

        clipboard_state.transfer_serial
    }

    /// Update the internal cache of the clipboard contents
    pub fn set_clipboard(
        &mut self,
        xwindow: xproto::Window,
        send_data: Box<dyn DataTransferSend>,
        clipboard: ClipboardSelectionType,
    ) {
        self.get_clipboard_mut(clipboard).owned_data = Some(send_data);
        self.xconn.set_selection_owner(xwindow, clipboard.selection_atom(self.xconn.atoms()));
    }

    pub(crate) fn fetch_dnd_data_transfer(
        &mut self,
        type_: &dyn TransferType,
    ) -> Result<AsyncRequestSerial, TransferError> {
        let type_ = type_
            .cast_ref::<SelectionType>()
            .or_else(|| self.find_type_by_hint(type_.hint()?))
            .cloned()
            .ok_or(TransferError::Failed)?;

        let Some(state) = self.state.as_mut() else {
            return Err(TransferError::Failed);
        };

        // If it's non-empty, assume that we're still waiting on some other fetch operation.
        // The `SelectionNotify` handler will send a new `convert_selection` event if any
        // more are on the stack.
        let should_emit_convert_selection = state.pending_fetch_types.is_empty();

        let atom = type_.atom();
        let selection = self.xconn.atoms()[XdndSelection];

        if should_emit_convert_selection {
            // TODO: The result is stored into a property with the same name as the selection.
            // Consider using a different name to avoid conflict.
            if let Err(e) =
                self.xconn.convert_selection(state.target_window, selection, atom, selection)
            {
                return Err(TransferError::Os(os_error!(format!(
                    "could not convert selection: {e}"
                ))));
            }
        }

        let serial = AsyncRequestSerial::get();
        state.pending_fetch_types.push_back((serial, type_));

        Ok(serial)
    }

    pub(crate) fn fetch_clipboard_data_transfer(
        &mut self,
        clipboard: ClipboardSelectionType,
        type_: &dyn TransferType,
        xwindow: xproto::Window,
    ) -> Result<AsyncRequestSerial, TransferError> {
        let ty = type_
            .cast_ref::<SelectionType>()
            .cloned()
            .or_else(|| {
                self.get_clipboard(clipboard).find_type_by_hint(type_.hint()?, self.xconn.atoms())
            })
            .ok_or(TransferError::Failed)?;

        let serial = AsyncRequestSerial::get();
        let should_emit_convert_selection =
            self.get_clipboard_mut(clipboard).add_to_fetch(serial, ty);

        if should_emit_convert_selection {
            self.do_next_clipboard_fetch(clipboard, xwindow);
        }

        Ok(serial)
    }

    /// Remove all pending fetches, INCR, and owned data for a clipboard
    pub fn clear_clipboard(&mut self, clipboard: ClipboardSelectionType) {
        let clipboard_state = self.get_clipboard_mut(clipboard);
        if !clipboard_state.pending_fetch_types.is_empty() {
            trace!("Clearing all pending fetch types for {clipboard:?}");
            clipboard_state.pending_fetch_types.clear();
        }

        clipboard_state.types = Default::default();
        if clipboard_state.owned_data.take().is_some() {
            trace!("Cleared owned data for {clipboard:?}")
        }
        self.clip_incr_receive.retain(|receive| receive.clipboard != clipboard);
        self.clip_incr_send.retain(|receive| receive.clipboard != clipboard);
    }
}