summaryrefslogtreecommitdiffstats
path: root/dom/midi/midir_impl/src/lib.rs
blob: 38cc3e003395b80db8d7176bc0a5a795e38bb6aa (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
extern crate thin_vec;

use midir::{
    InitError, MidiInput, MidiInputConnection, MidiInputPort, MidiOutput, MidiOutputConnection,
    MidiOutputPort,
};
use nsstring::{nsAString, nsString};
use std::ptr;
use thin_vec::ThinVec;
use uuid::Uuid;

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate midir;

#[cfg(target_os = "windows")]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct GeckoTimeStamp {
    gtc: u64,
    qpc: u64,

    is_null: u8,
    has_qpc: u8,
}

#[cfg(not(target_os = "windows"))]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct GeckoTimeStamp {
    value: u64,
}

enum MidiConnection {
    Input(MidiInputConnection<CallbackData>),
    Output(MidiOutputConnection),
}

struct MidiConnectionWrapper {
    id: String,
    connection: MidiConnection,
}

enum MidiPort {
    Input(MidiInputPort),
    Output(MidiOutputPort),
}

struct MidiPortWrapper {
    id: String,
    name: String,
    port: MidiPort,
    open_count: u32,
}

impl MidiPortWrapper {
    fn input(self: &MidiPortWrapper) -> bool {
        match self.port {
            MidiPort::Input(_) => true,
            MidiPort::Output(_) => false,
        }
    }
}

pub struct MidirWrapper {
    ports: Vec<MidiPortWrapper>,
    connections: Vec<MidiConnectionWrapper>,
}

struct CallbackData {
    nsid: nsString,
    open_timestamp: GeckoTimeStamp,
}

type AddCallback = unsafe extern "C" fn(id: &nsString, name: &nsString, input: bool);
type RemoveCallback = AddCallback;

impl MidirWrapper {
    fn refresh(
        self: &mut MidirWrapper,
        add_callback: AddCallback,
        remove_callback: Option<RemoveCallback>,
    ) {
        if let Ok(ports) = collect_ports() {
            if let Some(remove_callback) = remove_callback {
                self.remove_missing_ports(&ports, remove_callback);
            }

            self.add_new_ports(ports, add_callback);
        }
    }

    fn remove_missing_ports(
        self: &mut MidirWrapper,
        ports: &Vec<MidiPortWrapper>,
        remove_callback: RemoveCallback,
    ) {
        let old_ports = &mut self.ports;
        let mut i = 0;
        while i < old_ports.len() {
            if !ports
                .iter()
                .any(|p| p.name == old_ports[i].name && p.input() == old_ports[i].input())
            {
                let port = old_ports.remove(i);
                let id = nsString::from(&port.id);
                let name = nsString::from(&port.name);
                unsafe { remove_callback(&id, &name, port.input()) };
            } else {
                i += 1;
            }
        }
    }

    fn add_new_ports(
        self: &mut MidirWrapper,
        ports: Vec<MidiPortWrapper>,
        add_callback: AddCallback,
    ) {
        for port in ports {
            if !self.is_port_present(&port) && !Self::is_microsoft_synth_output(&port) {
                let id = nsString::from(&port.id);
                let name = nsString::from(&port.name);
                unsafe { add_callback(&id, &name, port.input()) };
                self.ports.push(port);
            }
        }
    }

    fn is_port_present(self: &MidirWrapper, port: &MidiPortWrapper) -> bool {
        self.ports
            .iter()
            .any(|p| p.name == port.name && p.input() == port.input())
    }

    // We explicitly disable Microsoft's soft synthesizer, see bug 1798097
    fn is_microsoft_synth_output(port: &MidiPortWrapper) -> bool {
        (port.input() == false) && (port.name == "Microsoft GS Wavetable Synth")
    }

