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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::os::raw::c_void;
use bitflags::bitflags;
use num_derive::{FromPrimitive, ToPrimitive};
use super::{ContextInternal, Context};
use crate::operation;
use crate::callbacks::{box_closure_get_capi_ptr, MultiUseCallback};
pub use capi::context::subscribe::pa_subscription_event_type_t as EventType;
#[deprecated(since = "2.20.0", note="use the associated constant on `Facility` instead")]
pub const FACILITY_MASK: u32 = capi::PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
#[deprecated(since = "2.20.0", note="use the associated constant on `Operation` instead")]
pub const OPERATION_MASK: u32 = capi::PA_SUBSCRIPTION_EVENT_TYPE_MASK;
bitflags! {
#[repr(transparent)]
pub struct InterestMaskSet: u32 {
const NULL = capi::PA_SUBSCRIPTION_MASK_NULL;
const SINK = capi::PA_SUBSCRIPTION_MASK_SINK;
const SOURCE = capi::PA_SUBSCRIPTION_MASK_SOURCE;
const SINK_INPUT = capi::PA_SUBSCRIPTION_MASK_SINK_INPUT;
const SOURCE_OUTPUT = capi::PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT;
const MODULE = capi::PA_SUBSCRIPTION_MASK_MODULE;
const CLIENT = capi::PA_SUBSCRIPTION_MASK_CLIENT;
const SAMPLE_CACHE = capi::PA_SUBSCRIPTION_MASK_SAMPLE_CACHE;
const SERVER = capi::PA_SUBSCRIPTION_MASK_SERVER;
const CARD = capi::PA_SUBSCRIPTION_MASK_CARD;
const ALL = capi::PA_SUBSCRIPTION_MASK_ALL;
}
}
#[deprecated(since = "2.20.0", note = "Use the associated constants on `InterestMaskSet`.")]
pub mod subscription_masks {
use super::InterestMaskSet;
pub const NULL: InterestMaskSet = InterestMaskSet::NULL;
pub const SINK: InterestMaskSet = InterestMaskSet::SINK;
pub const SOURCE: InterestMaskSet = InterestMaskSet::SOURCE;
pub const SINK_INPUT: InterestMaskSet = InterestMaskSet::SINK_INPUT;
pub const SOURCE_OUTPUT: InterestMaskSet = InterestMaskSet::SOURCE_OUTPUT;
pub const MODULE: InterestMaskSet = InterestMaskSet::MODULE;
pub const CLIENT: InterestMaskSet = InterestMaskSet::CLIENT;
pub const SAMPLE_CACHE: InterestMaskSet = InterestMaskSet::SAMPLE_CACHE;
pub const SERVER: InterestMaskSet = InterestMaskSet::SERVER;
pub const MASK_CARD: InterestMaskSet = InterestMaskSet::CARD;
pub const ALL: InterestMaskSet = InterestMaskSet::ALL;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(FromPrimitive, ToPrimitive)]
pub enum Facility {
Sink = 0,
Source = 1,
SinkInput = 2,
SourceOutput = 3,
Module = 4,
Client = 5,
SampleCache = 6,
Server = 7,
Card = 9,
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(FromPrimitive, ToPrimitive)]
pub enum Operation {
New = 0,
Changed = 0x10,
Removed = 0x20,
}
impl Facility {
pub const MASK: EventType = capi::PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
fn from_event(value: EventType) -> Option<Facility> {
match value & Self::MASK {
0 => Some(Facility::Sink),
1 => Some(Facility::Source),
2 => Some(Facility::SinkInput),
3 => Some(Facility::SourceOutput),
4 => Some(Facility::Module),
5 => Some(Facility::Client),
6 => Some(Facility::SampleCache),
7 => Some(Facility::Server),
9 => Some(Facility::Card),
_ => None,
}
}
#[inline(always)]
pub const fn to_interest_mask(self) -> InterestMaskSet {
InterestMaskSet::from_bits_truncate(1u32 << (self as u32))
}
}
impl Operation {
pub const MASK: EventType = capi::PA_SUBSCRIPTION_EVENT_TYPE_MASK;
fn from_event(value: EventType) -> Option<Operation> {
match value & Self::MASK {
0 => Some(Operation::New),
0x10 => Some(Operation::Changed),
0x20 => Some(Operation::Removed),
_ => None,
}
}
}
pub(super) type Callback = MultiUseCallback<dyn FnMut(Option<Facility>, Option<Operation>, u32),
extern "C" fn(*mut ContextInternal, EventType, u32, *mut c_void)>;
impl Context {
pub fn subscribe<F>(&mut self, mask: InterestMaskSet, callback: F)
-> operation::Operation<dyn FnMut(bool)>
where F: FnMut(bool) + 'static
{
let cb_data = box_closure_get_capi_ptr::<dyn FnMut(bool)>(Box::new(callback));
let ptr = unsafe { capi::pa_context_subscribe(self.ptr, mask.bits(),
Some(super::success_cb_proxy), cb_data) };
operation::Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
}
pub fn set_subscribe_callback(&mut self,
callback: Option<Box<dyn FnMut(Option<Facility>, Option<Operation>, u32) + 'static>>)
{
let saved = &mut self.cb_ptrs.subscribe;
*saved = Callback::new(callback);
let (cb_fn, cb_data) = saved.get_capi_params(cb_proxy);
unsafe { capi::pa_context_set_subscribe_callback(self.ptr, cb_fn, cb_data); }
}
}
extern "C"
fn cb_proxy(_: *mut ContextInternal, et: EventType, index: u32, userdata: *mut c_void) {
let _ = std::panic::catch_unwind(|| {
let facility = Facility::from_event(et);
let operation = Operation::from_event(et);
let callback = Callback::get_callback(userdata);
(callback)(facility, operation, index);
});
}