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

//! PipeWire [Client](https://docs.pipewire.org/group__pw__client.html) bindings.
//!
use std::pin::Pin;

use pipewire_wrapper_proc_macro::{interface, proxy_wrapper, RawWrapper};

use crate::core_api::client::events::ClientEvents;
use crate::core_api::core::Core;
use crate::core_api::proxy::{Proxy, ProxyRef};
use crate::core_api::registry::restricted::RegistryBind;
use crate::listeners::{AddListener, Listeners, OwnListeners};
use crate::spa_interface_call;
use crate::wrapper::{RawWrapper, Wrapper};

pub mod events;
pub mod info;

/// Wrapper for the external [pw_sys::pw_client] value.
#[derive(RawWrapper, Debug)]
#[interface(methods=pw_sys::pw_client_methods, interface="Client")]
#[repr(transparent)]
pub struct ClientRef {
    #[raw]
    raw: pw_sys::pw_client,
}

impl<'a> AddListener<'a> for ClientRef {
    type Events = ClientEvents<'a>;

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

        events
    }
}

/// Wrapper for the Client proxy, can be obtained from the [crate::core_api::registry::Registry].
#[derive(Clone)]
#[proxy_wrapper(ClientRef)]
pub struct Client<'c> {
    ref_: Proxy<'c>,

    listeners: Listeners<Pin<Box<ClientEvents<'c>>>>,
}

impl<'c> RegistryBind<'c> for Client<'c> {
    fn from_ref(core: &'c Core, ref_: &ProxyRef) -> Self {
        Self {
            ref_: Proxy::from_ref(core, ref_),
            listeners: Listeners::default(),
        }
    }
}

impl<'a> OwnListeners<'a> for Client<'a> {
    fn listeners(
        &self,
    ) -> &Listeners<Pin<Box<<<Self as Wrapper>::RawWrapperType as AddListener<'a>>::Events>>> {
        &self.listeners
    }
}