use crate::{ffi, DeviceInfo};
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::boxed::Box as Box_;
glib::wrapper! {
#[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;
}
pub trait DeviceCommonExt: IsA<DeviceCommon> + 'static {
#[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))
}
}
}
#[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))
}
}
}
#[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))
}
}
}
#[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 {}