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
#[cfg(any(feature = "v0_4_2"))]
use crate::plugin::LookupDirs;
#[cfg(feature = "lua")]
use crate::{lua::ToLuaTable, plugin::ComponentLoader};
use {
	crate::{prelude::*, Core, InitFlags, Properties},
	glib::{MainContext, MainLoop},
	pipewire_sys::{pw_context, pw_core},
};

impl Core {
	#[doc(alias = "wp_init")]
	pub fn init_with_flags(flags: InitFlags) {
		unsafe { ffi::wp_init(flags.into_glib()) }
	}

	#[doc(alias = "wp_init")]
	pub fn init() {
		Self::init_with_flags(InitFlags::ALL)
	}

	#[cfg(feature = "v0_4_12")]
	#[cfg_attr(docsrs, doc(cfg(feature = "v0_4_12")))]
	#[doc(alias = "wp_get_library_version")]
	pub fn library_version() -> String {
		unsafe { from_glib_full(ffi::wp_get_library_version()) }
	}

	#[cfg(feature = "v0_4_12")]
	#[cfg_attr(docsrs, doc(cfg(feature = "v0_4_12")))]
	#[doc(alias = "wp_get_library_api_version")]
	pub fn library_api_version() -> String {
		unsafe { from_glib_full(ffi::wp_get_library_api_version()) }
	}

	#[doc(alias = "wp_get_module_dir")]
	pub fn module_dir() -> String {
		unsafe { from_glib_full(ffi::wp_get_module_dir()) }
	}

	#[cfg_attr(feature = "v0_4_2", deprecated = "use `find_file` instead")]
	#[doc(alias = "wp_get_config_dir")]
	pub fn config_dir() -> String {
		unsafe { from_glib_full(ffi::wp_get_config_dir()) }
	}

	#[cfg_attr(feature = "v0_4_2", deprecated = "use `find_file` instead")]
	#[doc(alias = "wp_get_data_dir")]
	pub fn data_dir() -> String {
		unsafe { from_glib_full(ffi::wp_get_data_dir()) }
	}

	#[cfg(feature = "v0_4_2")]
	#[cfg_attr(docsrs, doc(cfg(feature = "v0_4_2")))]
	#[doc(alias = "wp_find_file")]
	pub fn find_file(dirs: LookupDirs, filename: &str, subdir: Option<&str>) -> Option<String> {
		unsafe {
			from_glib_full(ffi::wp_find_file(
				dirs.into_glib(),
				filename.to_glib_none().0,
				subdir.to_glib_none().0,
			))
		}
	}

	#[cfg(feature = "v0_4_2")]
	#[cfg_attr(docsrs, doc(cfg(feature = "v0_4_2")))]
	#[doc(alias = "wp_new_files_iterator")]
	pub fn find_files(dirs: LookupDirs, subdir: Option<&str>, suffix: Option<&str>) -> IntoValueIterator<String> {
		unsafe {
			IntoValueIterator::with_inner(from_glib_full(ffi::wp_new_files_iterator(
				dirs.into_glib(),
				subdir.to_glib_none().0,
				suffix.to_glib_none().0,
			)))
		}
	}

	#[doc(alias = "wp_core_clone")]
	pub fn clone_context(&self) -> Option<Self> {
		unsafe { from_glib_full(ffi::wp_core_clone(self.to_glib_none().0)) }
	}

	#[doc(alias = "wp_core_get_g_main_context")]
	#[doc(alias = "get_g_main_context")]
	pub fn default_context(&self) -> MainContext {
		self
			.g_main_context()
			.unwrap_or_else(|| MainContext::ref_thread_default())
	}

	#[doc(alias = "wp_core_get_pw_core")]
	#[doc(alias = "get_pw_core")]
	pub fn pw_core_raw(&self) -> *mut pw_core {
		unsafe { ffi::wp_core_get_pw_core(self.to_glib_none().0) }
	}

	#[doc(alias = "wp_core_get_pw_context")]
	#[doc(alias = "get_pw_context")]
	pub fn pw_context_raw(&self) -> NonNull<pw_context> {
		unsafe { NonNull::new(ffi::wp_core_get_pw_context(self.to_glib_none().0)).expect("pw_context for WpCore") }
	}

	#[cfg(feature = "lua")]
	#[cfg_attr(docsrs, doc(cfg(feature = "lua")))]
	#[doc(alias = "wp_core_load_component")]
	pub fn load_lua_script<A: ToLuaTable>(&self, script_path: &str, args: A) -> Result<(), Error> {
		let args = args
			.to_lua_variant()
			.and_then(|v| v.map(|v| v.into_vardict()).transpose())?;
		self.load_component(
			script_path,
			ComponentLoader::TYPE_LUA_SCRIPT,
			args.as_ref().map(|v| v.as_variant()),
		)
	}

	#[cfg(feature = "futures")]
	#[cfg_attr(docsrs, doc(cfg(feature = "futures")))]
	#[doc(alias = "wp_core_connect")]
	#[doc(alias = "connect")]
	pub fn connect_future(&self) -> impl Future<Output = Result<(), Error>> {
		use crate::util::futures::signal_once;

		let connect = signal_once(match () {
			#[cfg(feature = "glib-signal")]
			() => self.signal_stream(Self::SIGNAL_CONNECTED),
			#[cfg(not(feature = "glib-signal"))]
			() => |handler| self.connect_connected(handler),
		});

		let res = match self.connect() {
			true => Ok(connect),
			false => Err(Error::new(
				LibraryErrorEnum::OperationFailed,
				"failed to connect to pipewire",
			)),
		};

		async move {
			match res {
				Ok(connect) => connect.await,
				Err(e) => Err(e),
			}
		}
	}

	pub fn run<F: FnOnce(&MainContext, MainLoop, Core)>(props: Option<Properties>, setup: F) {
		let mainloop = MainLoop::new(None, false);
		let context = mainloop.context();
		let core = context
			.with_thread_default(|| {
				let core = Core::new(Some(&context), props);
				let _disconnect_handler = core.connect_disconnected({
					let mainloop = mainloop.clone();
					move |_core| mainloop.quit()
				});

				setup(&context, mainloop.clone(), core.clone());

				mainloop.run();

				core
			})
			.unwrap();

		core.disconnect();
	}
}

#[test]
#[cfg(any(feature = "v0_4_2"))]
fn wp_new_files_iterator() {
	let file = Core::find_file(LookupDirs::PREFIX_SHARE, "create-item.lua", Some("scripts"));
	assert!(file.is_some());

	let files = Core::find_files(LookupDirs::PREFIX_SHARE, None, Some(".conf")).into_iter();
	assert_ne!(0, files.count());
}