alsahwdep 0.8.0

API binding for alsahwdep library
Documentation
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{ffi, DeviceInfo};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// An interface to express common features of ALSA HwDep device.
    ///
    /// A [`DeviceCommon`][crate::DeviceCommon] should be implemented by any type of ALSA HwDep device.
    ///
    /// ## Signals
    ///
    ///
    /// #### `handle-disconnection`
    ///  Emitted when the sound card is not available anymore due to unbinding driver or hot
    /// unplugging. The owner of the object should prepare to call `GObject::Object::unref()`
    /// so that ALSA HwDep character device is going to be closed and sound card is released.
    ///
    /// Action
    ///
    /// # Implements
    ///
    /// [`DeviceCommonExt`][trait@crate::prelude::DeviceCommonExt], [`DeviceCommonExtManual`][trait@crate::prelude::DeviceCommonExtManual]
    #[doc(alias = "ALSAHwdepDeviceCommon")]
    pub struct DeviceCommon(Interface<ffi::ALSAHwdepDeviceCommon, ffi::ALSAHwdepDeviceCommonInterface>);

    match fn {
        type_ => || ffi::alsahwdep_device_common_get_type(),
    }
}

impl DeviceCommon {
    pub const NONE: Option<&'static DeviceCommon> = None;
}

/// Trait containing the part of [`struct@DeviceCommon`] methods.
///
/// # Implementors
///
/// [`DeviceCommon`][struct@crate::DeviceCommon]
pub trait DeviceCommonExt: IsA<DeviceCommon> + 'static {
    /// Allocate [`glib::Source`][crate::glib::Source] structure to handle events from ALSA hwdep character device. In
    /// each iteration of `GLib::MainContext`, the `read(2)` system call is executed to dispatch
    /// hwdep event, according to the result of `poll(2)` system call.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    ///
    /// ## `source`
    /// A [`glib::Source`][crate::glib::Source] to handle events from ALSA hwdep character device.
    #[doc(alias = "alsahwdep_device_common_create_source")]
    fn create_source(&self) -> Result<glib::Source, glib::Error> {
        unsafe {
            let mut source = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsahwdep_device_common_create_source(
                self.as_ref().to_glib_none().0,
                &mut source,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(source))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Get the information according to given numeric IDs for card and device. The call of function is
    /// expected to executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_HWDEP_INFO` command to the
    /// character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    ///
    /// ## `device_info`
    /// The information of device.
    #[doc(alias = "alsahwdep_device_common_get_device_info")]
    #[doc(alias = "get_device_info")]
    fn device_info(&self) -> Result<DeviceInfo, glib::Error> {
        unsafe {
            let mut device_info = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsahwdep_device_common_get_device_info(
                self.as_ref().to_glib_none().0,
                &mut device_info,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(device_info))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Open one of ALSA hwdep character devices for the sound card.
    ///
    /// The call of function is expected to execute `open(2)` system call for ALSA hwdep character
    /// device.
    /// ## `card_id`
    /// The numeric identifier of sound card.
    /// ## `device_id`
    /// The numerid identifier of device.
    /// ## `open_flag`
    /// The flag of `open(2)` system call.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsahwdep_device_common_open")]
    fn open(&self, card_id: u32, device_id: u32, open_flag: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsahwdep_device_common_open(
                self.as_ref().to_glib_none().0,
                card_id,
                device_id,
                open_flag,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Emitted when the sound card is not available anymore due to unbinding driver or hot
    /// unplugging. The owner of the object should prepare to call `GObject::Object::unref()`
    /// so that ALSA HwDep character device is going to be closed and sound card is released.
    #[doc(alias = "handle-disconnection")]
    fn connect_handle_disconnection<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn handle_disconnection_trampoline<
            P: IsA<DeviceCommon>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::ALSAHwdepDeviceCommon,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(DeviceCommon::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"handle-disconnection".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    handle_disconnection_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn emit_handle_disconnection(&self) {
        self.emit_by_name::<()>("handle-disconnection", &[]);
    }
}

impl<O: IsA<DeviceCommon>> DeviceCommonExt for O {}