Skip to main content

spice_client_glib/
display_primary.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::SurfaceFormat;
6use std::convert::TryInto;
7
8#[derive(Debug, Clone)]
9pub struct DisplayPrimary(ffi::SpiceDisplayPrimary);
10
11impl DisplayPrimary {
12    pub fn new() -> Self {
13        Self(unsafe { std::mem::zeroed() })
14    }
15
16    pub fn format(&self) -> Result<SurfaceFormat, i32> {
17        self.0.format.try_into()
18    }
19
20    pub fn width(&self) -> usize {
21        self.0.width as _
22    }
23
24    pub fn height(&self) -> usize {
25        self.0.height as _
26    }
27
28    pub fn stride(&self) -> usize {
29        self.0.stride as _
30    }
31
32    pub fn shmid(&self) -> i32 {
33        self.0.shmid
34    }
35
36    pub fn data(&self) -> &[u8] {
37        unsafe { std::slice::from_raw_parts(self.0.data, self.stride() * self.height()) }
38    }
39
40    pub fn marked(&self) -> bool {
41        unsafe { from_glib(self.0.marked) }
42    }
43
44    pub fn as_ptr(&self) -> *const ffi::SpiceDisplayPrimary {
45        &self.0
46    }
47}
48
49impl From<ffi::SpiceDisplayPrimary> for DisplayPrimary {
50    fn from(p: ffi::SpiceDisplayPrimary) -> Self {
51        skip_assert_initialized!();
52        Self(p)
53    }
54}
55
56#[doc(hidden)]
57impl<'a> ToGlibPtr<'a, *const ffi::SpiceDisplayPrimary> for DisplayPrimary {
58    type Storage = &'a Self;
59
60    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::SpiceDisplayPrimary, Self> {
61        Stash(&self.0, self)
62    }
63}
64
65#[doc(hidden)]
66impl<'a> ToGlibPtrMut<'a, *mut ffi::SpiceDisplayPrimary> for DisplayPrimary {
67    type Storage = &'a mut Self;
68
69    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::SpiceDisplayPrimary, Self> {
70        StashMut(&mut self.0, self)
71    }
72}
73
74impl Default for DisplayPrimary {
75    fn default() -> Self {
76        Self::new()
77    }
78}