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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 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::{DeviceId, EventType, InstanceInfo, RealTimeEvent, SlaveClass, TickTimeEvent};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// A GObject-derived object to express user instance.
    ///
    /// A [`UserInstance`][crate::UserInstance] is a GObject-derived object to express information of user instance
    /// attached to any timer device or the other instance as slave. After calling
    /// [`UserInstanceExt::open()`][crate::prelude::UserInstanceExt::open()], the object maintains file descriptor till object destruction. After
    /// calling [`UserInstanceExt::attach()`][crate::prelude::UserInstanceExt::attach()] or [`UserInstanceExt::attach_as_slave()`][crate::prelude::UserInstanceExt::attach_as_slave()], the user instance
    /// is attached to any timer device or the other instance as slave.
    ///
    /// ## Signals
    ///
    ///
    /// #### `handle-disconnection`
    ///  Emitted when the attached timer device 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 release ALSA timer character device.
    ///
    ///
    ///
    ///
    /// #### `handle-real-time-event`
    ///  Emitted when event occurs to notify real time.
    ///
    ///
    ///
    ///
    /// #### `handle-tick-time-event`
    ///  Emitted when event occurs to notify tick time.
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`UserInstanceExt`][trait@crate::prelude::UserInstanceExt], [`UserInstanceExtManual`][trait@crate::prelude::UserInstanceExtManual]
    #[doc(alias = "ALSATimerUserInstance")]
    pub struct UserInstance(Object<ffi::ALSATimerUserInstance, ffi::ALSATimerUserInstanceClass>);

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

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

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

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

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

