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
//! Main loop IO events.

// This file is part of the PulseAudio Rust language binding.
//
// Copyright (c) 2017 Lyndon Brown
//
// This library is free software; you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation; either version
// 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with this library;
// if not, see <http://www.gnu.org/licenses/>.

use capi;
use std::os::raw::c_void;
use std::rc::Rc;
use super::super::api::{MainloopApi, MainloopInnerType};

pub use capi::pa_io_event as IoEventInternal;

/// A bitmask for IO events
pub type IoEventFlagSet = capi::mainloop::pa_io_event_flags_t;

pub mod flags {
    use capi;
    use super::IoEventFlagSet;

    /// No event
    pub const NULL: IoEventFlagSet = capi::PA_IO_EVENT_NULL;
    /// Input event
    pub const INPUT: IoEventFlagSet = capi::PA_IO_EVENT_INPUT;
    /// Output event
    pub const OUTPUT: IoEventFlagSet = capi::PA_IO_EVENT_OUTPUT;
    /// Hangup event
    pub const HANGUP: IoEventFlagSet = capi::PA_IO_EVENT_HANGUP;
    /// Error event
    pub const ERROR: IoEventFlagSet = capi::PA_IO_EVENT_ERROR;
}

/// An IO event source
pub struct IoEvent<T>
    where T: MainloopInnerType
{
    ptr: *mut IoEventInternal,
    /// Source mainloop
    owner: Rc<T>,
    /// Saved callback closure, for later destruction
    _saved_cb: EventCb,
}

/// A reference to an IO event source, provided to the callback, allowing modification within the
/// callback itself
pub struct IoEventRef<T: 'static>
    where T: MainloopInnerType
{
    ptr: *mut IoEventInternal,
    /// Source mainloop
    owner: Rc<T>,
}

pub(crate) type EventCb =
    ::callbacks::MultiUseCallback<FnMut(*mut IoEventInternal, i32, IoEventFlagSet),
        extern "C" fn(a: *const MainloopApi, e: *mut IoEventInternal, fd: i32,
        events: IoEventFlagSet, userdata: *mut c_void)>;

impl<T> IoEvent<T>
    where T: MainloopInnerType
{
    pub(crate) fn from_raw(ptr: *mut IoEventInternal, mainloop_inner: Rc<T>, callback: EventCb
        ) -> Self
    {
        assert_eq!(false, ptr.is_null());
        Self { ptr: ptr, owner: mainloop_inner, _saved_cb: callback }
    }

    /// Enable or disable IO events on this object.
    pub fn enable(&mut self, events: IoEventFlagSet) {
        let fn_ptr = (*self.owner).get_api().io_enable.unwrap();
        fn_ptr(self.ptr, events);
    }
}

impl<T> IoEventRef<T>
    where T: MainloopInnerType
{
    pub(crate) fn from_raw(ptr: *mut IoEventInternal, mainloop_inner: Rc<T>) -> Self {
        assert_eq!(false, ptr.is_null());
        Self { ptr: ptr, owner: mainloop_inner }
    }

    /// Enable or disable IO events on this object.
    pub fn enable(&mut self, events: IoEventFlagSet) {
        let fn_ptr = (*self.owner).get_api().io_enable.unwrap();
        fn_ptr(self.ptr, events);
    }
}

impl<T> Drop for IoEvent<T>
    where T: MainloopInnerType
{
    fn drop(&mut self) {
        let fn_ptr = (*self.owner).get_api().io_free.unwrap();
        fn_ptr(self.ptr);
    }
}

/// Proxy for the event callback.
/// Warning: This is for multi-use cases! It does **not** destroy the actual closure callback, which
/// must be accomplished separately to avoid a memory leak.
pub(crate)
extern "C"
fn event_cb_proxy(_: *const MainloopApi, e: *mut IoEventInternal, fd: i32, events: IoEventFlagSet,
    userdata: *mut c_void)
{
    let callback = EventCb::get_callback(userdata);
    callback(e, fd, events);
}