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
use std::{
    io,
    os::raw::*,
    path::{Path, PathBuf},
    str::Utf8Error,
    sync::Arc,
};

use percent_encoding::percent_decode;

use super::{ffi, util, XConnection, XError};

#[derive(Debug)]
pub(crate) struct DndAtoms {
    pub enter: ffi::Atom,
    pub leave: ffi::Atom,
    pub drop: ffi::Atom,
    pub position: ffi::Atom,
    pub status: ffi::Atom,
    pub action_private: ffi::Atom,
    pub selection: ffi::Atom,
    pub finished: ffi::Atom,
    pub type_list: ffi::Atom,
    pub uri_list: ffi::Atom,
    pub none: ffi::Atom,
}

impl DndAtoms {
    pub fn new(xconn: &Arc<XConnection>) -> Result<Self, XError> {
        let names = [
            b"XdndEnter\0".as_ptr() as *mut c_char,
            b"XdndLeave\0".as_ptr() as *mut c_char,
            b"XdndDrop\0".as_ptr() as *mut c_char,
            b"XdndPosition\0".as_ptr() as *mut c_char,
            b"XdndStatus\0".as_ptr() as *mut c_char,
            b"XdndActionPrivate\0".as_ptr() as *mut c_char,
            b"XdndSelection\0".as_ptr() as *mut c_char,
            b"XdndFinished\0".as_ptr() as *mut c_char,
            b"XdndTypeList\0".as_ptr() as *mut c_char,
            b"text/uri-list\0".as_ptr() as *mut c_char,
            b"None\0".as_ptr() as *mut c_char,
        ];
        let atoms = unsafe { xconn.get_atoms(&names) }?;
        Ok(DndAtoms {
            enter: atoms[0],
            leave: atoms[1],
            drop: atoms[2],
            position: atoms[3],
            status: atoms[4],
            action_private: atoms[5],
            selection: atoms[6],
            finished: atoms[7],
            type_list: atoms[8],
            uri_list: atoms[9],
            none: atoms[10],
        })
    }
}

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

#[derive(Debug)]
pub enum DndDataParseError {
    EmptyData,
    InvalidUtf8(Utf8Error),
    HostnameSpecified(String),
    UnexpectedProtocol(String),
    UnresolvablePath(io::Error),
}

impl From<Utf8Error> for DndDataParseError {
    fn from(e: Utf8Error) -> Self {
        DndDataParseError::InvalidUtf8(e)
    }
}

impl From<io::Error> for DndDataParseError {
    fn from(e: io::Error) -> Self {
        DndDataParseError::UnresolvablePath(e)
    }
}

pub(crate) struct Dnd {
    xconn: Arc<XConnection>,
    pub atoms: DndAtoms,
    // Populated by XdndEnter event handler
    pub version: Option<c_long>,
    pub type_list: Option<Vec<c_ulong>>,
    // Populated by XdndPosition event handler
    pub source_window: Option<c_ulong>,
    // Populated by SelectionNotify event handler (triggered by XdndPosition event handler)
    pub result: Option<Result<Vec<PathBuf>, DndDataParseError>>,
}

impl Dnd {
    pub fn new(xconn: Arc<XConnection>) -> Result<Self, XError> {
        let atoms = DndAtoms::new(&xconn)?;
        Ok(Dnd {
            xconn,
            atoms,
            version: None,
            type_list: None,
            source_window: None,
            result: None,
        })
    }

    pub fn reset(&mut self) {
        self.version = None;
        self.type_list = None;
        self.source_window = None;
        self.result = None;
    }

    pub unsafe fn send_status(
        &self,
        this_window: c_ulong,
        target_window: c_ulong,
        state: DndState,
    ) -> Result<(), XError> {
        let (accepted, action) = match state {
            DndState::Accepted => (1, self.atoms.action_private as c_long),
            DndState::Rejected => (0, self.atoms.none as c_long),
        };
        self.xconn
            .send_client_msg(
                target_window,
                target_window,
                self.atoms.status,
                None,
                [this_window as c_long, accepted, 0, 0, action],
            )
            .flush()
    }

    pub unsafe fn send_finished(
        &self,
        this_window: c_ulong,
        target_window: c_ulong,
        state: DndState,
    ) -> Result<(), XError> {
        let (accepted, action) = match state {
            DndState::Accepted => (1, self.atoms.action_private as c_long),
            DndState::Rejected => (0, self.atoms.none as c_long),
        };
        self.xconn
            .send_client_msg(
                target_window,
                target_window,
                self.atoms.finished,
                None,
                [this_window as c_long, accepted, action, 0, 0],
            )
            .flush()
    }

    pub unsafe fn get_type_list(
        &self,
        source_window: c_ulong,
    ) -> Result<Vec<ffi::Atom>, util::GetPropertyError> {
        self.xconn
            .get_property(source_window, self.atoms.type_list, ffi::XA_ATOM)
    }

    pub unsafe fn convert_selection(&self, window: c_ulong, time: c_ulong) {
        (self.xconn.xlib.XConvertSelection)(
            self.xconn.display,
            self.atoms.selection,
            self.atoms.uri_list,
            self.atoms.selection,
            window,
            time,
        );
    }

    pub unsafe fn read_data(
        &self,
        window: c_ulong,
    ) -> Result<Vec<c_uchar>, util::GetPropertyError> {
        self.xconn
            .get_property(window, self.atoms.selection, self.atoms.uri_list)
    }

    pub fn parse_data(&self, data: &mut [c_uchar]) -> Result<Vec<PathBuf>, DndDataParseError> {
        if !data.is_empty() {
            let mut path_list = Vec::new();
            let decoded = percent_decode(data).decode_utf8()?.into_owned();
            for uri in decoded.split("\r\n").filter(|u| !u.is_empty()) {
                // The format is specified as protocol://host/path
                // However, it's typically simply protocol:///path
                let path_str = if uri.starts_with("file://") {
                    let path_str = uri.replace("file://", "");
                    if !path_str.starts_with('/') {
                        // A hostname is specified
                        // Supporting this case is beyond the scope of my mental health
                        return Err(DndDataParseError::HostnameSpecified(path_str));
                    }
                    path_str
                } else {
                    // Only the file protocol is supported
                    return Err(DndDataParseError::UnexpectedProtocol(uri.to_owned()));
                };

                let path = Path::new(&path_str).canonicalize()?;
                path_list.push(path);
            }
            Ok(path_list)
        } else {
            Err(DndDataParseError::EmptyData)
        }
    }
}