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

use super::PictureParameters;
use crate::{
    auto::{
        render::{
            CreatePictureRequest, PictType, Pictformat, Pictforminfo, Pictscreen, Picture,
            Pictvisual, QueryPictFormatsReply, QueryPictFormatsRequest, QueryVersionReply,
            QueryVersionRequest,
        },
        xproto::{Drawable, Visualtype},
    },
    display::{Connection, Display, DisplayLike},
    send_request, sr_request,
};
use alloc::vec::Vec;

#[cfg(feature = "async")]
use crate::display::AsyncConnection;

/// A wrapper around the `Display` that contains XRender-specific data.
#[derive(Debug)]
pub struct RenderDisplay<Dpy> {
    inner: Dpy,
    formats: Vec<Pictforminfo>,
    screens: Vec<Pictscreen>,
    subpixels: Vec<u32>,
    major_version: u32,
    minor_version: u32,
}

impl<Dpy: DisplayLike> DisplayLike for RenderDisplay<Dpy> {
    type Connection = Dpy::Connection;

    #[inline]
    fn display(&self) -> &Display<Dpy::Connection> {
        self.inner.display()
    }

    #[inline]
    fn display_mut(&mut self) -> &mut Display<Dpy::Connection> {
        self.inner.display_mut()
    }
}

impl<Dpy> RenderDisplay<Dpy> {
    /// Get a reference to the inner object.
    #[inline]
    pub fn inner(&self) -> &Dpy {
        &self.inner
    }

    /// Get a mutable reference to the inner object.
    #[inline]
    pub fn inner_mut(&mut self) -> &mut Dpy {
        &mut self.inner
    }

    /// Destroy this objet and return the inner display.
    #[inline]
    pub fn into_inner(self) -> Dpy {
        self.inner
    }

    #[inline]
    fn fold_for_visformat<F: FnMut(&Pictvisual) -> bool>(&self, mut f: F) -> Option<Pictformat> {
        self.screens
            .iter()
            .flat_map(|s| s.depths.iter())
            .flat_map(|d| d.visuals.iter())
            .find_map(|v| if f(v) { Some(v.format) } else { None })
    }

    /// Get a `Pictformat` object that matches the given `Visualtype`.
    #[inline]
    pub fn find_visual_format(&self, visual: &Visualtype) -> Option<Pictformat> {
        self.fold_for_visformat(|v| v.visual == visual.visual_id)
    }

    /// Get a `Pictformat` based on a standard format.
    #[inline]
    pub fn find_standard_format(&self, standard: StandardFormat) -> Option<Pictformat> {
        const STANDARDS: &[fn(&Pictforminfo) -> bool] = &[
            |p| {
                p.ty == PictType::Direct
                    && p.depth == 32
                    && p.direct.red_shift == 16
                    && p.direct.red_mask == 0xFF
                    && p.direct.green_shift == 8
                    && p.direct.green_mask == 0xFF
                    && p.direct.blue_shift == 0
                    && p.direct.blue_mask == 0xFF
                    && p.direct.alpha_shift == 24
                    && p.direct.alpha_mask == 0xFF
            },
            |p| {
                p.ty == PictType::Direct
                    && p.depth == 24
                    && p.direct.red_shift == 16
                    && p.direct.red_mask == 0xFF
                    && p.direct.green_shift == 8
                    && p.direct.green_mask == 0xFF
                    && p.direct.blue_shift == 0
                    && p.direct.blue_mask == 0xFF
                    && p.direct.alpha_mask == 0
            },
            |p| {
                p.ty == PictType::Direct
                    && p.depth == 8
                    && p.direct.red_mask == 0x00
                    && p.direct.green_mask == 0x00
                    && p.direct.blue_mask == 0x00
                    && p.direct.alpha_shift == 0
                    && p.direct.alpha_mask == 0xFF
            },
            |p| {
                p.ty == PictType::Direct
                    && p.depth == 4
                    && p.direct.red_mask == 0x00
                    && p.direct.green_mask == 0x00
                    && p.direct.blue_mask == 0x00
                    && p.direct.alpha_shift == 0
                    && p.direct.alpha_mask == 0x0F
            },
            |p| {
                p.ty == PictType::Direct
                    && p.depth == 1
                    && p.direct.red_mask == 0x00
                    && p.direct.green_mask == 0x00
                    && p.direct.blue_mask == 0x00
                    && p.direct.alpha_shift == 0
                    && p.direct.alpha_mask == 0x01
            },
        ];
        let standard_identifier = STANDARDS[standard as usize];

        self.formats.iter().find_map(|p| {
            if standard_identifier(p) {
                Some(p.id)
            } else {
                None
            }
        })
    }

