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

use std::num::NonZeroU32;

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

use crate::{Caps, DeviceMonitor};

#[derive(Debug, PartialEq, Eq)]
pub struct DeviceMonitorFilterId(NonZeroU32);

impl IntoGlib for DeviceMonitorFilterId {
    type GlibType = libc::c_uint;

    #[inline]
    fn into_glib(self) -> libc::c_uint {
        self.0.get()
    }
}

impl FromGlib<libc::c_uint> for DeviceMonitorFilterId {
    #[inline]
    unsafe fn from_glib(val: libc::c_uint) -> DeviceMonitorFilterId {
        skip_assert_initialized!();
        debug_assert_ne!(val, 0);
        DeviceMonitorFilterId(NonZeroU32::new_unchecked(val))
    }
}
mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::DeviceMonitor>> Sealed for T {}
}

pub trait DeviceMonitorExtManual: sealed::Sealed + IsA<DeviceMonitor> + 'static {
    /// Adds a filter for which [`Device`][crate::Device] will be monitored, any device that matches
    /// all these classes and the [`Caps`][crate::Caps] will be returned.
    ///
    /// If this function is called multiple times to add more filters, each will be
    /// matched independently. That is, adding more filters will not further restrict
    /// what devices are matched.
    ///
    /// The [`Caps`][crate::Caps] supported by the device as returned by [`DeviceExt::caps()`][crate::prelude::DeviceExt::caps()] are
    /// not intersected with caps filters added using this function.
    ///
    /// Filters must be added before the [`DeviceMonitor`][crate::DeviceMonitor] is started.
    /// ## `classes`
    /// device classes to use as filter or [`None`] for any class
    /// ## `caps`
    /// the [`Caps`][crate::Caps] to filter or [`None`] for ANY
    ///
    /// # Returns
    ///
    /// The id of the new filter or 0 if no provider matched the filter's
    ///  classes.
    #[doc(alias = "gst_device_monitor_add_filter")]
    fn add_filter(
        &self,
        classes: Option<&str>,
        caps: Option<&Caps>,
    ) -> Option<DeviceMonitorFilterId> {
        let id = unsafe {
            ffi::gst_device_monitor_add_filter(
                self.as_ref().to_glib_none().0,
                classes.to_glib_none().0,
                caps.to_glib_none().0,
            )
        };

        if id == 0 {
            None
        } else {
            Some(unsafe { from_glib(id) })
        }
    }

    /// Removes a filter from the [`DeviceMonitor`][crate::DeviceMonitor] using the id that was returned
    /// by [`add_filter()`][Self::add_filter()].
    /// ## `filter_id`
    /// the id of the filter
    ///
    /// # Returns
    ///
    /// [`true`] of the filter id was valid, [`false`] otherwise
    #[doc(alias = "gst_device_monitor_remove_filter")]
    fn remove_filter(
        &self,
        filter_id: DeviceMonitorFilterId,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_device_monitor_remove_filter(
                    self.as_ref().to_glib_none().0,
                    filter_id.into_glib()
                ),
                "Failed to remove the filter"
            )
        }
    }

    /// Gets a list of devices from all of the relevant monitors. This may actually
    /// probe the hardware if the monitor is not currently started.
    ///
    /// # Returns
    ///
    /// a `GList` of
    ///  [`Device`][crate::Device]
    #[doc(alias = "gst_device_monitor_get_devices")]
    #[doc(alias = "get_devices")]
    fn devices(&self) -> glib::List<crate::Device> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gst_device_monitor_get_devices(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl<O: IsA<DeviceMonitor>> DeviceMonitorExtManual for O {}