libpulse_binding/mainloop/events/
io.rs

1// Copyright 2017 Lyndon Brown
2//
3// This file is part of the PulseAudio Rust language binding.
4//
5// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not
6// copy, modify, or distribute this file except in compliance with said license. You can find copies
7// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at
8// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0>
9// respectively.
10//
11// Portions of documentation are copied from the LGPL 2.1+ licensed PulseAudio C headers on a
12// fair-use basis, as discussed in the overall project readme (available in the git repository).
13
14//! Main loop IO events.
15
16use std::os::raw::c_void;
17use std::rc::Rc;
18use bitflags::bitflags;
19use crate::mainloop::api::{MainloopApi, MainloopInnerType};
20use crate::callbacks::MultiUseCallback;
21
22pub use capi::pa_io_event as IoEventInternal;
23
24bitflags! {
25    /// IO event flag set.
26    #[repr(transparent)]
27    pub struct FlagSet: u32 {
28        /// No event.
29        const NULL = capi::PA_IO_EVENT_NULL;
30        /// Input event.
31        const INPUT = capi::PA_IO_EVENT_INPUT;
32        /// Output event.
33        const OUTPUT = capi::PA_IO_EVENT_OUTPUT;
34        /// Hangup event.
35        const HANGUP = capi::PA_IO_EVENT_HANGUP;
36        /// Error event.
37        const ERROR = capi::PA_IO_EVENT_ERROR;
38    }
39}
40
41/// An IO event source
42pub struct IoEvent<T>
43    where T: MainloopInnerType
44{
45    /// Internal object pointer
46    ptr: *mut IoEventInternal,
47    /// Source mainloop.
48    owner: Rc<T>,
49    /// Saved callback closure, for later destruction.
50    _saved_cb: EventCb,
51}
52
53/// A reference to an IO event source, provided to the callback, allowing modification within the
54/// callback itself.
55pub struct IoEventRef<T: 'static>
56    where T: MainloopInnerType
57{
58    /// Internal object pointer
59    ptr: *mut IoEventInternal,
60    /// Source mainloop.
61    owner: Rc<T>,
62}
63
64pub(crate) type EventCb = MultiUseCallback<dyn FnMut(*mut IoEventInternal, i32, FlagSet),
65    extern "C" fn(a: *const MainloopApi, e: *mut IoEventInternal, fd: i32, events: FlagSet,
66    userdata: *mut c_void)>;
67
68impl<T> IoEvent<T>
69    where T: MainloopInnerType
70{
71    #[inline]
72    pub(crate) fn from_raw(ptr: *mut IoEventInternal, mainloop_inner: Rc<T>, callback: EventCb)
73        -> Self
74    {
75        assert_eq!(false, ptr.is_null());
76        Self { ptr: ptr, owner: mainloop_inner, _saved_cb: callback }
77    }
78
79    /// Enables or disables IO events on this object.
80    #[inline]
81    pub fn enable(&mut self, events: FlagSet) {
82        let fn_ptr = (*self.owner).get_api().io_enable.unwrap();
83        fn_ptr(self.ptr, events);
84    }
85}
86
87impl<T> IoEventRef<T>
88    where T: MainloopInnerType
89{
90    #[inline]
91    pub(crate) fn from_raw(ptr: *mut IoEventInternal, mainloop_inner: Rc<T>) -> Self {
92        assert_eq!(false, ptr.is_null());
93        Self { ptr: ptr, owner: mainloop_inner }
94    }
95
96    /// Enables or disables IO events on this object.
97    #[inline]
98    pub fn enable(&mut self, events: FlagSet) {
99        let fn_ptr = (*self.owner).get_api().io_enable.unwrap();
100        fn_ptr(self.ptr, events);
101    }
102}
103
104impl<T> Drop for IoEvent<T>
105    where T: MainloopInnerType
106{
107    fn drop(&mut self) {
108        let fn_ptr = (*self.owner).get_api().io_free.unwrap();
109        fn_ptr(self.ptr);
110    }
111}
112
113/// Proxy for the event callback.
114///
115/// Warning: This is for multi-use cases! It does **not** destroy the actual closure callback, which
116/// must be accomplished separately to avoid a memory leak.
117pub(crate)
118extern "C"
119fn event_cb_proxy(_: *const MainloopApi, e: *mut IoEventInternal, fd: i32, events: FlagSet,
120    userdata: *mut c_void)
121{
122    let _ = std::panic::catch_unwind(|| {
123        let callback = EventCb::get_callback(userdata);
124        (callback)(e, fd, events);
125    });
126}