    #[inline]
    fn create_picture_request(
        pid: Picture,
        drawable: Drawable,
        format: Pictformat,
        props: PictureParameters,
    ) -> CreatePictureRequest {
        let mut cpr = CreatePictureRequest {
            pid,
            drawable,
            format,
            ..Default::default()
        };
        let cp = props.convert_to_flags(&mut cpr);
        cpr.value_mask = cp;
        cpr
    }
}

/// Standard formats.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(usize)]
pub enum StandardFormat {
    Argb32 = 0,
    Rgb24 = 1,
    A8 = 2,
    A4 = 3,
    A1 = 4,
}

impl<Dpy: DisplayLike> RenderDisplay<Dpy>
where
    Dpy::Connection: Connection,
{
    /// Initialize a RenderDisplay with the appropriate information.
    #[inline]
    pub fn new(
        mut dpy: Dpy,
        client_major_version: u32,
        client_minor_version: u32,
    ) -> crate::Result<Self> {
        // run QueryVersion and QueryPictFormats simultaneously
        let qvtok = send_request!(
            dpy.display_mut(),
            QueryVersionRequest {
                client_major_version,
                client_minor_version,
                ..Default::default()
            }
        )?;
        let qpftok = send_request!(dpy.display_mut(), QueryPictFormatsRequest::default())?;

        let QueryVersionReply {
            major_version,
            minor_version,
            ..
        } = dpy.display_mut().resolve_request(qvtok)?;
        let QueryPictFormatsReply {
            formats,
            screens,
            subpixels,
            ..
        } = dpy.display_mut().resolve_request(qpftok)?;

        Ok(Self {
            inner: dpy,
            major_version,
            minor_version,
            formats,
            screens,
            subpixels,
        })
    }

    /// Create a new Picture.
    #[inline]
    pub fn create_picture<Target: Into<Drawable>>(
        &mut self,
        target: Target,
        format: Pictformat,
        properties: PictureParameters,
    ) -> crate::Result<Picture> {
        let pic = Picture::const_from_xid(self.display_mut().generate_xid()?);
        let cpr = Self::create_picture_request(pic, target.into(), format, properties);
        sr_request!(self.display_mut(), cpr)?;
        Ok(pic)
    }
}

#[cfg(feature = "async")]
impl<Dpy: DisplayLike> RenderDisplay<Dpy>
where
    Dpy::Connection: AsyncConnection + Send,
{
    /// Initialize a RenderDisplay with the appropriate information, async redox.
    #[inline]
    pub async fn new_async(
        mut dpy: Dpy,
        client_major_version: u32,
        client_minor_version: u32,
    ) -> crate::Result<Self> {
        let qvtok = send_request!(
            dpy.display_mut(),
            QueryVersionRequest {
                client_major_version,
                client_minor_version,
                ..Default::default()
            },
            async
        )
        .await?;
        let qpftok =
            send_request!(dpy.display_mut(), QueryPictFormatsRequest::default(), async).await?;

        let QueryVersionReply {
            major_version,
            minor_version,
            ..
        } = dpy.display_mut().resolve_request_async(qvtok).await?;
        let QueryPictFormatsReply {
            formats,
            screens,
            subpixels,
            ..
        } = dpy.display_mut().resolve_request_async(qpftok).await?;

        Ok(Self {
            inner: dpy,
            major_version,
            minor_version,
            formats,
            screens,
            subpixels,
        })
    }

    /// Create a new Picture, async redox.
    #[inline]
    pub async fn create_picture_async<Target: Into<Drawable>>(
        &mut self,
        target: Target,
        format: Pictformat,
        properties: PictureParameters,
    ) -> crate::Result<Picture> {
        let pic = Picture::const_from_xid(self.display_mut().generate_xid()?);
        let cpr = Self::create_picture_request(pic, target.into(), format, properties);
        sr_request!(self.display_mut(), cpr, async).await?;
        Ok(pic)
    }
}