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
use {
	crate::{
		prelude::*,
		pw::{self, FromPipewirePropertyString, PipewireObject, Proxy, ProxyFeatures},
		spa::SpaPod,
	},
	pipewire_sys::pw_proxy,
};

impl ProxyFeatures {
	#[doc(alias = "WP_PIPEWIRE_OBJECT_FEATURES_ALL")]
	pub const ALL: Self = Self::from_bits_retain(ffi::WP_PIPEWIRE_OBJECT_FEATURES_ALL as u32);

	/// ProxyFeatures::PROXY_FEATURE_BOUND | ProxyFeatures::PIPEWIRE_OBJECT_FEATURE_INFO
	#[doc(alias = "WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL")]
	pub const MINIMAL: Self = Self::from_bits_retain(ffi::WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL as u32);
}

pub trait ProxyExt2: 'static {
	#[doc(alias = "wp_proxy_get_pw_proxy")]
	#[doc(alias = "get_pw_proxy")]
	fn pw_proxy_raw(&self) -> *mut pw_proxy;

	#[doc(alias = "wp_proxy_set_pw_proxy")]
	fn set_pw_proxy_raw(&self, proxy: *mut pw_proxy);

	// TODO: bound_id() -> Option<>
}

impl<O: IsA<Proxy>> ProxyExt2 for O {
	fn pw_proxy_raw(&self) -> *mut pw_proxy {
		unsafe { ffi::wp_proxy_get_pw_proxy(self.as_ref().to_glib_none().0) }
	}

	fn set_pw_proxy_raw(&self, proxy: *mut pw_proxy) {
		unsafe { ffi::wp_proxy_set_pw_proxy(self.as_ref().to_glib_none().0, proxy) }
	}
}

pub trait PipewireObjectExt2: 'static {
	#[doc(alias = "wp_pipewire_object_get_native_info")]
	#[doc(alias = "get_native_info")]
	fn native_info(&self) -> gconstpointer;

	fn object_id(&self) -> Result<u32, Error>;

	#[doc(alias = "wp_pipewire_object_get_property")]
	#[doc(alias = "get_property")]
	fn get_pw_property(&self, key: &str) -> Option<String>;
	fn get_pw_property_cstring(&self, key: &str) -> Option<CString>;

	fn with_pw_property_cstr<R, F: FnOnce(&CStr) -> R>(&self, key: &str, f: F) -> Option<R>;
	fn with_pw_property<R, F: FnOnce(&str) -> R>(&self, key: &str, f: F) -> Option<R>;

	#[doc(alias = "wp_pipewire_object_get_property")]
	#[doc(alias = "get_property")]
	fn pw_property<T: FromPipewirePropertyString>(&self, key: &str) -> Result<T, Error>;
	fn pw_property_optional<T: FromPipewirePropertyString>(&self, key: &str) -> Result<Option<T>, Error>;

	#[doc(alias = "wp_pipewire_object_enum_params")]
	fn params_future(
		&self,
		id: Option<&str>,
		filter: Option<&SpaPod>,
	) -> Pin<Box<dyn Future<Output = Result<ValueIterator<SpaPod>, Error>> + 'static>>;
}

impl<O: IsA<PipewireObject>> PipewireObjectExt2 for O {
	fn native_info(&self) -> gconstpointer {
		unsafe { ffi::wp_pipewire_object_get_native_info(self.as_ref().to_glib_none().0) }
	}

	fn object_id(&self) -> Result<u32, Error> {
		self.pw_property(pw::PW_KEY_OBJECT_ID)
	}

	fn get_pw_property_cstring(&self, key: &str) -> Option<CString> {
		self.with_pw_property_cstr(key, |cstr| cstr.to_owned())
	}

	fn get_pw_property(&self, key: &str) -> Option<String> {
		self.with_pw_property(key, |str| str.to_owned())
	}

	fn with_pw_property_cstr<R, F: FnOnce(&CStr) -> R>(&self, key: &str, f: F) -> Option<R> {
		let this = self.as_ref();
		unsafe {
			let value = ffi::wp_pipewire_object_get_property(this.to_glib_none().0, key.to_glib_none().0);
			if value.is_null() {
				None
			} else {
				Some(f(CStr::from_ptr(value)))
			}
		}
	}

	fn with_pw_property<R, F: FnOnce(&str) -> R>(&self, key: &str, f: F) -> Option<R> {
		let this = self.as_ref();
		self
			.with_pw_property_cstr(key, move |cstr| match cstr.to_str() {
				Err(e) => {
					wp_warning!(self: this, "pw_property {key} ({cstr:?}) was not valid UTF-8: {e:?}");
					None
				},
				Ok(str) => Some(f(str)),
			})
			.flatten()
	}

	fn pw_property<T: FromPipewirePropertyString>(&self, key: &str) -> Result<T, Error> {
		match self.with_pw_property(key, T::from_pipewire_string) {
			None => Err(Error::new(
				LibraryErrorEnum::InvalidArgument,
				&format!("pw_property {key} not found"),
			)),
			Some(Err(e)) => Err(Error::new(
				LibraryErrorEnum::InvalidArgument,
				&format!("pw_property {key} failed to parse: {e:?}"),
			)),
			Some(Ok(v)) => Ok(v),
		}
	}

	fn pw_property_optional<T: FromPipewirePropertyString>(&self, key: &str) -> Result<Option<T>, Error> {
		self
			.with_pw_property(key, T::from_pipewire_string)
			.transpose()
			.map_err(|e| {
				Error::new(
					LibraryErrorEnum::InvalidArgument,
					&format!("pw_property {key} failed to parse: {e:?}"),
				)
			})
	}

	fn params_future(
		&self,
		id: Option<&str>,
		filter: Option<&SpaPod>,
	) -> Pin<Box<dyn Future<Output = Result<ValueIterator<SpaPod>, Error>> + 'static>> {
		let res = self.enum_params_future(id, filter);
		Box::pin(async move { res.await.map(|r| ValueIterator::with_inner(r.unwrap())) })
	}
}

impl Display for PipewireObject {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if let Some(res) = self.with_pw_property(pw::PW_KEY_OBJECT_PATH, |path| f.write_str(path)) {
			return res
		}

		f.write_str("pw.object")?;

		self
			.with_pw_property(pw::PW_KEY_OBJECT_ID, |id| write!(f, "({id})"))
			.unwrap_or(Ok(()))
	}
}