    fn open_port(
        self: &mut MidirWrapper,
        nsid: &nsString,
        timestamp: GeckoTimeStamp,
        callback: unsafe extern "C" fn(
            id: &nsString,
            data: *const u8,
            length: usize,
            timestamp: &GeckoTimeStamp,
            micros: u64,
        ),
    ) -> Result<(), ()> {
        let id = nsid.to_string();
        let connections = &mut self.connections;
        let port = self.ports.iter_mut().find(|e| e.id.eq(&id));
        if let Some(port) = port {
            if port.open_count == 0 {
                let connection = match &port.port {
                    MidiPort::Input(port) => {
                        let input = MidiInput::new("WebMIDI input").map_err(|_err| ())?;
                        let data = CallbackData {
                            nsid: nsid.clone(),
                            open_timestamp: timestamp,
                        };
                        let connection = input
                            .connect(
                                port,
                                "Input connection",
                                move |stamp, message, data| unsafe {
                                    callback(
                                        &data.nsid,
                                        message.as_ptr(),
                                        message.len(),
                                        &data.open_timestamp,
                                        stamp,
                                    );
                                },
                                data,
                            )
                            .map_err(|_err| ())?;
                        MidiConnectionWrapper {
                            id: id.clone(),
                            connection: MidiConnection::Input(connection),
                        }
                    }
                    MidiPort::Output(port) => {
                        let output = MidiOutput::new("WebMIDI output").map_err(|_err| ())?;
                        let connection = output
                            .connect(port, "Output connection")
                            .map_err(|_err| ())?;
                        MidiConnectionWrapper {
                            connection: MidiConnection::Output(connection),
                            id: id.clone(),
                        }
                    }
                };

                connections.push(connection);
            }

            port.open_count += 1;
            return Ok(());
        }

        Err(())
    }

    fn close_port(self: &mut MidirWrapper, id: &str) {
        let port = self.ports.iter_mut().find(|e| e.id.eq(&id)).unwrap();
        port.open_count -= 1;

        if port.open_count > 0 {
            return;
        }

        let connections = &mut self.connections;
        let index = connections.iter().position(|e| e.id.eq(id)).unwrap();
        let connection_wrapper = connections.remove(index);

        match connection_wrapper.connection {
            MidiConnection::Input(connection) => {
                connection.close();
            }
            MidiConnection::Output(connection) => {
                connection.close();
            }
        }
    }

    fn send(self: &mut MidirWrapper, id: &str, data: &[u8]) -> Result<(), ()> {
        let connections = &mut self.connections;
        let index = connections.iter().position(|e| e.id.eq(id)).ok_or(())?;
        let connection_wrapper = connections.get_mut(index).unwrap();

        match &mut connection_wrapper.connection {
            MidiConnection::Output(connection) => {
                connection.send(data).map_err(|_err| ())?;
            }
            _ => {
                panic!("Sending on an input port!");
            }
        }

        Ok(())
    }
}

fn collect_ports() -> Result<Vec<MidiPortWrapper>, InitError> {
    let input = MidiInput::new("WebMIDI input")?;
    let output = MidiOutput::new("WebMIDI output")?;
    let mut ports = Vec::<MidiPortWrapper>::new();
    collect_input_ports(&input, &mut ports);
    collect_output_ports(&output, &mut ports);
    Ok(ports)
}

impl MidirWrapper {
    fn new() -> Result<MidirWrapper, InitError> {
        let ports = Vec::new();
        let connections: Vec<MidiConnectionWrapper> = Vec::new();
        Ok(MidirWrapper { ports, connections })
    }
}

/// Create the C++ wrapper that will be used to talk with midir.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// This function deliberately leaks the wrapper because ownership is
/// transfered to the C++ code. Use [midir_impl_shutdown()] to free it.
#[no_mangle]
pub unsafe extern "C" fn midir_impl_init(callback: AddCallback) -> *mut MidirWrapper {
    if let Ok(mut midir_impl) = MidirWrapper::new() {
        midir_impl.refresh(callback, None);

        // Gecko invokes this initialization on a separate thread from all the
        // other operations, so make it clear to Rust this needs to be Send.
        fn assert_send<T: Send>(_: &T) {}
        assert_send(&midir_impl);

        let midir_box = Box::new(midir_impl);
        // Leak the object as it will be owned by the C++ code from now on
        Box::leak(midir_box) as *mut _
    } else {
        ptr::null_mut()
    }
}

