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
// MIT/Apache2 License

use x11rb_protocol::{
    protocol::{
        bigreq::EnableRequest,
        xc_misc::{GetXIDRangeReply, GetXIDRangeRequest},
        xproto::{GetInputFocusReply, GetInputFocusRequest, QueryExtensionRequest},
    },
    x11_utils::{ExtensionInformation, ReplyRequest},
    SequenceNumber,
};

use super::{
    raw_request::{from_reply_request, BufferedRequest},
    Display, RawReply,
};
use crate::Result;

cfg_async! {
    use super::{AsyncStatus, CanBeAsyncDisplay};
    use core::task::Context;
}

/// Some internal data that needs to be fetched through
/// another request.
pub struct Prefetch<T: PrefetchTarget> {
    state: PrefetchState<T>,
}

enum PrefetchState<T: PrefetchTarget> {
    /// In the process of formatting.
    Formatting(Option<BufferedRequest>),
    /// We're in the process of sending this data.
    #[allow(dead_code)]
    Sending(Option<BufferedRequest>, u64),
    /// We've sent this data and are waiting for a reply.
    Waiting(SequenceNumber),
    /// The data is available for us; the request is complete.
    Complete(T::Target),
}

/// A request that needs to be prefetched.
pub trait PrefetchTarget: ReplyRequest {
    /// The resulting data from the prefetch.
    type Target;

    /// Map the reply to the prefetch target.
    fn map_reply(reply: Self::Reply) -> Self::Target;

    /// The target when an X11 error occurs.
    fn on_x11_error() -> Self::Target;
}

impl<T: PrefetchTarget + Default> Default for Prefetch<T> {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl<T: PrefetchTarget> From<PrefetchState<T>> for Prefetch<T> {
    fn from(state: PrefetchState<T>) -> Self {
        Self { state }
    }
}

impl<T: PrefetchTarget> Prefetch<T> {
    pub fn new(req: T) -> Self {
        #[allow(clippy::redundant_closure_for_method_calls)]
        let req = from_reply_request(req, |req| req.into());
        PrefetchState::Formatting(Some(req)).into()
    }

    /// Get the target if and only if this prefetch has
    /// already resolved.
    pub(crate) fn get_if_resolved(&self) -> Option<&T::Target> {
        match self.state {
            PrefetchState::Complete(ref c) => Some(c),
            _ => None,
        }
    }

    /// Format the request.
    ///
    /// The user may use this in order to format the request.
    pub(crate) fn request_to_format(&mut self) -> &mut BufferedRequest {
        match self.state {
            PrefetchState::Formatting(Some(ref mut req)) => req,
            _ => panic!("Prefetch is not formatting"),
        }
    }

    /// Send with an overridden sequence number.
    pub(crate) fn sent_override(&mut self, seq: u64) {
        match self.state {
            PrefetchState::Sending(..) | PrefetchState::Formatting(..) => {
                *self = PrefetchState::Waiting(seq).into();
            }
            _ => panic!("Prefetch is not sending or formatting"),
        }
    }

    /// The sequence number of the reply we're waiting for.
    pub(crate) fn sequence(&self) -> u64 {
        match self.state {
            PrefetchState::Waiting(ref seq) => *seq,
            _ => panic!("Prefetch is not waiting"),
        }
    }

    /// Read in the reply.
    pub(crate) fn read_reply(&mut self, reply: RawReply) -> Result<()> {
        let reply: T::Reply = reply.into_reply()?;
        let mapped = T::map_reply(reply);
        *self = PrefetchState::Complete(mapped).into();
        Ok(())
    }

    /// Error out.
    pub(crate) fn on_x11_error(&mut self) {
        *self = PrefetchState::Complete(T::on_x11_error()).into();
    }

