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

#![cfg(feature = "glx")]

use crate::{
    auto::{
        glx::{
            Context, ContextTag, CreateContextAttribsArbRequest, Drawable, Fbconfig,
            GetDrawableAttributesRequest, GetFbConfigsReply, GetFbConfigsRequest,
            GetVisualConfigsReply, GetVisualConfigsRequest, QueryVersionRequest,
            SwapBuffersRequest,
        },
        xproto,
    },
    display::{Connection, Display, RequestCookie},
    send_request, sr_request,
};
use alloc::vec::Vec;
use core::convert::TryInto;

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

impl From<xproto::Drawable> for Drawable {
    #[inline]
    fn from(d: xproto::Drawable) -> Drawable {
        Drawable::const_from_xid(d.xid)
    }
}

#[derive(Debug, Clone, Default)]
pub struct Configs {
    pub num_configs: u32,
    pub num_properties_per_config: u32,
    pub properties: Vec<u32>,
}

impl From<GetVisualConfigsReply> for Configs {
    #[inline]
    fn from(gvcr: GetVisualConfigsReply) -> Self {
        Self {
            num_configs: gvcr.num_visuals,
            num_properties_per_config: gvcr.num_properties,
            properties: gvcr.property_list,
        }
    }
}

impl From<GetFbConfigsReply> for Configs {
    #[inline]
    fn from(fbcr: GetFbConfigsReply) -> Self {
        Self {
            num_configs: fbcr.num_fb_configs,
            num_properties_per_config: fbcr.num_properties,
            properties: fbcr.property_list,
        }
    }
}

impl<Conn> Display<Conn> {
    #[inline]
    fn create_context_attribs_arb_request(
        context: Context,
        fbconfig: Fbconfig,
        screen: usize,
        share_list: Context,
        is_direct: bool,
        attribs: Vec<u32>,
    ) -> CreateContextAttribsArbRequest {
        let attribs_len: u32 = attribs.len().try_into().expect("usize dont fit 2");
        CreateContextAttribsArbRequest {
            context,
            fbconfig,
            screen: screen.try_into().expect("usize dont fit"),
            share_list,
            is_direct,
            num_attribs: attribs_len / 2u32,
            attribs,
            ..Default::default()
        }
    }
}

impl<Conn: Connection> Display<Conn> {
    /// Query GLX version.
    #[inline]
    pub fn query_glx_version(
        &mut self,
        required_major: u32,
        required_minor: u32,
    ) -> crate::Result<RequestCookie<QueryVersionRequest>> {
        send_request!(
            self,
            QueryVersionRequest {
                major_version: required_major,
                minor_version: required_minor,
                ..Default::default()
            }
        )
    }

    /// Immediately query GLX version.
    #[inline]
    pub fn query_glx_version_immediate(
        &mut self,
        required_major: u32,
        required_minor: u32,
    ) -> crate::Result<(u32, u32)> {
        let tok = self.query_glx_version(required_major, required_minor)?;
        let repl = self.resolve_request(tok)?;
        Ok((repl.major_version, repl.minor_version))
    }

    /// Get the visual configurations associated with the given screen.
    #[inline]
    pub fn get_visual_configs(
        &mut self,
        screen: usize,
    ) -> crate::Result<RequestCookie<GetVisualConfigsRequest>> {
        send_request!(
            self,
            GetVisualConfigsRequest {
                screen: screen as _,
                ..Default::default()
            }
        )
    }

    /// Immediately get the visual configurations associated with the given screen.
    #[inline]
    pub fn get_visual_configs_immediate(&mut self, screen: usize) -> crate::Result<Configs> {
        let tok = self.get_visual_configs(screen)?;
        Ok(self.resolve_request(tok)?.into())
    }

    /// Get the framebuffer configurations associated with the given screen.
    #[inline]
    pub fn get_fb_configs(
        &mut self,
        screen: usize,
    ) -> crate::Result<RequestCookie<GetFbConfigsRequest>> {
        send_request!(
            self,
            GetFbConfigsRequest {
                screen: screen as _,
                ..Default::default()
            }
        )
    }

    /// Immediately get the framebuffer configurations associated with the given screen.
    #[inline]
    pub fn get_fb_configs_immediate(&mut self, screen: usize) -> crate::Result<Configs> {
        let tok = self.get_fb_configs(screen)?;
        Ok(self.resolve_request(tok)?.into())
    }

    /// Get the properties of a GLX drawable.
    #[inline]
    pub fn get_drawable_properties(
        &mut self,
        drawable: Drawable,
    ) -> crate::Result<RequestCookie<GetDrawableAttributesRequest>> {
        send_request!(
            self,
            GetDrawableAttributesRequest {
                drawable,
                ..Default::default()
            }
        )
    }

    /// Immediately get the properties of a GLX drawable.
    #[inline]
    pub fn get_drawable_properties_immediate(
        &mut self,
        drawable: Drawable,
    ) -> crate::Result<Vec<u32>> {
        let tok = self.get_drawable_properties(drawable)?;
        Ok(self.resolve_request(tok)?.attribs)
    }