/// Refresh the list of ports.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// `wrapper` must be the pointer returned by [midir_impl_init()].
#[no_mangle]
pub unsafe extern "C" fn midir_impl_refresh(
    wrapper: *mut MidirWrapper,
    add_callback: AddCallback,
    remove_callback: RemoveCallback,
) {
    (*wrapper).refresh(add_callback, Some(remove_callback))
}

/// Shutdown midir and free the C++ wrapper.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// `wrapper` must be the pointer returned by [midir_impl_init()]. After this
/// has been called the wrapper object will be destoyed and cannot be accessed
/// anymore.
#[no_mangle]
pub unsafe extern "C" fn midir_impl_shutdown(wrapper: *mut MidirWrapper) {
    // The MidirImpl object will be automatically destroyed when the contents
    // of this box are automatically dropped at the end of the function
    let _midir_box = Box::from_raw(wrapper);
}

/// Open a MIDI port.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// `wrapper` must be the pointer returned by [midir_impl_init()].
#[no_mangle]
pub unsafe extern "C" fn midir_impl_open_port(
    wrapper: *mut MidirWrapper,
    nsid: *mut nsString,
    timestamp: *mut GeckoTimeStamp,
    callback: unsafe extern "C" fn(
        id: &nsString,
        data: *const u8,
        length: usize,
        timestamp: &GeckoTimeStamp,
        micros: u64,
    ),
) -> bool {
    (*wrapper)
        .open_port(nsid.as_ref().unwrap(), *timestamp, callback)
        .is_ok()
}

/// Close a MIDI port.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// `wrapper` must be the pointer returned by [midir_impl_init()].
#[no_mangle]
pub unsafe extern "C" fn midir_impl_close_port(wrapper: *mut MidirWrapper, id: *mut nsString) {
    (*wrapper).close_port(&(*id).to_string());
}

/// Send a message over a MIDI output port.
///
/// This function will be exposed to C++
///
/// # Safety
///
/// `wrapper` must be the pointer returned by [midir_impl_init()].
#[no_mangle]
pub unsafe extern "C" fn midir_impl_send(
    wrapper: *mut MidirWrapper,
    id: *const nsAString,
    data: *const ThinVec<u8>,
) -> bool {
    (*wrapper)
        .send(&(*id).to_string(), (*data).as_slice())
        .is_ok()
}

fn collect_input_ports(input: &MidiInput, wrappers: &mut Vec<MidiPortWrapper>) {
    let ports = input.ports();
    for port in ports {
        let id = Uuid::new_v4()
            .as_hyphenated()
            .encode_lower(&mut Uuid::encode_buffer())
            .to_owned();
        let name = input
            .port_name(&port)
            .unwrap_or_else(|_| "unknown input port".to_string());
        let port = MidiPortWrapper {
            id,
            name,
            port: MidiPort::Input(port),
            open_count: 0,
        };
        wrappers.push(port);
    }
}

fn collect_output_ports(output: &MidiOutput, wrappers: &mut Vec<MidiPortWrapper>) {
    let ports = output.ports();
    for port in ports {
        let id = Uuid::new_v4()
            .as_hyphenated()
            .encode_lower(&mut Uuid::encode_buffer())
            .to_owned();
        let name = output
            .port_name(&port)
            .unwrap_or_else(|_| "unknown input port".to_string());
        let port = MidiPortWrapper {
            id,
            name,
            port: MidiPort::Output(port),
            open_count: 0,
        };
        wrappers.push(port);
    }
}