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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std;
use capi;
use std::os::raw::c_void;
use std::rc::Rc;
use libc::timeval;
use super::events::io::*;
use super::events::timer::*;
use super::events::deferred::*;
pub use capi::pa_mainloop_api as ApiInternal;
pub trait MainloopInternalType {}
pub trait MainloopInnerType {
type I: MainloopInternalType;
fn get_ptr(&self) -> *mut Self::I;
fn get_api(&self) -> &mut MainloopApi;
}
pub struct MainloopInner<T>
where T: MainloopInternalType
{
pub ptr: *mut T,
pub api: *mut MainloopApi,
pub dropfn: fn(&mut MainloopInner<T>),
}
impl<T> Drop for MainloopInner<T>
where T: MainloopInternalType
{
fn drop(&mut self) {
(self.dropfn)(self);
}
}
impl<T> MainloopInnerType for MainloopInner<T>
where T: MainloopInternalType
{
type I = T;
fn get_ptr(&self) -> *mut T {
self.ptr
}
fn get_api(&self) -> &mut MainloopApi {
assert!(!self.api.is_null());
unsafe { &mut *self.api }
}
}
pub trait Mainloop {
type MI: MainloopInnerType;
fn inner(&self) -> Rc<Self::MI>;
fn new_io_event(&self, fd: i32, events: IoEventFlagSet, cb: (IoEventCb, *mut c_void)
) -> Option<IoEvent<Self::MI>>
{
let fn_ptr = self.inner().get_api().io_new.unwrap();
let ptr = fn_ptr(self.inner().get_api(), fd, events, Some(cb.0), cb.1);
if ptr.is_null() {
return None;
}
Some(IoEvent::<Self::MI>::from_raw(ptr, self.inner().clone()))
}
fn new_timer_event(&self, tv: &timeval, cb: (TimeEventCb, *mut c_void)
) -> Option<TimeEvent<Self::MI>>
{
let fn_ptr = self.inner().get_api().time_new.unwrap();
let ptr = fn_ptr(self.inner().get_api(), tv, Some(cb.0), cb.1);
if ptr.is_null() {
return None;
}
Some(TimeEvent::<Self::MI>::from_raw(ptr, self.inner().clone()))
}
fn new_deferred_event(&self, cb: (DeferEventCb, *mut c_void)) -> Option<DeferEvent<Self::MI>> {
let fn_ptr = self.inner().get_api().defer_new.unwrap();
let ptr = fn_ptr(self.inner().get_api(), Some(cb.0), cb.1);
if ptr.is_null() {
return None;
}
Some(DeferEvent::<Self::MI>::from_raw(ptr, self.inner().clone()))
}
fn set_api_userdata(&self, userdata: *mut c_void) {
self.inner().get_api().userdata = userdata;
}
fn get_api_userdata(&self) -> *mut c_void {
self.inner().get_api().userdata
}
fn quit(&self, retval: i32) {
let fn_ptr = self.inner().get_api().quit.unwrap();
fn_ptr(self.inner().get_api(), retval);
}
}
#[repr(C)]
pub struct MainloopApi {
pub userdata: *mut c_void,
pub io_new: Option<extern "C" fn(a: *mut MainloopApi, fd: i32, events: IoEventFlagSet,
cb: Option<IoEventCb>, userdata: *mut c_void) -> *mut IoEventInternal>,
pub io_enable: Option<extern "C" fn(e: *mut IoEventInternal, events: IoEventFlagSet)>,
pub io_free: Option<extern "C" fn(e: *mut IoEventInternal)>,
pub io_set_destroy: Option<extern "C" fn(e: *mut IoEventInternal, cb: Option<IoEventDestroyCb>)>,
pub time_new: Option<extern "C" fn(a: *mut MainloopApi, tv: *const timeval,
cb: Option<TimeEventCb>, userdata: *mut c_void) -> *mut TimeEventInternal>,
pub time_restart: Option<extern "C" fn(e: *mut TimeEventInternal, tv: *const timeval)>,
pub time_free: Option<extern "C" fn(e: *mut TimeEventInternal)>,
pub time_set_destroy: Option<extern "C" fn(e: *mut TimeEventInternal,
cb: Option<TimeEventDestroyCb>)>,
pub defer_new: Option<extern "C" fn(a: *mut MainloopApi, cb: Option<DeferEventCb>,
userdata: *mut c_void) -> *mut DeferEventInternal>,
pub defer_enable: Option<extern "C" fn(e: *mut DeferEventInternal, b: i32)>,
pub defer_free: Option<extern "C" fn(e: *mut DeferEventInternal)>,
pub defer_set_destroy: Option<extern "C" fn(e: *mut DeferEventInternal,
cb: Option<DeferEventDestroyCb>)>,
pub quit: Option<extern "C" fn(a: *mut MainloopApi, retval: i32)>,
}
pub type MainloopApiOnceCallback = extern "C" fn(m: *mut ApiInternal,
userdata: *mut c_void);
impl MainloopApi {
pub fn mainloop_api_once(&mut self, cb: (MainloopApiOnceCallback, *mut c_void)) {
unsafe { capi::pa_mainloop_api_once(std::mem::transmute(self), Some(cb.0), cb.1) };
}
}