    #[inline]
    pub fn create_context_attribs_arb(
        &mut self,
        fbconfig: Fbconfig,
        screen: usize,
        share_list: Context,
        is_direct: bool,
        attribs: Vec<u32>,
    ) -> crate::Result<Context> {
        let xid = Context::const_from_xid(self.generate_xid()?);
        sr_request!(
            self,
            Self::create_context_attribs_arb_request(
                xid, fbconfig, screen, share_list, is_direct, attribs,
            )
        )?;
        Ok(xid)
    }

    /// Swap buffers.
    #[inline]
    pub fn swap_buffers<Target: Into<Drawable>>(
        &mut self,
        context_tag: ContextTag,
        drawable: Target,
    ) -> crate::Result {
        sr_request!(
            self,
            SwapBuffersRequest {
                context_tag,
                drawable: drawable.into(),
                ..Default::default()
            }
        )
    }
}

#[cfg(feature = "async")]
impl<Conn: AsyncConnection + Send> Display<Conn> {
    /// Query GLX version, async redox.
    #[inline]
    pub async fn query_glx_version_async(
        &mut self,
        required_major: u32,
        required_minor: u32,
    ) -> crate::Result<RequestCookie<QueryVersionRequest>> {
        send_request!(
            self,
            QueryVersionRequest {
                major_version: required_major,
                minor_version: required_minor,
                ..Default::default()
            },
            async
        )
        .await
    }

    /// Get the visual configurations associated with the given screen, async redox.
    #[inline]
    pub async fn get_visual_configs_async(
        &mut self,
        screen: usize,
    ) -> crate::Result<RequestCookie<GetVisualConfigsRequest>> {
        send_request!(
            self,
            GetVisualConfigsRequest {
                screen: screen as _,
                ..Default::default()
            },
            async
        )
        .await
    }

    /// Immediately get the visual configurations associated with the given screen, async redox.
    #[inline]
    pub async fn get_visual_configs_immediate_async(
        &mut self,
        screen: usize,
    ) -> crate::Result<Configs> {
        let tok = self.get_visual_configs_async(screen).await?;
        Ok(self.resolve_request_async(tok).await?.into())
    }

    /// Get the framebuffer configurations associated with the given screen, async redox.
    #[inline]
    pub async fn get_fb_configs_async(
        &mut self,
        screen: usize,
    ) -> crate::Result<RequestCookie<GetFbConfigsRequest>> {
        send_request!(
            self,
            GetFbConfigsRequest {
                screen: screen as _,
                ..Default::default()
            },
            async
        )
        .await
    }

    /// Immediately get the framebuffer configurations associated with the given screen, async redox.
    #[inline]
    pub async fn get_fb_configs_immediate_async(
        &mut self,
        screen: usize,
    ) -> crate::Result<Configs> {
        let tok = self.get_fb_configs_async(screen).await?;
        Ok(self.resolve_request_async(tok).await?.into())
    }

    /// Get the properties of a GLX drawable, async redox.
    #[inline]
    pub async fn get_drawable_properties_async(
        &mut self,
        drawable: Drawable,
    ) -> crate::Result<RequestCookie<GetDrawableAttributesRequest>> {
        send_request!(
            self,
            GetDrawableAttributesRequest {
                drawable,
                ..Default::default()
            },
            async
        )
        .await
    }

    /// Immediately query GLX version, async redox.
    #[inline]
    pub async fn query_glx_version_immediate_async(
        &mut self,
        required_major: u32,
        required_minor: u32,
    ) -> crate::Result<(u32, u32)> {
        let tok = self
            .query_glx_version_async(required_major, required_minor)
            .await?;
        let repl = self.resolve_request_async(tok).await?;
        Ok((repl.major_version, repl.minor_version))
    }

    /// Immediately get the properties of a GLX drawable, async redox.
    #[inline]
    pub async fn get_drawable_properties_immediate_async(
        &mut self,
        drawable: Drawable,
    ) -> crate::Result<Vec<u32>> {
        let tok = self.get_drawable_properties_async(drawable).await?;
        Ok(self.resolve_request_async(tok).await?.attribs)
    }

    #[inline]
    pub async fn create_context_attribs_arb_async(
        &mut self,
        fbconfig: Fbconfig,
        screen: usize,
        share_list: Context,
        is_direct: bool,
        attribs: Vec<u32>,
    ) -> crate::Result<Context> {
        let xid = Context::const_from_xid(self.generate_xid()?);
        sr_request!(
            self,
            Self::create_context_attribs_arb_request(
                xid, fbconfig, screen, share_list, is_direct, attribs,
            ),
            async
        )
        .await?;
        Ok(xid)
    }

    /// Swap buffers, async redox.
    #[inline]
    pub async fn swap_buffers_async<Target: Into<Drawable>>(
        &mut self,
        context_tag: ContextTag,
        drawable: Target,
    ) -> crate::Result {
        sr_request!(
            self,
            SwapBuffersRequest {
                context_tag,
                drawable: drawable.into(),
                ..Default::default()
            },
            async
        )
        .await
    }
}