libpulse_binding/context/
ext_device_manager.rs1use std::ffi::{CStr, CString};
17use std::borrow::Cow;
18use std::os::raw::{c_char, c_void};
19use std::ptr::{null, null_mut};
20use capi::pa_ext_device_manager_info as InfoInternal;
21use capi::pa_ext_device_manager_role_priority_info as RolePriorityInfoInternal;
22use super::{ContextInternal, Context};
23use crate::def;
24use crate::callbacks::{ListResult, box_closure_get_capi_ptr, callback_for_list_instance};
25use crate::operation::Operation;
26
27#[derive(Debug)]
29pub struct RolePriorityInfo<'a> {
30 pub role: Option<Cow<'a, str>>,
32 pub priority: u32,
34}
35
36impl RolePriorityInfo<'_> {
37 fn new_from_raw(p: *const RolePriorityInfoInternal) -> Self {
38 assert!(!p.is_null());
39 let src = unsafe { p.as_ref().unwrap() };
40 unsafe {
41 RolePriorityInfo {
42 role: match src.role.is_null() {
43 false => Some(CStr::from_ptr(src.role).to_string_lossy()),
44 true => None,
45 },
46 priority: src.priority,
47 }
48 }
49 }
50
51 pub fn to_owned(&self) -> RolePriorityInfo<'static> {
53 RolePriorityInfo {
54 role: self.role.clone().map(|o| Cow::Owned(o.into_owned())),
55 ..*self
56 }
57 }
58}
59
60#[derive(Debug)]
63pub struct Info<'a> {
64 pub name: Option<Cow<'a, str>>,
67 pub description: Option<Cow<'a, str>>,
69 pub icon: Option<Cow<'a, str>>,
71 pub index: Option<u32>,
73 pub role_priorities: Vec<RolePriorityInfo<'a>>,
75}
76
77impl Info<'_> {
78 fn new_from_raw(p: *const InfoInternal) -> Self {
79 assert!(!p.is_null());
80 let src = unsafe { p.as_ref().unwrap() };
81
82 let mut rp_vec = Vec::with_capacity(src.n_role_priorities as usize);
83 assert!(src.n_role_priorities == 0 || !src.role_priorities.is_null());
84 for i in 0..src.n_role_priorities as isize {
85 let indexed_ptr = unsafe { src.role_priorities.offset(i) as *mut RolePriorityInfoInternal };
86 if !indexed_ptr.is_null() {
87 rp_vec.push(RolePriorityInfo::new_from_raw(indexed_ptr));
88 }
89 }
90
91 unsafe {
92 Info {
93 name: match src.name.is_null() {
94 false => Some(CStr::from_ptr(src.name).to_string_lossy()),
95 true => None,
96 },
97 description: match src.description.is_null() {
98 false => Some(CStr::from_ptr(src.description).to_string_lossy()),
99 true => None,
100 },
101 icon: match src.icon.is_null() {
102 false => Some(CStr::from_ptr(src.icon).to_string_lossy()),
103 true => None,
104 },
105 index: match src.index {
106 def::INVALID_INDEX => None,
107 i => Some(i),
108 },
109 role_priorities: rp_vec,
110 }
111 }
112 }
113
114 pub fn to_owned(&self) -> Info<'static> {
116 Info {
117 name: self.name.clone().map(|o| Cow::Owned(o.into_owned())),
118 description: self.description.clone().map(|o| Cow::Owned(o.into_owned())),
119 icon: self.icon.clone().map(|o| Cow::Owned(o.into_owned())),
120 role_priorities: self.role_priorities.iter().map(RolePriorityInfo::to_owned).collect(),
121 ..*self
122 }
123 }
124}
125
126pub struct DeviceManager {
130 context: *mut ContextInternal,
131 cb_ptrs: CallbackPointers,
133}
134
135unsafe impl Send for DeviceManager {}
136unsafe impl Sync for DeviceManager {}
137
138#[derive(Default)]
141struct CallbackPointers {
142 subscribe: super::ExtSubscribeCb,
143}
144
145impl Context {
146 pub fn device_manager(&self) -> DeviceManager {
151 unsafe { capi::pa_context_ref(self.ptr) };
152 DeviceManager::from_raw(self.ptr)
153 }
154}
155
156impl DeviceManager {
157 fn from_raw(context: *mut ContextInternal) -> Self {
159 Self { context: context, cb_ptrs: Default::default() }
160 }
161
162 pub fn test<F>(&mut self, callback: F) -> Operation<dyn FnMut(u32)>
166 where F: FnMut(u32) + 'static
167 {
168 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(u32)>(Box::new(callback));
169 let ptr = unsafe { capi::pa_ext_device_manager_test(self.context,
170 Some(super::ext_test_cb_proxy), cb_data) };
171 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(u32)>)
172 }
173
174 pub fn read<F>(&mut self, callback: F) -> Operation<dyn FnMut(ListResult<&Info>)>
178 where F: FnMut(ListResult<&Info>) + 'static
179 {
180 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(ListResult<&Info>)>(Box::new(callback));
181 let ptr = unsafe { capi::pa_ext_device_manager_read(self.context, Some(read_list_cb_proxy),
182 cb_data) };
183 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(ListResult<&Info>)>)
184 }
185
186 pub fn set_device_description<F>(&mut self, device: &str, description: &str, callback: F)
192 -> Operation<dyn FnMut(bool)>
193 where F: FnMut(bool) + 'static
194 {
195 let c_dev = CString::new(device).unwrap();
198 let c_desc = CString::new(description).unwrap();
199
200 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
201 let ptr = unsafe {
202 capi::pa_ext_device_manager_set_device_description(self.context, c_dev.as_ptr(),
203 c_desc.as_ptr(), Some(super::success_cb_proxy), cb_data)
204 };
205 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
206 }
207
208 pub fn delete<F>(&mut self, devices: &[&str], callback: F) -> Operation<dyn FnMut(bool)>
214 where F: FnMut(bool) + 'static
215 {
216 let mut c_devs: Vec<CString> = Vec::with_capacity(devices.len());
219 for device in devices {
220 c_devs.push(CString::new(*device).unwrap());
221 }
222
223 let mut c_dev_ptrs: Vec<*const c_char> = Vec::with_capacity(c_devs.len() + 1);
226 for c_dev in &c_devs {
227 c_dev_ptrs.push(c_dev.as_ptr());
228 }
229 c_dev_ptrs.push(null());
230
231 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
232 let ptr = unsafe { capi::pa_ext_device_manager_delete(self.context, c_dev_ptrs.as_ptr(),
233 Some(super::success_cb_proxy), cb_data) };
234 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
235 }
236
237 pub fn enable_role_device_priority_routing<F>(&mut self, enable: bool, callback: F)
243 -> Operation<dyn FnMut(bool)>
244 where F: FnMut(bool) + 'static
245 {
246 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
247 let ptr = unsafe {
248 capi::pa_ext_device_manager_enable_role_device_priority_routing(self.context,
249 enable as i32, Some(super::success_cb_proxy), cb_data)
250 };
251 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
252 }
253
254 pub fn reorder_devices_for_role<F>(&mut self, role: &str, devices: &[&str], callback: F)
260 -> Operation<dyn FnMut(bool)>
261 where F: FnMut(bool) + 'static
262 {
263 let c_role = CString::new(role).unwrap();
266 let mut c_devs: Vec<CString> = Vec::with_capacity(devices.len());
267 for device in devices {
268 c_devs.push(CString::new(*device).unwrap());
269 }
270
271 let mut c_dev_ptrs: Vec<*const c_char> = Vec::with_capacity(c_devs.len() + 1);
274 for c_dev in &c_devs {
275 c_dev_ptrs.push(c_dev.as_ptr());
276 }
277 c_dev_ptrs.push(null());
278
279 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
280 let ptr = unsafe {
281 capi::pa_ext_device_manager_reorder_devices_for_role(self.context, c_role.as_ptr(),
282 c_dev_ptrs.as_ptr(), Some(super::success_cb_proxy), cb_data)
283 };
284 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
285 }
286
287 pub fn subscribe<F>(&mut self, enable: bool, callback: F) -> Operation<dyn FnMut(bool)>
293 where F: FnMut(bool) + 'static
294 {
295 let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
296 let ptr = unsafe { capi::pa_ext_device_manager_subscribe(self.context, enable as i32,
297 Some(super::success_cb_proxy), cb_data) };
298 Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
299 }
300
301 pub fn set_subscribe_cb<F>(&mut self, callback: F)
304 where F: FnMut() + 'static
305 {
306 let saved = &mut self.cb_ptrs.subscribe;
307 *saved = super::ExtSubscribeCb::new(Some(Box::new(callback)));
308 let (cb_fn, cb_data) = saved.get_capi_params(super::ext_subscribe_cb_proxy);
309 unsafe { capi::pa_ext_device_manager_set_subscribe_cb(self.context, cb_fn, cb_data) };
310 }
311}
312
313impl Drop for DeviceManager {
314 fn drop(&mut self) {
315 unsafe { capi::pa_context_unref(self.context) };
316 self.context = null_mut::<ContextInternal>();
317 }
318}
319
320extern "C"
324fn read_list_cb_proxy(_: *mut ContextInternal, i: *const InfoInternal, eol: i32,
325 userdata: *mut c_void)
326{
327 let _ = std::panic::catch_unwind(|| {
328 callback_for_list_instance(i, eol, userdata, Info::new_from_raw);
329 });
330}