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
/*
 * SPDX-License-Identifier: MIT
 */
use std::ffi::CStr;
use std::ptr::NonNull;
use std::rc::Rc;

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::core_api::context::{Context, ContextRef};
use crate::new_instance_raw_wrapper;
use crate::wrapper::RawWrapper;

pub mod implementation;

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

#[derive(Wrapper, Debug)]
pub struct Protocol {
    #[raw_wrapper]
    ref_: NonNull<ProtocolRef>,

    context: std::sync::Arc<Context>,
}

impl Drop for Protocol {
    fn drop(&mut self) {
        unsafe { pw_sys::pw_protocol_destroy(self.as_raw_ptr()) }
    }
}

impl Protocol {
    pub fn new(context: &std::sync::Arc<Context>, name: &CStr) -> crate::Result<Self> {
        let ptr = unsafe { pw_sys::pw_protocol_new(context.as_raw_ptr(), name.as_ptr(), 0) };
        Ok(Self {
            ref_: new_instance_raw_wrapper(ptr)?,
            context: context.clone(),
        })
    }

    pub fn context(&self) -> &std::sync::Arc<Context> {
        &self.context
    }
}

impl ProtocolRef {
    pub fn get_context(&self) -> &ContextRef {
        unsafe { ContextRef::from_raw_ptr(pw_sys::pw_protocol_get_context(self.as_raw_ptr())) }
    }

    unsafe fn get_user_data<T>(&self) -> Option<&mut T> {
        let ptr = pw_sys::pw_protocol_get_user_data(self.as_raw_ptr()) as *mut T;
        ptr.as_mut()
    }
}