| 1 |
// Copyright © 2017 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details. |
| 5 |
|
| 6 |
use ffi; |
| 7 |
use std::ffi::CStr; |
| 8 |
use std::mem::{forget, MaybeUninit}; |
| 9 |
use std::os::raw::{c_int, c_void}; |
| 10 |
use std::ptr; |
| 11 |
use util::UnwrapCStr; |
| 12 |
use *; |
| 13 |
|
| 14 |
// A note about `wrapped` functions |
| 15 |
// |
| 16 |
// C FFI demands `unsafe extern fn(*mut pa_context, ...) -> i32`, etc, |
| 17 |
// but we want to allow such callbacks to be safe. This means no |
| 18 |
// `unsafe` or `extern`, and callbacks should be called with a safe |
| 19 |
// wrapper of `*mut pa_context`. Since the callback doesn't take |
| 20 |
// ownership, this is `&Context`. `fn wrapped<T>(...)` defines a |
| 21 |
// function that converts from our safe signature to the unsafe |
| 22 |
// signature. |
| 23 |
// |
| 24 |
// Currently, we use a property of Rust, namely that each function |
| 25 |
// gets its own unique type. These unique types can't be written |
| 26 |
// directly, so we use generic and a type parameter, and let the Rust |
| 27 |
// compiler fill in the name for us: |
| 28 |
// |
| 29 |
// fn get_sink_input_info<CB>(&self, ..., _: CB, ...) -> ... |
| 30 |
// where CB: Fn(&Context, *const SinkInputInfo, i32, *mut c_void) |
| 31 |
// |
| 32 |
// Because we aren't storing or passing any state, we assert, at run-time :-(, |
| 33 |
// that our functions are zero-sized: |
| 34 |
// |
| 35 |
// assert!(mem::size_of::<F>() == 0); |
| 36 |
// |
| 37 |
// We need to obtain a value of type F in order to call it. Since we |
| 38 |
// can't name the function, we have to unsafely construct that value |
| 39 |
// somehow - we do this using mem::uninitialized. Then, we call that |
| 40 |
// function with a reference to the Context, and save the result: |
| 41 |
// |
| 42 |
// | generate value || call it | |
| 43 |
// let result = ::std::mem::uninitialized::<F>()(&mut object); |
| 44 |
// |
| 45 |
// Lastly, since our Object is an owned type, we need to avoid |
| 46 |
// dropping it, then return the result we just generated. |
| 47 |
// |
| 48 |
// mem::forget(object); |
| 49 |
// result |
| 50 |
|
| 51 |
// Aid in returning Operation from callbacks |
| 52 |
macro_rules! op_or_err { |
| 53 |
($self_:ident, $e:expr) => {{ |
| 54 |
let o = unsafe { $e }; |
| 55 |
if o.is_null() { |
| 56 |
Err(ErrorCode::from_error_code($self_.errno())) |
| 57 |
} else { |
| 58 |
Ok(unsafe { operation::from_raw_ptr(o) }) |
| 59 |
} |
| 60 |
}}; |
| 61 |
} |
| 62 |
|
| 63 |
#[repr(C)] |
| 64 |
#[derive(Debug)] |
| 65 |
pub struct Context(*mut ffi::pa_context); |
| 66 |
|
| 67 |
impl Context { |
| 68 |
pub fn new<'a, OPT>(api: &MainloopApi, name: OPT) -> Option<Self> |
| 69 |
where |
| 70 |
OPT: Into<Option<&'a CStr>>, |
| 71 |
{ |
| 72 |
let ptr = unsafe { ffi::pa_context_new(api.raw_mut(), name.unwrap_cstr()) }; |
| 73 |
if ptr.is_null() { |
| 74 |
None |
| 75 |
} else { |
| 76 |
Some(Context(ptr)) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[doc(hidden)] |
| 81 |
pub fn raw_mut(&self) -> &mut ffi::pa_context { |
| 82 |
unsafe { &mut *self.0 } |
| 83 |
} |
| 84 |
|
| 85 |
pub fn unref(self) { |
| 86 |
unsafe { |
| 87 |
ffi::pa_context_unref(self.raw_mut()); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub fn clear_state_callback(&self) { |
| 92 |
unsafe { |
| 93 |
ffi::pa_context_set_state_callback(self.raw_mut(), None, ptr::null_mut()); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub fn set_state_callback<CB>(&self, _: CB, userdata: *mut c_void) |
| 98 |
where |
| 99 |
CB: Fn(&Context, *mut c_void), |
| 100 |
{ |
| 101 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 102 |
|
| 103 |
// See: A note about `wrapped` functions |
| 104 |
unsafe extern "C" fn wrapped<F>(c: *mut ffi::pa_context, userdata: *mut c_void) |
| 105 |
where |
| 106 |
F: Fn(&Context, *mut c_void), |
| 107 |
{ |
| 108 |
let ctx = context::from_raw_ptr(c); |
| 109 |
let cb = MaybeUninit::<F>::uninit(); |
| 110 |
let result = (*cb.as_ptr())(&ctx, userdata); |
| 111 |
forget(ctx); |
| 112 |
|
| 113 |
result |
| 114 |
} |
| 115 |
|
| 116 |
unsafe { |
| 117 |
ffi::pa_context_set_state_callback(self.raw_mut(), Some(wrapped::<CB>), userdata); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
pub fn errno(&self) -> ffi::pa_error_code_t { |
| 122 |
unsafe { ffi::pa_context_errno(self.raw_mut()) } |
| 123 |
} |
| 124 |
|
| 125 |
pub fn get_state(&self) -> ContextState { |
| 126 |
ContextState::try_from(unsafe { ffi::pa_context_get_state(self.raw_mut()) }) |
| 127 |
.expect("pa_context_get_state returned invalid ContextState") |
| 128 |
} |
| 129 |
|
| 130 |
pub fn connect<'a, OPT>( |
| 131 |
&self, |
| 132 |
server: OPT, |
| 133 |
flags: ContextFlags, |
| 134 |
api: *const ffi::pa_spawn_api, |
| 135 |
) -> Result<()> |
| 136 |
where |
| 137 |
OPT: Into<Option<&'a CStr>>, |
| 138 |
{ |
| 139 |
let r = unsafe { |
| 140 |
ffi::pa_context_connect( |
| 141 |
self.raw_mut(), |
| 142 |
server.into().unwrap_cstr(), |
| 143 |
flags.into(), |
| 144 |
api, |
| 145 |
) |
| 146 |
}; |
| 147 |
error_result!((), r) |
| 148 |
} |
| 149 |
|
| 150 |
pub fn disconnect(&self) { |
| 151 |
unsafe { |
| 152 |
ffi::pa_context_disconnect(self.raw_mut()); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
pub fn drain<CB>(&self, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 157 |
where |
| 158 |
CB: Fn(&Context, *mut c_void), |
| 159 |
{ |
| 160 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 161 |
|
| 162 |
// See: A note about `wrapped` functions |
| 163 |
unsafe extern "C" fn wrapped<F>(c: *mut ffi::pa_context, userdata: *mut c_void) |
| 164 |
where |
| 165 |
F: Fn(&Context, *mut c_void), |
| 166 |
{ |
| 167 |
let ctx = context::from_raw_ptr(c); |
| 168 |
let cb = MaybeUninit::<F>::uninit(); |
| 169 |
let result = (*cb.as_ptr())(&ctx, userdata); |
| 170 |
forget(ctx); |
| 171 |
|
| 172 |
result |
| 173 |
} |
| 174 |
|
| 175 |
op_or_err!( |
| 176 |
self, |
| 177 |
ffi::pa_context_drain(self.raw_mut(), Some(wrapped::<CB>), userdata) |
| 178 |
) |
| 179 |
} |
| 180 |
|
| 181 |
pub fn rttime_new<CB>( |
| 182 |
&self, |
| 183 |
usec: USec, |
| 184 |
_: CB, |
| 185 |
userdata: *mut c_void, |
| 186 |
) -> *mut ffi::pa_time_event |
| 187 |
where |
| 188 |
CB: Fn(&MainloopApi, *mut ffi::pa_time_event, &TimeVal, *mut c_void), |
| 189 |
{ |
| 190 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 191 |
|
| 192 |
// See: A note about `wrapped` functions |
| 193 |
unsafe extern "C" fn wrapped<F>( |
| 194 |
a: *mut ffi::pa_mainloop_api, |
| 195 |
e: *mut ffi::pa_time_event, |
| 196 |
tv: *const TimeVal, |
| 197 |
userdata: *mut c_void, |
| 198 |
) where |
| 199 |
F: Fn(&MainloopApi, *mut ffi::pa_time_event, &TimeVal, *mut c_void), |
| 200 |
{ |
| 201 |
let api = mainloop_api::from_raw_ptr(a); |
| 202 |
let timeval = &*tv; |
| 203 |
let cb = MaybeUninit::<F>::uninit(); |
| 204 |
let result = (*cb.as_ptr())(&api, e, timeval, userdata); |
| 205 |
forget(api); |
| 206 |
|
| 207 |
result |
| 208 |
} |
| 209 |
|
| 210 |
unsafe { ffi::pa_context_rttime_new(self.raw_mut(), usec, Some(wrapped::<CB>), userdata) } |
| 211 |
} |
| 212 |
|
| 213 |
pub fn get_server_info<CB>(&self, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 214 |
where |
| 215 |
CB: Fn(&Context, Option<&ServerInfo>, *mut c_void), |
| 216 |
{ |
| 217 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 218 |
|
| 219 |
// See: A note about `wrapped` functions |
| 220 |
unsafe extern "C" fn wrapped<F>( |
| 221 |
c: *mut ffi::pa_context, |
| 222 |
i: *const ffi::pa_server_info, |
| 223 |
userdata: *mut c_void, |
| 224 |
) where |
| 225 |
F: Fn(&Context, Option<&ServerInfo>, *mut c_void), |
| 226 |
{ |
| 227 |
let info = if i.is_null() { None } else { Some(&*i) }; |
| 228 |
let ctx = context::from_raw_ptr(c); |
| 229 |
let cb = MaybeUninit::<F>::uninit(); |
| 230 |
let result = (*cb.as_ptr())(&ctx, info, userdata); |
| 231 |
forget(ctx); |
| 232 |
|
| 233 |
result |
| 234 |
} |
| 235 |
|
| 236 |
op_or_err!( |
| 237 |
self, |
| 238 |
ffi::pa_context_get_server_info(self.raw_mut(), Some(wrapped::<CB>), userdata) |
| 239 |
) |
| 240 |
} |
| 241 |
|
| 242 |
pub fn get_sink_info_by_name<'str, CS, CB>( |
| 243 |
&self, |
| 244 |
name: CS, |
| 245 |
_: CB, |
| 246 |
userdata: *mut c_void, |
| 247 |
) -> Result<Operation> |
| 248 |
where |
| 249 |
CB: Fn(&Context, *const SinkInfo, i32, *mut c_void), |
| 250 |
CS: Into<Option<&'str CStr>>, |
| 251 |
{ |
| 252 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 253 |
|
| 254 |
// See: A note about `wrapped` functions |
| 255 |
unsafe extern "C" fn wrapped<F>( |
| 256 |
c: *mut ffi::pa_context, |
| 257 |
info: *const ffi::pa_sink_info, |
| 258 |
eol: c_int, |
| 259 |
userdata: *mut c_void, |
| 260 |
) where |
| 261 |
F: Fn(&Context, *const SinkInfo, i32, *mut c_void), |
| 262 |
{ |
| 263 |
let ctx = context::from_raw_ptr(c); |
| 264 |
let cb = MaybeUninit::<F>::uninit(); |
| 265 |
let result = (*cb.as_ptr())(&ctx, info, eol, userdata); |
| 266 |
forget(ctx); |
| 267 |
|
| 268 |
result |
| 269 |
} |
| 270 |
|
| 271 |
op_or_err!( |
| 272 |
self, |
| 273 |
ffi::pa_context_get_sink_info_by_name( |
| 274 |
self.raw_mut(), |
| 275 |
name.into().unwrap_cstr(), |
| 276 |
Some(wrapped::<CB>), |
| 277 |
userdata |
| 278 |
) |
| 279 |
) |
| 280 |
} |
| 281 |
|
| 282 |
pub fn get_sink_info_list<CB>(&self, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 283 |
where |
| 284 |
CB: Fn(&Context, *const SinkInfo, i32, *mut c_void), |
| 285 |
{ |
| 286 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 287 |
|
| 288 |
// See: A note about `wrapped` functions |
| 289 |
unsafe extern "C" fn wrapped<F>( |
| 290 |
c: *mut ffi::pa_context, |
| 291 |
info: *const ffi::pa_sink_info, |
| 292 |
eol: c_int, |
| 293 |
userdata: *mut c_void, |
| 294 |
) where |
| 295 |
F: Fn(&Context, *const SinkInfo, i32, *mut c_void), |
| 296 |
{ |
| 297 |
let ctx = context::from_raw_ptr(c); |
| 298 |
let cb = MaybeUninit::<F>::uninit(); |
| 299 |
let result = (*cb.as_ptr())(&ctx, info, eol, userdata); |
| 300 |
forget(ctx); |
| 301 |
|
| 302 |
result |
| 303 |
} |
| 304 |
|
| 305 |
op_or_err!( |
| 306 |
self, |
| 307 |
ffi::pa_context_get_sink_info_list(self.raw_mut(), Some(wrapped::<CB>), userdata) |
| 308 |
) |
| 309 |
} |
| 310 |
|
| 311 |
pub fn get_sink_input_info<CB>( |
| 312 |
&self, |
| 313 |
idx: u32, |
| 314 |
_: CB, |
| 315 |
userdata: *mut c_void, |
| 316 |
) -> Result<Operation> |
| 317 |
where |
| 318 |
CB: Fn(&Context, *const SinkInputInfo, i32, *mut c_void), |
| 319 |
{ |
| 320 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 321 |
|
| 322 |
// See: A note about `wrapped` functions |
| 323 |
unsafe extern "C" fn wrapped<F>( |
| 324 |
c: *mut ffi::pa_context, |
| 325 |
info: *const ffi::pa_sink_input_info, |
| 326 |
eol: c_int, |
| 327 |
userdata: *mut c_void, |
| 328 |
) where |
| 329 |
F: Fn(&Context, *const SinkInputInfo, i32, *mut c_void), |
| 330 |
{ |
| 331 |
let ctx = context::from_raw_ptr(c); |
| 332 |
let cb = MaybeUninit::<F>::uninit(); |
| 333 |
let result = (*cb.as_ptr())(&ctx, info, eol, userdata); |
| 334 |
forget(ctx); |
| 335 |
|
| 336 |
result |
| 337 |
} |
| 338 |
|
| 339 |
op_or_err!( |
| 340 |
self, |
| 341 |
ffi::pa_context_get_sink_input_info(self.raw_mut(), idx, Some(wrapped::<CB>), userdata) |
| 342 |
) |
| 343 |
} |
| 344 |
|
| 345 |
pub fn get_source_info_list<CB>(&self, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 346 |
where |
| 347 |
CB: Fn(&Context, *const SourceInfo, i32, *mut c_void), |
| 348 |
{ |
| 349 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 350 |
|
| 351 |
// See: A note about `wrapped` functions |
| 352 |
unsafe extern "C" fn wrapped<F>( |
| 353 |
c: *mut ffi::pa_context, |
| 354 |
info: *const ffi::pa_source_info, |
| 355 |
eol: c_int, |
| 356 |
userdata: *mut c_void, |
| 357 |
) where |
| 358 |
F: Fn(&Context, *const SourceInfo, i32, *mut c_void), |
| 359 |
{ |
| 360 |
let ctx = context::from_raw_ptr(c); |
| 361 |
let cb = MaybeUninit::<F>::uninit(); |
| 362 |
let result = (*cb.as_ptr())(&ctx, info, eol, userdata); |
| 363 |
forget(ctx); |
| 364 |
|
| 365 |
result |
| 366 |
} |
| 367 |
|
| 368 |
op_or_err!( |
| 369 |
self, |
| 370 |
ffi::pa_context_get_source_info_list(self.raw_mut(), Some(wrapped::<CB>), userdata) |
| 371 |
) |
| 372 |
} |
| 373 |
|
| 374 |
pub fn set_sink_input_volume<CB>( |
| 375 |
&self, |
| 376 |
idx: u32, |
| 377 |
volume: &CVolume, |
| 378 |
_: CB, |
| 379 |
userdata: *mut c_void, |
| 380 |
) -> Result<Operation> |
| 381 |
where |
| 382 |
CB: Fn(&Context, i32, *mut c_void), |
| 383 |
{ |
| 384 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 385 |
|
| 386 |
// See: A note about `wrapped` functions |
| 387 |
unsafe extern "C" fn wrapped<F>( |
| 388 |
c: *mut ffi::pa_context, |
| 389 |
success: c_int, |
| 390 |
userdata: *mut c_void, |
| 391 |
) where |
| 392 |
F: Fn(&Context, i32, *mut c_void), |
| 393 |
{ |
| 394 |
let ctx = context::from_raw_ptr(c); |
| 395 |
let cb = MaybeUninit::<F>::uninit(); |
| 396 |
let result = (*cb.as_ptr())(&ctx, success, userdata); |
| 397 |
forget(ctx); |
| 398 |
|
| 399 |
result |
| 400 |
} |
| 401 |
|
| 402 |
op_or_err!( |
| 403 |
self, |
| 404 |
ffi::pa_context_set_sink_input_volume( |
| 405 |
self.raw_mut(), |
| 406 |
idx, |
| 407 |
volume, |
| 408 |
Some(wrapped::<CB>), |
| 409 |
userdata |
| 410 |
) |
| 411 |
) |
| 412 |
} |
| 413 |
|
| 414 |
pub fn subscribe<CB>( |
| 415 |
&self, |
| 416 |
m: SubscriptionMask, |
| 417 |
_: CB, |
| 418 |
userdata: *mut c_void, |
| 419 |
) -> Result<Operation> |
| 420 |
where |
| 421 |
CB: Fn(&Context, i32, *mut c_void), |
| 422 |
{ |
| 423 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 424 |
|
| 425 |
// See: A note about `wrapped` functions |
| 426 |
unsafe extern "C" fn wrapped<F>( |
| 427 |
c: *mut ffi::pa_context, |
| 428 |
success: c_int, |
| 429 |
userdata: *mut c_void, |
| 430 |
) where |
| 431 |
F: Fn(&Context, i32, *mut c_void), |
| 432 |
{ |
| 433 |
let ctx = context::from_raw_ptr(c); |
| 434 |
let cb = MaybeUninit::<F>::uninit(); |
| 435 |
let result = (*cb.as_ptr())(&ctx, success, userdata); |
| 436 |
forget(ctx); |
| 437 |
|
| 438 |
result |
| 439 |
} |
| 440 |
|
| 441 |
op_or_err!( |
| 442 |
self, |
| 443 |
ffi::pa_context_subscribe(self.raw_mut(), m.into(), Some(wrapped::<CB>), userdata) |
| 444 |
) |
| 445 |
} |
| 446 |
|
| 447 |
pub fn clear_subscribe_callback(&self) { |
| 448 |
unsafe { |
| 449 |
ffi::pa_context_set_subscribe_callback(self.raw_mut(), None, ptr::null_mut()); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
pub fn set_subscribe_callback<CB>(&self, _: CB, userdata: *mut c_void) |
| 454 |
where |
| 455 |
CB: Fn(&Context, SubscriptionEvent, u32, *mut c_void), |
| 456 |
{ |
| 457 |
assert_eq!(::std::mem::size_of::<CB>(), 0); |
| 458 |
|
| 459 |
// See: A note about `wrapped` functions |
| 460 |
unsafe extern "C" fn wrapped<F>( |
| 461 |
c: *mut ffi::pa_context, |
| 462 |
t: ffi::pa_subscription_event_type_t, |
| 463 |
idx: u32, |
| 464 |
userdata: *mut c_void, |
| 465 |
) where |
| 466 |
F: Fn(&Context, SubscriptionEvent, u32, *mut c_void), |
| 467 |
{ |
| 468 |
let ctx = context::from_raw_ptr(c); |
| 469 |
let event = SubscriptionEvent::try_from(t) |
| 470 |
.expect("pa_context_subscribe_cb_t passed invalid pa_subscription_event_type_t"); |
| 471 |
let cb = MaybeUninit::<F>::uninit(); |
| 472 |
let result = (*cb.as_ptr())(&ctx, event, idx, userdata); |
| 473 |
forget(ctx); |
| 474 |
|
| 475 |
result |
| 476 |
} |
| 477 |
|
| 478 |
unsafe { |
| 479 |
ffi::pa_context_set_subscribe_callback(self.raw_mut(), Some(wrapped::<CB>), userdata); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
#[doc(hidden)] |
| 485 |
pub unsafe fn from_raw_ptr(ptr: *mut ffi::pa_context) -> Context { |
| 486 |
Context(ptr) |
| 487 |
} |