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
/*
 * SPDX-License-Identifier: MIT
 */
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::ptr::{slice_from_raw_parts, NonNull};
use std::slice::from_raw_parts;

use bitflags::bitflags;

use pipewire_wrapper_proc_macro::{RawWrapper, Wrapper};

use crate::core_api::node::Node;
use crate::enum_wrapper;
use crate::spa::dict::DictRef;
use crate::spa::param::{ParamInfo, ParamInfoRef};
use crate::wrapper::{RawWrapper, Wrapper};

bitflags! {
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    #[repr(transparent)]
    pub struct ChangeMask: u64 {
        const INPUT_PORTS = pw_sys::PW_NODE_CHANGE_MASK_INPUT_PORTS as u64;
        const OUTPUT_PORTS = pw_sys::PW_NODE_CHANGE_MASK_OUTPUT_PORTS as u64;
        const STATE = pw_sys::PW_NODE_CHANGE_MASK_STATE as u64;
        const PROPS = pw_sys::PW_NODE_CHANGE_MASK_PROPS as u64;
        const PARAMS = pw_sys::PW_NODE_CHANGE_MASK_PARAMS as u64;
        const ALL = pw_sys::PW_NODE_CHANGE_MASK_ALL as u64;
    }
}

enum_wrapper!(
    NodeState,
    pw_sys::pw_node_state,
    ERROR: pw_sys::pw_node_state_PW_NODE_STATE_ERROR,
    CREATING: pw_sys::pw_node_state_PW_NODE_STATE_CREATING,
    SUSPENDED: pw_sys::pw_node_state_PW_NODE_STATE_SUSPENDED,
    IDLE: pw_sys::pw_node_state_PW_NODE_STATE_IDLE,
    RUNNING: pw_sys::pw_node_state_PW_NODE_STATE_RUNNING,
);

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

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

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

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

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

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

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

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

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

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

    pub fn params(&self) -> &[ParamInfoRef] {
        unsafe {
            from_raw_parts(
                self.raw.params as *mut ParamInfoRef,
                self.raw.n_params as usize,
            )
        }
    }
}

impl Debug for NodeInfoRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NodeInfoRef")
            .field("id", &self.id())
            .field("max_input_ports", &self.max_input_ports())
            .field("max_output_ports", &self.max_output_ports())
            .field("change_mask", &self.change_mask())
            .field("n_input_ports", &self.n_input_ports())
            .field("n_output_ports", &self.n_output_ports())
            .field("state", &self.state())
            .field("error", &self.error())
            .field("props", &self.props())
            .field("params", &self.params())
            .finish()
    }
}

#[derive(Debug, Clone)]
pub struct NodeInfo {
    id: u32,
    max_input_ports: u32,
    max_output_ports: u32,
    change_mask: ChangeMask,
    n_input_ports: u32,
    n_output_ports: u32,
    state: NodeState,
    error: Option<CString>,
    props: HashMap<CString, CString>,
    params: Vec<ParamInfo>,
}

impl NodeInfo {
    pub fn from_ref(ref_: &NodeInfoRef) -> Self {
        let raw = ref_.raw;
        Self {
            id: raw.id,
            max_input_ports: raw.max_input_ports,
            max_output_ports: raw.max_output_ports,
            change_mask: ref_.change_mask(),
            n_input_ports: raw.n_input_ports,
            n_output_ports: raw.n_output_ports,
            state: ref_.state(),
            error: ref_.error().map(CString::from),
            props: ref_.props().into(),
            params: ref_.params().iter().map(ParamInfo::from_ref).collect(),
        }
    }

    pub fn id(&self) -> u32 {
        self.id
    }
    pub fn max_input_ports(&self) -> u32 {
        self.max_input_ports
    }
    pub fn max_output_ports(&self) -> u32 {
        self.max_output_ports
    }
    pub fn change_mask(&self) -> ChangeMask {
        self.change_mask
    }
    pub fn n_input_ports(&self) -> u32 {
        self.n_input_ports
    }
    pub fn n_output_ports(&self) -> u32 {
        self.n_output_ports
    }
    pub fn state(&self) -> NodeState {
        self.state
    }
    pub fn error(&self) -> &Option<CString> {
        &self.error
    }
    pub fn props(&self) -> &HashMap<CString, CString> {
        &self.props
    }
    pub fn params(&self) -> &Vec<ParamInfo> {
        &self.params
    }
}

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