    /// Evaluate the prefetch while blocking.
    pub fn evaluate(&mut self, display: &mut impl Display) -> Result<&T::Target> {
        // call all functions in order
        let request = self.request_to_format();
        let seq = request.take(|request| display.send_request_raw(request))?;
        self.sent_override(seq);
        match display.wait_for_reply_raw(self.sequence()) {
            Ok(reply) => {
                self.read_reply(reply)?;
            }
            Err(e) if e.is_protocol_error() => {
                self.on_x11_error();
            }
            Err(e) => return Err(e),
        };
        Ok(self.get_if_resolved().unwrap())
    }
}

cfg_async! {
    impl<T: PrefetchTarget> Prefetch<T> {
        fn not_yet_formatted(&self) -> bool {
            matches!(self.state, PrefetchState::Formatting(_))
        }

        fn not_yet_sent(&self) -> bool {
            matches!(self.state, PrefetchState::Formatting(_) | PrefetchState::Sending(..))
        }

        fn not_yet_read(&self) -> bool {
            !matches!(self.state, PrefetchState::Complete(_))
        }

        /// Indicate that the request has been sent.
        pub(crate) fn sent(&mut self) {
            match self.state {
                PrefetchState::Sending(_, seq) => *self = PrefetchState::Waiting(seq).into(),
                _ => panic!("Prefetch is not sending"),
            }
        }

        /// Indicate that we've formatted the request.
        pub(crate) fn formatted(&mut self, seq: u64) {
            match self.state {
                PrefetchState::Formatting(ref mut request) => {
                    *self = PrefetchState::Sending(request.take(), seq).into();
                }
                _ => panic!("Prefetch is not formatting"),
            }
        }

        /// Get the request to send.
        pub(crate) fn request_to_send(&mut self) -> &mut BufferedRequest {
            match self.state {
                PrefetchState::Sending(Some(ref mut req), ..)
                | PrefetchState::Formatting(Some(ref mut req)) => req,
                _ => panic!("Prefetch is not sending"),
            }
        }

        /// Evaluate the prefetch, but avoid blocking.
        pub fn try_evaluate(
            &mut self,
            display: &mut impl CanBeAsyncDisplay,
            ctx: &mut Context<'_>,
        ) -> Result<AsyncStatus<&T::Target>> {
            let span = tracing::trace_span!(
                "try_evaluate",
                formatted = !self.not_yet_formatted(),
                sent = !self.not_yet_sent(),
                read = !self.not_yet_read()
            );
            let _enter = span.enter();

            // call all functions in order
            if self.not_yet_formatted() {
                tracing::trace!("formatting request");
                let request = self.request_to_format();
                let seq = mtry! {
                    request.borrow(|request| {
                        display.format_request(request, ctx)
                    })
                };
                self.formatted(seq);
            }

            if self.not_yet_sent() {
                tracing::trace!("sending request");
                let request = self.request_to_send();
                mtry! {
                    request.borrow(|request| {
                        display.try_send_request_raw(request, ctx)
                    })
                };
                self.sent();
            }

            if self.not_yet_read() {
                tracing::trace!("receiving reply");
                match display.try_wait_for_reply_raw(self.sequence(), ctx) {
                    Ok(AsyncStatus::Ready(t)) => self.read_reply(t)?,
                    Ok(status) => return Ok(status.map(|_| unreachable!())),
                    Err(e) if e.is_protocol_error() => {
                        self.on_x11_error();
                    }
                    Err(e) => return Err(e),
                }
            }

            Ok(AsyncStatus::Ready(self.get_if_resolved().unwrap()))
        }
    }
}

// prefetch targets: query for bigreq and query for extension

impl PrefetchTarget for EnableRequest {
    type Target = Option<usize>;

    fn map_reply(reply: Self::Reply) -> Self::Target {
        let maxlen = reply.maximum_request_length as usize;
        Some(maxlen)
    }

    fn on_x11_error() -> Self::Target {
        None
    }
}

impl<'a> PrefetchTarget for QueryExtensionRequest<'a> {
    type Target = Option<ExtensionInformation>;

    fn map_reply(reply: Self::Reply) -> Self::Target {
        if !reply.present {
            return None;
        }

        let info = ExtensionInformation {
            major_opcode: reply.major_opcode,
            first_error: reply.first_error,
            first_event: reply.first_event,
        };

        Some(info)
    }

    fn on_x11_error() -> Self::Target {
        None
    }
}

impl PrefetchTarget for GetInputFocusRequest {
    type Target = GetInputFocusReply;

    fn map_reply(reply: Self::Reply) -> Self::Target {
        reply
    }

    fn on_x11_error() -> Self::Target {
        tracing::error!("synchronization should never error out");
        GetInputFocusReply::default()
    }
}

impl PrefetchTarget for GetXIDRangeRequest {
    type Target = GetXIDRangeReply;

    fn map_reply(reply: Self::Reply) -> Self::Target {
        reply
    }

    fn on_x11_error() -> Self::Target {
        tracing::error!("XID refresh should never error out");
        GetXIDRangeReply::default()
    }
}