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
/*
 * SPDX-License-Identifier: MIT
 */

//! PipeWire [Proxy](https://docs.pipewire.org/group__pw__proxy.html) bindings.
//!
use std::ffi::CStr;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::ptr::NonNull;
use std::rc::Rc;

use pipewire_wrapper_proc_macro::RawWrapper;

use crate::core_api::core::Core;
use crate::core_api::proxy::events::ProxyEvents;
use crate::core_api::type_info::TypeInfo;
use crate::error::Error;
use crate::i32_as_void_result;
use crate::impl_api::protocol::ProtocolRef;
use crate::listeners::AddListener;
use crate::spa::SPA_ID_INVALID;
use crate::wrapper::{RawWrapper, Wrapper};

pub mod events;

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

#[derive(Debug)]
pub struct Proxy<'c> {
    inner: Rc<InnerProxy>,

    core: &'c Core,
}

#[derive(Debug)]
struct InnerProxy {
    ref_: NonNull<ProxyRef>,
}

pub trait Proxied: RawWrapper {
    fn type_info() -> TypeInfo<'static>;

    fn as_proxy(&self) -> &ProxyRef {
        unsafe { ProxyRef::from_raw_ptr(self.as_raw_ptr() as *mut _) }
    }
}

impl<'c> Proxy<'c> {
    pub(crate) fn from_ref(core: &'c Core, ref_: &ProxyRef) -> Self {
        Self {
            inner: Rc::new(InnerProxy {
                ref_: NonNull::new(ref_.as_ptr()).unwrap(),
            }),
            core,
        }
    }

    pub fn core(&self) -> &'c Core {
        self.core
    }
}

impl<'c> Wrapper for Proxy<'c> {
    type RawWrapperType = ProxyRef;
}

impl<'c> AsMut<ProxyRef> for Proxy<'c> {
    fn as_mut(&mut self) -> &'c mut ProxyRef {
        unsafe { &mut *self.inner.ref_.as_ptr() }
    }
}

impl<'c> AsRef<ProxyRef> for Proxy<'c> {
    fn as_ref(&self) -> &'c ProxyRef {
        unsafe { self.inner.ref_.as_ref() }
    }
}

impl<'c> Deref for Proxy<'c> {
    type Target = <Self as crate::wrapper::Wrapper>::RawWrapperType;

    fn deref(&self) -> &'c Self::Target {
        unsafe { self.inner.ref_.as_ref() }
    }
}

impl<'c> DerefMut for Proxy<'c> {
    fn deref_mut(&mut self) -> &'c mut Self::Target {
        unsafe { &mut *self.inner.ref_.as_ptr() }
    }
}

impl Drop for InnerProxy {
    fn drop(&mut self) {
        unsafe { pw_sys::pw_proxy_destroy(self.ref_.as_ref().as_raw_ptr()) }
    }
}

impl<'c> Clone for Proxy<'c> {
    fn clone(&self) -> Self {
        let cloned = Self {
            inner: self.inner.clone(),
            core: self.core,
        };
        unsafe { pw_sys::pw_proxy_ref(self.as_raw_ptr()) };
        cloned
    }
}

impl<'c> Drop for Proxy<'c> {
    fn drop(&mut self) {
        if Rc::strong_count(&self.inner) > 1 {
            unsafe { pw_sys::pw_proxy_unref(self.as_raw_ptr()) }
        }
    }
}

impl ProxyRef {
    //todo add_object_listener
    //todo get_user_data

    pub fn get_id(&self) -> u32 {
        unsafe { pw_sys::pw_proxy_get_id(self.as_raw_ptr()) }
    }

    pub fn is_bound(&self) -> bool {
        self.get_id() != SPA_ID_INVALID
    }

    pub fn get_type_and_version(&self) -> (TypeInfo, u32) {
        let mut version = 0u32;
        let type_str = unsafe { pw_sys::pw_proxy_get_type(self.as_raw_ptr(), &mut version) };
        unsafe { (TypeInfo::from_c_str(CStr::from_ptr(type_str)), version) }
    }

    pub fn get_type(&self) -> TypeInfo {
        self.get_type_and_version().0
    }

    pub fn get_protocol(&self) -> &ProtocolRef {
        unsafe { ProtocolRef::from_raw_ptr(pw_sys::pw_proxy_get_protocol(self.as_raw_ptr())) }
    }

    pub fn sync(&self, seq: i32) -> crate::Result<()> {
        let result = unsafe { pw_sys::pw_proxy_sync(self.as_raw_ptr(), seq) };
        i32_as_void_result(result)
    }

    pub fn get_bound_id(&self) -> Option<u32> {
        let id = unsafe { pw_sys::pw_proxy_get_bound_id(self.as_raw_ptr()) };
        if id == SPA_ID_INVALID {
            None
        } else {
            Some(id)
        }
    }

    pub fn error(&self, res: i32, error: &CStr) -> crate::Result<()> {
        let result = unsafe { pw_sys::pw_proxy_error(self.as_raw_ptr(), res, error.as_ptr()) };
        i32_as_void_result(result)
    }

    // todo errorf
    //todo get_object_listeners
    //todo get_get_marshal
    //todo get_install_marshal

    pub fn as_object<T>(&self) -> crate::Result<&T>
    where
        T: Proxied,
    {
        let proxy_type = self.get_type();
        let target_type = T::type_info();
        if proxy_type == target_type {
            unsafe { Ok(self.as_object_unchecked()) }
        } else {
            Err(Error::TypeMismatch)
        }
    }

    pub(crate) unsafe fn as_object_unchecked<T>(&self) -> &'_ T
    where
        T: Proxied,
    {
        T::from_raw_ptr(self.as_raw_ptr().cast())
    }
}

impl<'a> AddListener<'a> for ProxyRef {
    type Events = ProxyEvents<'a>;

    fn add_listener(&self, events: Pin<Box<Self::Events>>) -> Pin<Box<Self::Events>> {
        unsafe {
            pw_sys::pw_proxy_add_listener(
                self.as_raw_ptr(),
                events.hook().as_raw_ptr(),
                events.as_raw_ptr(),
                &*events as *const _ as *mut _,
            )
        }

        events
    }
}