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
/*
 * SPDX-License-Identifier: MIT
 */
use std::collections::HashMap;
use std::ffi::CString;
use std::{ffi::CStr, fmt::Debug};

use bitflags::bitflags;

use pipewire_wrapper_proc_macro::RawWrapper;

use crate::spa::pod::pod_buf::AllocPod;
use crate::spa::pod::ToOwnedPod;
use crate::{
    enum_wrapper,
    spa::{dict::DictRef, pod::PodRef},
    wrapper::RawWrapper,
};

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct LinkInfoRef {
    #[raw]
    raw: pw_sys::pw_link_info,
}

bitflags! {
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    #[repr(transparent)]
    pub struct ChangeMask: u64 {
        const STATE = pw_sys::PW_LINK_CHANGE_MASK_STATE as u64;
        const FORMAT = pw_sys::PW_LINK_CHANGE_MASK_FORMAT as u64;
        const PROPS = pw_sys::PW_LINK_CHANGE_MASK_PROPS as u64;
        const ALL = pw_sys::PW_LINK_CHANGE_MASK_ALL as u64;
    }
}

enum_wrapper!(
    LinkState,
    pw_sys::pw_link_state,
    ERROR: pw_sys::pw_link_state_PW_LINK_STATE_ERROR,
    UNLINKED: pw_sys::pw_link_state_PW_LINK_STATE_UNLINKED,
    INIT: pw_sys::pw_link_state_PW_LINK_STATE_INIT,
    NEGOTIATING: pw_sys::pw_link_state_PW_LINK_STATE_NEGOTIATING,
    ALLOCATING: pw_sys::pw_link_state_PW_LINK_STATE_ALLOCATING,
    PAUSED: pw_sys::pw_link_state_PW_LINK_STATE_PAUSED,
    ACTIVE: pw_sys::pw_link_state_PW_LINK_STATE_ACTIVE,
);

impl LinkInfoRef {
    pub fn id(&self) -> u32 {
        self.raw.id
    }

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

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

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

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

    pub fn change_mask(&self) -> ChangeMask {
        ChangeMask::from_bits_retain(self.raw.change_mask)
    }

    pub fn state(&self) -> LinkState {
        LinkState::from_raw(self.raw.state)
    }

    pub fn error(&self) -> Option<&CStr> {
        unsafe { self.raw.error.as_ref().map(|s| CStr::from_ptr(s)) }
    }

    pub fn format(&self) -> &PodRef {
        unsafe { PodRef::from_raw_ptr(self.raw.format) }
    }

    pub fn props(&self) -> &DictRef {
        unsafe { DictRef::from_raw_ptr(self.raw.props) }
    }
}

impl Debug for LinkInfoRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LinkInfoRef")
            .field("id", &self.raw.id)
            .field("output_node_id", &self.raw.output_node_id)
            .field("output_port_id", &self.raw.output_port_id)
            .field("input_node_id", &self.raw.input_node_id)
            .field("input_port_id", &self.raw.input_port_id)
            .field("change_mask", &self.raw.change_mask)
            .field("state", &self.raw.state)
            .field("error", &self.raw.error)
            .field("format", &self.raw.format)
            .field("props", &self.raw.props)
            .finish()
    }
}

#[derive(Clone, Debug)]
pub struct LinkInfo {
    id: u32,
    output_node_id: u32,
    output_port_id: u32,
    input_node_id: u32,
    input_port_id: u32,
    change_mask: ChangeMask,
    state: LinkState,
    error: Option<CString>,
    format: AllocPod<PodRef>,
    props: HashMap<CString, CString>,
}

impl LinkInfo {
    pub fn from_ref(ref_: &LinkInfoRef) -> Self {
        Self {
            id: ref_.id(),
            output_node_id: ref_.output_node_id(),
            output_port_id: ref_.output_port_id(),
            input_node_id: ref_.input_node_id(),
            input_port_id: ref_.input_port_id(),
            change_mask: ref_.change_mask(),
            state: ref_.state(),
            error: ref_.error().map(CString::from),
            format: ref_.format().to_owned_pod().unwrap(),
            props: ref_.props().into(),
        }
    }

    pub fn id(&self) -> u32 {
        self.id
    }
    pub fn output_node_id(&self) -> u32 {
        self.output_node_id
    }
    pub fn output_port_id(&self) -> u32 {
        self.output_port_id
    }
    pub fn input_node_id(&self) -> u32 {
        self.input_node_id
    }
    pub fn input_port_id(&self) -> u32 {
        self.input_port_id
    }
    pub fn change_mask(&self) -> ChangeMask {
        self.change_mask
    }
    pub fn state(&self) -> LinkState {
        self.state
    }
    pub fn error(&self) -> &Option<CString> {
        &self.error
    }
    pub fn format(&self) -> &AllocPod<PodRef> {
        &self.format
    }
    pub fn props(&self) -> &HashMap<CString, CString> {
        &self.props
    }
}

impl From<&LinkInfoRef> for LinkInfo {
    fn from(value: &LinkInfoRef) -> Self {
        LinkInfo::from_ref(value)
    }
}