/// Trait containing the part of [`struct@UserInstance`] methods.
///
/// # Implementors
///
/// [`UserInstance`][struct@crate::UserInstance]
pub trait UserInstanceExt: IsA<UserInstance> + sealed::Sealed + 'static {
    /// Attach the instance to the timer device. If the given device_id is for absent timer device, the
    /// instance can be detached with error.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_SELECT` command
    /// for ALSA timer character device.
    /// ## `device_id`
    /// A [`DeviceId`][crate::DeviceId] to which the instance is attached.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_attach")]
    fn attach(&self, device_id: &mut DeviceId) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsatimer_user_instance_attach(
                self.as_ref().to_glib_none().0,
                device_id.to_glib_none_mut().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))
            }
        }
    }

    /// Attach the instance as an slave to another instance indicated by a pair of slave_class and
    /// slave_id. If the slave_class is `SlaveClass:APPLICATION`, the slave_id is for the PID of
    /// application process which owns the instance of timer. If the slave_class is
    /// `SlaveClass:SEQUENCER`, the slave_id is the numeric ID of queue bound for timer device.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_SELECT` command
    /// for ALSA timer character device.
    /// ## `slave_class`
    /// The class identifier of master instance, one of [`SlaveClass`][crate::SlaveClass].
    /// ## `slave_id`
    /// The numeric identifier of master instance.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_attach_as_slave")]
    fn attach_as_slave(&self, slave_class: SlaveClass, slave_id: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsatimer_user_instance_attach_as_slave(
                self.as_ref().to_glib_none().0,
                slave_class.into_glib(),
                slave_id,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Choose the type of event data to receive.
    ///
    /// The call of function is successful just before call of [`attach()`][Self::attach()].
    /// [`EventType`][crate::EventType].TICK_TIME is used as a default if the function is not called for
    /// [`EventType`][crate::EventType].REAL_TIME explicitly. When the former is configured, event for tick time is
    /// available for [`handle_tick_time_event`][struct@crate::UserInstance#handle_tick_time_event]. When the latter is configured,
    /// event for real time is available for [`handle_real_time_event`][struct@crate::UserInstance#handle_real_time_event].
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_TREAD` command
    /// for ALSA timer character device.
    /// ## `event_type`
    /// The type of event data, one of [`EventType`][crate::EventType].
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_choose_event_type")]
    fn choose_event_type(&self, event_type: EventType) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsatimer_user_instance_choose_event_type(
                self.as_ref().to_glib_none().0,
                event_type.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))
            }
        }
    }

    #[doc(alias = "alsatimer_user_instance_continue")]
    #[doc(alias = "continue")]
    fn continue_(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok =
                ffi::alsatimer_user_instance_continue(self.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))
            }
        }
    }

    /// Allocate [`glib::Source`][crate::glib::Source] structure to handle events from ALSA timer character device. In
    /// each iteration of `GLib::MainContext`, the `read(2)` system call is executed to dispatch
    /// timer event for either [`handle-tick-time-event`][struct@crate::UserInstance#handle-tick-time-event] or
    /// [`handle-real-time-event`][struct@crate::UserInstance#handle-real-time-event] signals, 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 timer character device.
    #[doc(alias = "alsatimer_user_instance_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::alsatimer_user_instance_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))
            }
        }
    }

    /// Return the information of device if attached to the instance.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_INFO` command for
    /// ALSA timer character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    ///
    /// ## `instance_info`
    /// A [`InstanceInfo`][crate::InstanceInfo].
    #[doc(alias = "alsatimer_user_instance_get_info")]
    #[doc(alias = "get_info")]
    fn info(&self) -> Result<InstanceInfo, glib::Error> {
        unsafe {
            let mut instance_info = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsatimer_user_instance_get_info(
                self.as_ref().to_glib_none().0,
                &mut instance_info,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(instance_info))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Open ALSA Timer character device to allocate queue.
    ///
    /// The call of function executes `open(2)` system call for ALSA timer character device.
    /// ## `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 = "alsatimer_user_instance_open")]
    fn open(&self, open_flag: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::alsatimer_user_instance_open(
                self.as_ref().to_glib_none().0,
                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))
            }
        }
    }

    /// Pause timer event emission.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_PAUSE` command
    /// for ALSA timer character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_pause")]
    fn pause(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok =
                ffi::alsatimer_user_instance_pause(self.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))
            }
        }
    }

    /// Start timer event emission.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_START` command
    /// for ALSA timer character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_start")]
    fn start(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok =
                ffi::alsatimer_user_instance_start(self.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))
            }
        }
    }

    /// Stop timer event emission.
    ///
    /// The call of function executes `ioctl(2)` system call with `SNDRV_TIMER_IOCTL_STOP` command
    /// for ALSA timer character device.
    ///
    /// # Returns
    ///
    /// [`true`] when the overall operation finishes successfully, else [`false`].
    #[doc(alias = "alsatimer_user_instance_stop")]
    fn stop(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok =
                ffi::alsatimer_user_instance_stop(self.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))
            }
        }
    }

    /// Emitted when the attached timer device 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 release ALSA timer 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<UserInstance>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::ALSATimerUserInstance,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(UserInstance::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"handle-disconnection\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    handle_disconnection_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted when event occurs to notify real time.
    /// ## `event`
    /// The instance of [`RealTimeEvent`][crate::RealTimeEvent].
    #[doc(alias = "handle-real-time-event")]
    fn connect_handle_real_time_event<F: Fn(&Self, &RealTimeEvent) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn handle_real_time_event_trampoline<
            P: IsA<UserInstance>,
            F: Fn(&P, &RealTimeEvent) + 'static,
        >(
            this: *mut ffi::ALSATimerUserInstance,
            event: *mut ffi::ALSATimerRealTimeEvent,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                UserInstance::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(event),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"handle-real-time-event\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    handle_real_time_event_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Emitted when event occurs to notify tick time.
    /// ## `event`
    /// The instance of [`TickTimeEvent`][crate::TickTimeEvent].
    #[doc(alias = "handle-tick-time-event")]
    fn connect_handle_tick_time_event<F: Fn(&Self, &TickTimeEvent) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn handle_tick_time_event_trampoline<
            P: IsA<UserInstance>,
            F: Fn(&P, &TickTimeEvent) + 'static,
        >(
            this: *mut ffi::ALSATimerUserInstance,
            event: *mut ffi::ALSATimerTickTimeEvent,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                UserInstance::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(event),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"handle-tick-time-event\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    handle_tick_time_event_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<UserInstance>> UserInstanceExt for O {}