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

use derive_builder::Builder;
use pw_sys::{pw_context_events, pw_global, pw_impl_client};

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::core_api::context::{Context, ContextRef};
use crate::events_builder_build;
use crate::impl_api::global::GlobalRef;
use crate::impl_api::impl_client::ImplClientRef;
use crate::spa::interface::Hook;
use crate::wrapper::{RawWrapper, Wrapper};

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

pub type DestroyCallback<'c> = Box<dyn FnMut() + 'c>;
pub type FreeCallback<'c> = Box<dyn FnMut() + 'c>;
pub type CheckAccessCallback<'c> = Box<dyn for<'a> FnMut(&'a ImplClientRef) + 'c>;
pub type GlobalAddedCallback<'c> = Box<dyn for<'a> FnMut(&'a GlobalRef) + 'c>;
pub type GlobalRemovedCallback<'c> = Box<dyn for<'a> FnMut(&'a GlobalRef) + 'c>;

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

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

    #[builder(setter)]
    destroy: Option<DestroyCallback<'c>>,
    #[builder(setter)]
    free: Option<FreeCallback<'c>>,
    #[builder(setter)]
    check_access: Option<CheckAccessCallback<'c>>,
    #[builder(setter)]
    global_added: Option<GlobalAddedCallback<'c>>,
    #[builder(setter)]
    global_removed: Option<GlobalRemovedCallback<'c>>,
}

impl<'c> ContextEvents<'c> {
    unsafe extern "C" fn destroy_call(data: *mut ::std::os::raw::c_void) {
        if let Some(context_events) = (data as *mut ContextEvents).as_mut() {
            if let Some(callback) = &mut context_events.destroy {
                callback();
            }
        }
    }

    unsafe extern "C" fn free_call(data: *mut ::std::os::raw::c_void) {
        if let Some(context_events) = (data as *mut ContextEvents).as_mut() {
            if let Some(callback) = &mut context_events.free {
                callback();
            }
        }
    }

    unsafe extern "C" fn check_access_call(
        data: *mut ::std::os::raw::c_void,
        client: *mut pw_impl_client,
    ) {
        if let Some(context_events) = (data as *mut ContextEvents).as_mut() {
            if let Some(callback) = &mut context_events.check_access {
                callback(ImplClientRef::from_raw_ptr(client));
            }
        }
    }

    unsafe extern "C" fn global_added_call(
        data: *mut ::std::os::raw::c_void,
        global: *mut pw_global,
    ) {
        if let Some(context_events) = (data as *mut ContextEvents).as_mut() {
            if let Some(callback) = &mut context_events.global_added {
                callback(GlobalRef::from_raw_ptr(global));
            }
        }
    }

    unsafe extern "C" fn global_removed_call(
        data: *mut ::std::os::raw::c_void,
        global: *mut pw_global,
    ) {
        if let Some(context_events) = (data as *mut ContextEvents).as_mut() {
            if let Some(callback) = &mut context_events.global_removed {
                callback(GlobalRef::from_raw_ptr(global));
            }
        }
    }

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

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

impl Drop for ContextEvents<'_> {
    fn drop(&mut self) {
        // handled by hook
    }
}

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

// todo: channel builder

impl<'c> ContextEventsBuilder<'c> {
    events_builder_build! {
        ContextEvents<'c>,
        pw_context_events,
        destroy => destroy_call,
        free => free_call,
        check_access => check_access_call,
        global_added => global_added_call,
        global_removed => global_removed_call,
    }
}