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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use atom::*;
use std::mem::size_of;
use libc::{memcmp, memcpy, c_void};
pub fn lv2_atom_pad_size(size: u32) -> u32 {
(size + 7) & (!7)
}
pub fn lv2_atom_total_size(atom: &LV2Atom) -> u32 {
size_of::<LV2Atom>() as u32 + atom.size
}
pub unsafe fn lv2_atom_is_null(atom: *const LV2Atom) -> bool {
atom.is_null() || ((*atom).mytype == 0 && (*atom).size == 0)
}
pub unsafe fn lv2_atom_equals(a: *const LV2Atom, b: *const LV2Atom) -> bool {
(a == b) ||
(((*a).mytype == (*b).mytype) && ((*a).size == (*b).size) &&
(memcmp(a.offset(1) as *const c_void,
b.offset(1) as *const c_void,
(*a).size as usize) == 0))
}
pub unsafe fn lv2_atom_sequence_begin(body: *const LV2AtomSequenceBody) -> *mut LV2AtomEvent {
body.offset(1) as *mut LV2AtomEvent
}
pub unsafe fn lv2_atom_sequence_end(body: *const LV2AtomSequenceBody,
size: u32)
-> *const LV2AtomEvent {
(body as *const u8).offset(lv2_atom_pad_size(size) as isize) as *const LV2AtomEvent
}
pub unsafe fn lv2_atom_sequence_is_end(body: *const LV2AtomSequenceBody,
size: u32,
i: *const LV2AtomEvent)
-> bool {
let result = i as *const u8 >= (body as *const u8).offset(size as isize);
result
}
pub unsafe fn lv2_atom_sequence_next(i: *const LV2AtomEvent) -> *mut LV2AtomEvent {
let off = size_of::<LV2AtomEvent>() + lv2_atom_pad_size((*i).body.size) as usize;
let ptr = (i as *const u8).offset(off as isize);
ptr as *mut LV2AtomEvent
}
pub unsafe fn lv2_atom_sequence_clear(seq: *mut LV2AtomSequence) -> () {
(*seq).atom.size = size_of::<LV2AtomSequenceBody>() as u32;
}
pub unsafe fn lv2_atom_sequence_append_event(seq: *mut LV2AtomSequence,
capacity: u32,
event: *const LV2AtomEvent)
-> *const LV2AtomEvent {
let total_size = size_of::<LV2AtomEvent>() as u32 + (*event).body.size;
if (capacity - (*seq).atom.size) < total_size {
return 0 as *const LV2AtomEvent;
}
let e = lv2_atom_sequence_end(&(*seq).body, (*seq).atom.size);
memcpy(e as *mut c_void,
event as *const c_void,
total_size as usize);
(*seq).atom.size += lv2_atom_pad_size(total_size);
e
}
pub unsafe fn lv2_atom_object_begin(body: *const LV2AtomObjectBody) -> *mut LV2AtomPropertyBody {
body.offset(1) as *mut LV2AtomPropertyBody
}
pub unsafe fn lv2_atom_object_is_end(body: *const LV2AtomObjectBody,
size: u32,
i: *const LV2AtomPropertyBody)
-> bool {
i as *const u8 >= (body as *const u8).offset(size as isize)
}
pub unsafe fn lv2_atom_object_next(i: *const LV2AtomPropertyBody) -> *mut LV2AtomPropertyBody {
let value = (i as *const u8).offset((2 * size_of::<u32>()) as isize) as *const LV2Atom;
let offset = lv2_atom_pad_size(size_of::<LV2AtomPropertyBody>() as u32 + (*value).size);
(i as *mut u8).offset(offset as isize) as *mut LV2AtomPropertyBody
}
pub struct LV2AtomObjectQuery {
pub key: u32,
pub value: *mut *mut LV2Atom,
}
pub unsafe fn lv2_atom_object_query(obj: *mut LV2AtomObject,
query: *mut LV2AtomObjectQuery)
-> i32 {
let ref mut object = *obj;
let mut n_queries = 0;
let mut matches = 0;
let q = query;
while (*q).key != 0 {
n_queries += 1;
q.offset(1);
}
{
let f = |prop: *mut LV2AtomPropertyBody| -> bool {
let mut q = query;
while (*q).key != 0 {
if ((*q).key == (*prop).key) && (!(*q).value.is_null()) {
let ref mut val = (*prop).value;
*(*q).value = val;
matches += 1;
if matches == n_queries {
return true;
}
break;
}
q.offset(1);
}
false
};
object.foreach(f);
}
return matches;
}
pub struct ObjectHelper {
pub key: u32,
pub atom: *mut *mut LV2Atom,
}
pub unsafe fn lv2_atom_object_get(body: *mut LV2AtomObject, query: &[ObjectHelper]) -> i32 {
let mut matches = 0;
let mut n_queries = 0;
for it in query {
if it.atom.is_null() {
return -1;
}
n_queries += 1;
}
{
let f = |prop: *mut LV2AtomPropertyBody| -> bool {
for it in query {
let qkey = it.key;
if qkey == (*prop).key && (*(it.atom)).is_null() {
*(it.atom) = &mut (*prop).value;
matches += 1;
if matches == n_queries {
return matches > 0;
}
break;
}
}
return true;
};
(*body).foreach(f);
}
return matches;
}
impl LV2AtomSequenceBody {
pub unsafe fn foreach<F>(&mut self, size: u32, mut closure: F) -> ()
where F: FnMut(*const LV2AtomEvent) -> ()
{
let mut it = lv2_atom_sequence_begin(self);
while !lv2_atom_sequence_is_end(self, size, it) {
closure(it);
it = lv2_atom_sequence_next(it);
}
}
}
pub struct LV2AtomSequenceIterator<'a> {
pub seq: &'a LV2AtomSequence,
pub current: &'a LV2AtomEvent,
}
impl<'a> Iterator for LV2AtomSequenceIterator<'a> {
type Item = &'a LV2AtomEvent;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let body = &self.seq.body;
let size = self.seq.atom.size;
if !lv2_atom_sequence_is_end(body, size, self.current) {
let out = self.current;
self.current = &*lv2_atom_sequence_next(self.current);
Some(out)
} else {
None
}
}
}
}
impl<'a> IntoIterator for &'a LV2AtomSequence {
type Item = &'a LV2AtomEvent;
type IntoIter = LV2AtomSequenceIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
unsafe {
LV2AtomSequenceIterator {
seq: self,
current: &*lv2_atom_sequence_begin(&self.body as *const LV2AtomSequenceBody),
}
}
}
}