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
/*
 * SPDX-License-Identifier: MIT
 */
use std::ffi::CStr;
use std::fmt::{Debug, Formatter};
use std::pin::Pin;
use std::ptr::NonNull;

use bitflags::Flags;
use derive_builder::Builder;
use pw_sys::pw_registry_events;
use spa_sys::spa_dict;

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::core_api::core::Core;
use crate::core_api::permissions::Permissions;
use crate::core_api::registry::RegistryRef;
use crate::core_api::type_info::TypeInfo;
use crate::events_builder_build;
use crate::spa::dict::DictRef;
use crate::spa::interface::Hook;
use crate::wrapper::RawWrapper;

#[derive(RawWrapper, Debug)]
#[repr(transparent)]
pub struct RegistryEventsRef {
    #[raw]
    raw: pw_sys::pw_registry_events,
}

pub type GlobalCallback<'r> =
    Box<dyn for<'a> FnMut(u32, Permissions, TypeInfo<'a>, u32, &'a DictRef) + 'r>;
pub type GlobalRemoveCallback<'r> = Box<dyn FnMut(u32) + 'r>;

#[derive(Wrapper, Builder)]
#[builder(setter(skip, strip_option), build_fn(skip), pattern = "owned")]
pub struct RegistryEvents<'r> {
    #[raw_wrapper]
    ref_: NonNull<RegistryEventsRef>,

    raw: Pin<Box<RegistryEventsRef>>,
    hook: Pin<Box<Hook>>,

    #[builder(setter)]
    global: Option<GlobalCallback<'r>>,
    #[builder(setter)]
    global_remove: Option<GlobalRemoveCallback<'r>>,
}

impl<'r> RegistryEvents<'r> {
    unsafe extern "C" fn global_call(
        object: *mut ::std::os::raw::c_void,
        id: u32,
        permissions: u32,
        type_: *const ::std::os::raw::c_char,
        version: u32,
        props: *const spa_dict,
    ) {
        if let Some(registry_events) = (object as *mut RegistryEvents).as_mut() {
            if let Some(callback) = &mut registry_events.global {
                callback(
                    id,
                    Permissions::from_bits_retain(permissions),
                    TypeInfo::from_c_str(CStr::from_ptr(type_)),
                    version,
                    DictRef::from_raw_ptr(props),
                );
            }
        }
    }

    unsafe extern "C" fn global_remove_call(object: *mut ::std::os::raw::c_void, id: u32) {
        if let Some(registry_events) = (object as *mut RegistryEvents).as_mut() {
            if let Some(callback) = &mut registry_events.global_remove {
                callback(id);
            }
        }
    }

    pub fn hook(&self) -> &Pin<Box<Hook>> {
        &self.hook
    }

    pub fn version(&self) -> u32 {
        self.raw.raw.version
    }
}

// todo: channel builder

impl<'c> RegistryEventsBuilder<'c> {
    events_builder_build! {
        RegistryEvents<'c>,
        pw_registry_events,
        global => global_call,
        global_remove => global_remove_call,
    }
}

impl Debug for RegistryEvents<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RegistryEvents")
            .field("raw", &self.raw)
            .finish()
    }
}