summaryrefslogtreecommitdiffstats
path: root/third_party/rust/pulse/src/mainloop_api.rs
blob: 4b2ad426c358920c2e0fd5c52cbcc1abe06b4514 (plain)
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
// Copyright © 2017 Mozilla Foundation
//
// This program is made available under an ISC-style license.  See the
// accompanying file LICENSE for details.

use ffi;
use std::mem;
use std::os::raw::c_void;

#[allow(non_camel_case_types)]
type pa_once_cb_t =
    Option<unsafe extern "C" fn(m: *mut ffi::pa_mainloop_api, userdata: *mut c_void)>;
fn wrap_once_cb<F>(_: F) -> pa_once_cb_t
where
    F: Fn(&MainloopApi, *mut c_void),
{
    assert!(mem::size_of::<F>() == 0);

    unsafe extern "C" fn wrapped<F>(m: *mut ffi::pa_mainloop_api, userdata: *mut c_void)
    where
        F: Fn(&MainloopApi, *mut c_void),
    {
        let api = from_raw_ptr(m);
        let result = mem::transmute::<_, &F>(&())(&api, userdata);
        mem::forget(api);
        result
    }

    Some(wrapped::<F>)
}

pub struct MainloopApi(*mut ffi::pa_mainloop_api);

impl MainloopApi {
    pub fn raw_mut(&self) -> &mut ffi::pa_mainloop_api {
        unsafe { &mut *self.0 }
    }

    pub fn once<CB>(&self, cb: CB, userdata: *mut c_void)
    where
        CB: Fn(&MainloopApi, *mut c_void),
    {
        let wrapped = wrap_once_cb(cb);
        unsafe {
            ffi::pa_mainloop_api_once(self.raw_mut(), wrapped, userdata);
        }
    }

    pub fn time_free(&self, e: *mut ffi::pa_time_event) {
        unsafe {
            if let Some(f) = self.raw_mut().time_free {
                f(e);
            }
        }
    }
}

pub unsafe fn from_raw_ptr(raw: *mut ffi::pa_mainloop_api) -> MainloopApi {
    MainloopApi(raw)
}