alsactl 0.8.0

API binding for alsactl library
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// 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, CardInfo, ElemEventMask, ElemId, ElemValue};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// An GObject-derived object to express sound card.
    ///
    /// A [`Card`][crate::Card] is a GObject-derived object to express sound card. Applications use the
    /// instance of object to manipulate functionalities on sound card. After the call of
    /// [`CardExt::open()`][crate::prelude::CardExt::open()] for the numeric ID of sound card, the object maintains file descriptor till
    /// object destruction.
    ///
    /// ## Properties
    ///
    ///
    /// #### `devnode`
    ///  The full path to special file of control character device.
    ///
    /// Readable
    ///
    ///
    /// #### `subscribed`
    ///  Whether to be subscribed for event.
    ///
    /// Readable
    ///
    /// ## Signals
    ///
    ///
    /// #### `handle-disconnection`
    ///  Emitted when the sound card is not available anymore due to unbinding driver or hot
    /// unplugging. The owner of this object should call `GObject::Object::unref()` as quickly
    /// as possible to be going to release ALSA control character device.
    ///
    ///
    ///
    ///
    /// #### `handle-elem-event`
    ///  Emitted when event occurs for any element.
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`CardExt`][trait@crate::prelude::CardExt], [`CardExtManual`][trait@crate::prelude::CardExtManual]
    #[doc(alias = "ALSACtlCard")]
    pub struct Card(Object<ffi::ALSACtlCard, ffi::ALSACtlCardClass>);

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

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

    /// Allocate and return an instance of [`Card`][crate::Card].
    ///
    /// # Returns
    ///
    /// An instance of [`Card`][crate::Card].
    #[doc(alias = "alsactl_card_new")]
    pub fn new() -> Card {
        unsafe { from_glib_full(ffi::alsactl_card_new()) }
    }
}

impl Default for Card {
    fn default() -> Self {
        Self::new()
    }
}

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

    /// Get the information of sound card.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_CARD_INFO` command
    /// for ALSA control character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    ///
    /// ## `card_info`
    /// A [`Card`][crate::Card]Info for the sound card.
    #[doc(alias = "alsactl_card_get_info")]
    #[doc(alias = "get_info")]
    fn info(&self) -> Result<CardInfo, glib::Error> {
        unsafe {
            let mut card_info = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_get_info(
                self.as_ref().to_glib_none().0,
                &mut card_info,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(card_info))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Lock/Unlock indicated element not to be written by the other processes.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_ELEM_LOCK` and
    /// `SNDRV_CTL_IOCTL_ELEM_UNLOCK` commands for ALSA control character device.
    /// ## `elem_id`
    /// A [`ElemId`][crate::ElemId].
    /// ## `lock`
    /// whether to lock or unlock the element.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsactl_card_lock_elem")]
    fn lock_elem(&self, elem_id: &ElemId, lock: bool) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_lock_elem(
                self.as_ref().to_glib_none().0,
                elem_id.to_glib_none().0,
                lock.into_glib(),
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Open ALSA control character device for the sound card.
    ///
    /// The call of function executes `open(2)` system call for ALSA control character device.
    /// ## `card_id`
    /// The numeric ID of sound card.
    /// ## `open_flag`
    /// The flag of `open(2)` system call. O_RDONLY is forced to fulfil internally.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsactl_card_open")]
    fn open(&self, card_id: u32, open_flag: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_open(
                self.as_ref().to_glib_none().0,
                card_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))
            }
        }
    }

    /// Remove user-defined elements pointed by the identifier.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_ELEM_REMOVE` command
    /// for ALSA control character device.
    /// ## `elem_id`
    /// A [`ElemId`][crate::ElemId].
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsactl_card_remove_elems")]
    fn remove_elems(&self, elem_id: &ElemId) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_remove_elems(
                self.as_ref().to_glib_none().0,
                elem_id.to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Write the given array of bytes as Type/Length/Value data for element pointed by the identifier.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_TLV_WRITE` command
    /// for ALSA control character device.
    /// ## `elem_id`
    /// A [`ElemId`][crate::ElemId].
    /// ## `container`
    /// The array with qudalets for Type-Length-Value data.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsactl_card_write_elem_tlv")]
    fn write_elem_tlv(&self, elem_id: &ElemId, container: &[u32]) -> Result<(), glib::Error> {
        let container_count = container.len() as _;
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_write_elem_tlv(
                self.as_ref().to_glib_none().0,
                elem_id.to_glib_none().0,
                container.to_glib_none().0,
                container_count,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Write given value to element indicated by the given identifier.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_CTL_IOCTL_ELEM_WRITE` command
    /// for ALSA control character device.
    /// ## `elem_id`
    /// A [`ElemId`][crate::ElemId].
    /// ## `elem_value`
    /// A derivative of #ALSACtlElemValue.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsactl_card_write_elem_value")]
    fn write_elem_value(
        &self,
        elem_id: &ElemId,
        elem_value: &impl IsA<ElemValue>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsactl_card_write_elem_value(
                self.as_ref().to_glib_none().0,
                elem_id.to_glib_none().0,
                elem_value.as_ref().to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// The full path to special file of control character device.
    fn devnode(&self) -> Option<glib::GString> {
        ObjectExt::property(self.as_ref(), "devnode")
    }

    /// Whether to be subscribed for event.
    fn is_subscribed(&self) -> bool {
        ObjectExt::property(self.as_ref(), "subscribed")
    }

    /// Emitted when the sound card is not available anymore due to unbinding driver or hot
    /// unplugging. The owner of this object should call `GObject::Object::unref()` as quickly
    /// as possible to be going to release ALSA control character device.
    #[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<Card>, F: Fn(&P) + 'static>(
            this: *mut ffi::ALSACtlCard,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Card::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),
            )
        }
    }

    /// Emitted when event occurs for any element.
    /// ## `elem_id`
    /// A [`ElemId`][crate::ElemId].
    /// ## `events`
    /// A set of [`ElemEventMask`][crate::ElemEventMask].
    #[doc(alias = "handle-elem-event")]
    fn connect_handle_elem_event<F: Fn(&Self, &ElemId, ElemEventMask) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn handle_elem_event_trampoline<
            P: IsA<Card>,
            F: Fn(&P, &ElemId, ElemEventMask) + 'static,
        >(
            this: *mut ffi::ALSACtlCard,
            elem_id: *mut ffi::ALSACtlElemId,
            events: ffi::ALSACtlElemEventMask,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Card::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(elem_id),
                from_glib(events),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"handle-elem-event".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    handle_elem_event_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "devnode")]
    fn connect_devnode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_devnode_trampoline<P: IsA<Card>, F: Fn(&P) + 'static>(
            this: *mut ffi::ALSACtlCard,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Card::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::devnode".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_devnode_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "subscribed")]
    fn connect_subscribed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_subscribed_trampoline<P: IsA<Card>, F: Fn(&P) + 'static>(
            this: *mut ffi::ALSACtlCard,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Card::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::subscribed".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_subscribed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<Card>> CardExt for O {}