Skip to main content

alsaseq/subclass/
user_client.rs

1// SPDX-License-Identifier: MIT
2
3use super::*;
4
5/// Trait which should be implemented by subclass of [`UserClient`][crate::UserClient].
6pub trait UserClientImpl: ObjectImpl + UserClientImplExt {
7    /// When event occurs, this signal is emit with the instance of object which includes batch of
8    /// events.
9    /// ## `ev_cntr`
10    /// The instance of [`EventCntr`][crate::EventCntr] which includes batch of events.
11    fn handle_event(&self, user_client: &Self::Type, event_cntr: &EventCntr) {
12        self.parent_handle_event(user_client, event_cntr)
13    }
14}
15
16/// Trait which is automatically implemented by implementator of
17/// [`UserClientImpl`][self::UserClientImpl].
18pub trait UserClientImplExt: ObjectSubclass {
19    fn parent_handle_event(&self, user_client: &Self::Type, event_cntr: &EventCntr);
20}
21
22impl<T: UserClientImpl> UserClientImplExt for T {
23    fn parent_handle_event(&self, user_client: &Self::Type, event_cntr: &EventCntr) {
24        unsafe {
25            let data = T::type_data();
26            let parent_class = data.as_ref().parent_class() as *mut ffi::ALSASeqUserClientClass;
27            let f = (*parent_class)
28                .handle_event
29                .expect("No parent class implementation for \"handle_event\"");
30            f(
31                user_client.unsafe_cast_ref::<UserClient>().to_glib_none().0,
32                event_cntr.to_glib_none().0,
33            )
34        }
35    }
36}
37
38unsafe impl<T: UserClientImpl> IsSubclassable<T> for UserClient {
39    fn class_init(class: &mut glib::Class<Self>) {
40        Self::parent_class_init::<T>(class);
41
42        let klass = class.as_mut();
43        klass.handle_event = Some(user_client_handle_event::<T>);
44    }
45}
46
47unsafe extern "C" fn user_client_handle_event<T: UserClientImpl>(
48    ptr: *mut ffi::ALSASeqUserClient,
49    event_cntr: *const ffi::ALSASeqEventCntr,
50) {
51    let instance = &*(ptr as *mut T::Instance);
52    let imp = instance.imp();
53    let wrap: Borrowed<UserClient> = from_glib_borrow(ptr);
54
55    imp.handle_event(wrap.unsafe_cast_ref(), &from_glib_none(event_cntr))
56}