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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, Navigation};

pub trait NavigationImpl: ObjectImpl {
    fn send_event(&self, structure: gst::Structure);

    #[cfg(feature = "v1_22")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
    fn send_event_simple(&self, event: gst::Event) {
        if let Some(structure) = event.structure() {
            self.send_event(structure.to_owned());
        }
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::NavigationImplExt> Sealed for T {}
}

pub trait NavigationImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_send_event(&self, structure: gst::Structure) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Navigation>()
                as *const ffi::GstNavigationInterface;

            let func = match (*parent_iface).send_event {
                Some(func) => func,
                None => return,
            };

            func(
                self.obj().unsafe_cast_ref::<Navigation>().to_glib_none().0,
                structure.into_glib_ptr(),
            );
        }
    }

    #[cfg(feature = "v1_22")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
    fn parent_send_event_simple(&self, event: gst::Event) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Navigation>()
                as *const ffi::GstNavigationInterface;

            let func = match (*parent_iface).send_event_simple {
                Some(func) => func,
                None => return,
            };

            func(
                self.obj().unsafe_cast_ref::<Navigation>().to_glib_none().0,
                event.into_glib_ptr(),
            );
        }
    }
}

impl<T: NavigationImpl> NavigationImplExt for T {}

unsafe impl<T: NavigationImpl> IsImplementable<T> for Navigation {
    #[cfg(not(any(feature = "v1_22", docsrs)))]
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.send_event = Some(navigation_send_event::<T>);
    }

    #[cfg(feature = "v1_22")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.send_event = Some(navigation_send_event::<T>);
        iface.send_event_simple = Some(navigation_send_event_simple::<T>);
    }
}

unsafe extern "C" fn navigation_send_event<T: NavigationImpl>(
    nav: *mut ffi::GstNavigation,
    structure: *mut gst::ffi::GstStructure,
) {
    let instance = &*(nav as *mut T::Instance);
    let imp = instance.imp();

    imp.send_event(from_glib_full(structure));
}

#[cfg(feature = "v1_22")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
unsafe extern "C" fn navigation_send_event_simple<T: NavigationImpl>(
    nav: *mut ffi::GstNavigation,
    event: *mut gst::ffi::GstEvent,
) {
    let instance = &*(nav as *mut T::Instance);
    let imp = instance.imp();

    imp.send_event_simple(from_glib_full(event));
}