Skip to main content

spice_client_glib/
display_channel.rs

1use glib::object::ObjectType as ObjectType_;
2use glib::signal::connect_raw;
3use glib::signal::SignalHandlerId;
4use glib::translate::*;
5use std::boxed::Box as Box_;
6use std::mem::transmute;
7
8use crate::{DisplayChannel, DisplayMonitorConfig, DisplayPrimary};
9
10impl DisplayChannel {
11    pub fn connect_display_primary_create<F: Fn(&DisplayChannel) + 'static>(
12        &self,
13        f: F,
14    ) -> SignalHandlerId {
15        unsafe extern "C" fn display_primary_destroy_trampoline<
16            F: Fn(&DisplayChannel) + 'static,
17        >(
18            this: *mut ffi::SpiceDisplayChannel,
19            _format: libc::c_int,
20            _width: libc::c_int,
21            _height: libc::c_int,
22            _stride: libc::c_int,
23            _shmid: libc::c_int,
24            _imgdata: glib::ffi::gpointer,
25            f: glib::ffi::gpointer,
26        ) {
27            let f: &F = &*(f as *const F);
28            f(&from_glib_borrow(this))
29        }
30        unsafe {
31            let f: Box_<F> = Box_::new(f);
32            connect_raw(
33                self.as_ptr() as *mut _,
34                b"display-primary-create\0".as_ptr() as *const _,
35                Some(transmute::<_, unsafe extern "C" fn()>(
36                    display_primary_destroy_trampoline::<F> as *const (),
37                )),
38                Box_::into_raw(f),
39            )
40        }
41    }
42
43    pub fn monitors(&self) -> Option<Vec<DisplayMonitorConfig>> {
44        unsafe {
45            let mut value = glib::Value::from_type(from_glib(glib::ffi::g_array_get_type()));
46            glib::gobject_ffi::g_object_get_property(
47                self.as_ptr() as *mut glib::gobject_ffi::GObject,
48                b"monitors\0".as_ptr() as *const _,
49                value.to_glib_none_mut().0,
50            );
51            let array = glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0)
52                as *const glib::ffi::GArray;
53            if array.is_null() {
54                None
55            } else {
56                let array = &*array;
57                let data = array.data as *const ffi::SpiceDisplayMonitorConfig;
58                let len = array.len as usize;
59
60                let mut vec = Vec::with_capacity(len);
61                for i in 0..len {
62                    vec.push(from_glib_none(data.add(i)))
63                }
64                Some(vec)
65            }
66        }
67    }
68
69    pub fn primary(&self, id: u32) -> Option<DisplayPrimary> {
70        let mut primary = DisplayPrimary::new();
71
72        if self.get_primary(id, &mut primary).is_ok() {
73            Some(primary)
74        } else {
75            None
76        }
77    }
78}