| 1 |
use core_foundation::{ |
| 2 |
base::{OSStatus, TCFType}, |
| 3 |
string::CFString, |
| 4 |
}; |
| 5 |
|
| 6 |
use coremidi_sys::{ |
| 7 |
MIDIClientCreate, MIDIClientDispose, MIDIDestinationCreate, MIDIInputPortCreate, |
| 8 |
MIDINotification, MIDIOutputPortCreate, MIDIPacketList, MIDISourceCreate, |
| 9 |
}; |
| 10 |
|
| 11 |
use std::{mem::MaybeUninit, ops::Deref, os::raw::c_void, panic::catch_unwind, ptr}; |
| 12 |
|
| 13 |
use crate::{ |
| 14 |
callback::BoxedCallback, |
| 15 |
endpoints::{destinations::VirtualDestination, sources::VirtualSource, Endpoint}, |
| 16 |
notifications::Notification, |
| 17 |
object::Object, |
| 18 |
packets::PacketList, |
| 19 |
ports::{InputPort, OutputPort, Port}, |
| 20 |
result_from_status, |
| 21 |
}; |
| 22 |
|
| 23 |
/// A [MIDI client](https://developer.apple.com/reference/coremidi/midiclientref). |
| 24 |
/// |
| 25 |
/// An object maintaining per-client state. |
| 26 |
/// |
| 27 |
/// A simple example to create a Client: |
| 28 |
/// |
| 29 |
/// ```rust,no_run |
| 30 |
/// let client = coremidi::Client::new("example-client").unwrap(); |
| 31 |
/// ``` |
| 32 |
#[derive(Debug)] |
| 33 |
pub struct Client { |
| 34 |
// Order is important, object needs to be dropped first |
| 35 |
object: Object, |
| 36 |
callback: BoxedCallback<Notification>, |
| 37 |
} |
| 38 |
|
| 39 |
impl Client { |
| 40 |
/// Creates a new CoreMIDI client with support for notifications. |
| 41 |
/// See [MIDIClientCreate](https://developer.apple.com/reference/coremidi/1495360-midiclientcreate). |
| 42 |
/// |
| 43 |
/// The notification callback will be called on the [run loop](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html) |
| 44 |
/// that was current when this associated function is called. |
| 45 |
/// |
| 46 |
/// It follows that this particular run loop needs to be running in order to |
| 47 |
/// actually receive notifications. The run loop can be started after the |
| 48 |
/// client has been created if need be. |
| 49 |
pub fn new_with_notifications<F>(name: &str, callback: F) -> Result<Client, OSStatus> |
| 50 |
where |
| 51 |
F: FnMut(&Notification) + Send + 'static, |
| 52 |
{ |
| 53 |
let client_name = CFString::new(name); |
| 54 |
let mut client_ref = MaybeUninit::uninit(); |
| 55 |
let mut boxed_callback = BoxedCallback::new(callback); |
| 56 |
let status = unsafe { |
| 57 |
MIDIClientCreate( |
| 58 |
client_name.as_concrete_TypeRef(), |
| 59 |
Some(Self::notify_proc as extern "C" fn(_, _)), |
| 60 |
boxed_callback.raw_ptr(), |
| 61 |
client_ref.as_mut_ptr(), |
| 62 |
) |
| 63 |
}; |
| 64 |
result_from_status(status, || { |
| 65 |
let client_ref = unsafe { client_ref.assume_init() }; |
| 66 |
Client { |
| 67 |
object: Object(client_ref), |
| 68 |
callback: boxed_callback, |
| 69 |
} |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
/// Creates a new CoreMIDI client. |
| 74 |
/// See [MIDIClientCreate](https://developer.apple.com/reference/coremidi/1495360-midiclientcreate). |
| 75 |
/// |
| 76 |
pub fn new(name: &str) -> Result<Client, OSStatus> { |
| 77 |
let client_name = CFString::new(name); |
| 78 |
let mut client_ref = MaybeUninit::uninit(); |
| 79 |
let status = unsafe { |
| 80 |
MIDIClientCreate( |
| 81 |
client_name.as_concrete_TypeRef(), |
| 82 |
None, |
| 83 |
ptr::null_mut(), |
| 84 |
client_ref.as_mut_ptr(), |
| 85 |
) |
| 86 |
}; |
| 87 |
result_from_status(status, || { |
| 88 |
let client_ref = unsafe { client_ref.assume_init() }; |
| 89 |
Client { |
| 90 |
object: Object(client_ref), |
| 91 |
callback: BoxedCallback::null(), |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
/// Creates an output port through which the client may send outgoing MIDI messages to any MIDI destination. |
| 97 |
/// See [MIDIOutputPortCreate](https://developer.apple.com/reference/coremidi/1495166-midioutputportcreate). |
| 98 |
/// |
| 99 |
pub fn output_port(&self, name: &str) -> Result<OutputPort, OSStatus> { |
| 100 |
let port_name = CFString::new(name); |
| 101 |
let mut port_ref = MaybeUninit::uninit(); |
| 102 |
let status = unsafe { |
| 103 |
MIDIOutputPortCreate( |
| 104 |
self.object.0, |
| 105 |
port_name.as_concrete_TypeRef(), |
| 106 |
port_ref.as_mut_ptr(), |
| 107 |
) |
| 108 |
}; |
| 109 |
result_from_status(status, || { |
| 110 |
let port_ref = unsafe { port_ref.assume_init() }; |
| 111 |
OutputPort { |
| 112 |
port: Port { |
| 113 |
object: Object(port_ref), |
| 114 |
}, |
| 115 |
} |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
/// Creates an input port through which the client may receive incoming MIDI messages from any MIDI source. |
| 120 |
/// See [MIDIInputPortCreate](https://developer.apple.com/reference/coremidi/1495225-midiinputportcreate). |
| 121 |
/// |
| 122 |
pub fn input_port<F>(&self, name: &str, callback: F) -> Result<InputPort, OSStatus> |
| 123 |
where |
| 124 |
F: FnMut(&PacketList) + Send + 'static, |
| 125 |
{ |
| 126 |
let port_name = CFString::new(name); |
| 127 |
let mut port_ref = MaybeUninit::uninit(); |
| 128 |
let mut box_callback = BoxedCallback::new(callback); |
| 129 |
let status = unsafe { |
| 130 |
MIDIInputPortCreate( |
| 131 |
self.object.0, |
| 132 |
port_name.as_concrete_TypeRef(), |
| 133 |
Some(Self::read_proc as extern "C" fn(_, _, _)), |
| 134 |
box_callback.raw_ptr(), |
| 135 |
port_ref.as_mut_ptr(), |
| 136 |
) |
| 137 |
}; |
| 138 |
result_from_status(status, || { |
| 139 |
let port_ref = unsafe { port_ref.assume_init() }; |
| 140 |
InputPort { |
| 141 |
port: Port { |
| 142 |
object: Object(port_ref), |
| 143 |
}, |
| 144 |
callback: box_callback, |
| 145 |
} |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
/// Creates a virtual source in the client. |
| 150 |
/// See [MIDISourceCreate](https://developer.apple.com/reference/coremidi/1495212-midisourcecreate). |
| 151 |
/// |
| 152 |
pub fn virtual_source(&self, name: &str) -> Result<VirtualSource, OSStatus> { |
| 153 |
let virtual_source_name = CFString::new(name); |
| 154 |
let mut virtual_source = MaybeUninit::uninit(); |
| 155 |
let status = unsafe { |
| 156 |
MIDISourceCreate( |
| 157 |
self.object.0, |
| 158 |
virtual_source_name.as_concrete_TypeRef(), |
| 159 |
virtual_source.as_mut_ptr(), |
| 160 |
) |
| 161 |
}; |
| 162 |
result_from_status(status, || { |
| 163 |
let virtual_source = unsafe { virtual_source.assume_init() }; |
| 164 |
VirtualSource { |
| 165 |
endpoint: Endpoint { |
| 166 |
object: Object(virtual_source), |
| 167 |
}, |
| 168 |
} |
| 169 |
}) |
| 170 |
} |
| 171 |
|
| 172 |
/// Creates a virtual destination in the client. |
| 173 |
/// See [MIDIDestinationCreate](https://developer.apple.com/reference/coremidi/1495347-mididestinationcreate). |
| 174 |
/// |
| 175 |
pub fn virtual_destination<F>( |
| 176 |
&self, |
| 177 |
name: &str, |
| 178 |
callback: F, |
| 179 |
) -> Result<VirtualDestination, OSStatus> |
| 180 |
where |
| 181 |
F: FnMut(&PacketList) + Send + 'static, |
| 182 |
{ |
| 183 |
let virtual_destination_name = CFString::new(name); |
| 184 |
let mut virtual_destination = MaybeUninit::uninit(); |
| 185 |
let mut boxed_callback = BoxedCallback::new(callback); |
| 186 |
let status = unsafe { |
| 187 |
MIDIDestinationCreate( |
| 188 |
self.object.0, |
| 189 |
virtual_destination_name.as_concrete_TypeRef(), |
| 190 |
Some(Self::read_proc as extern "C" fn(_, _, _)), |
| 191 |
boxed_callback.raw_ptr(), |
| 192 |
virtual_destination.as_mut_ptr(), |
| 193 |
) |
| 194 |
}; |
| 195 |
result_from_status(status, || { |
| 196 |
let virtual_destination = unsafe { virtual_destination.assume_init() }; |
| 197 |
VirtualDestination { |
| 198 |
endpoint: Endpoint { |
| 199 |
object: Object(virtual_destination), |
| 200 |
}, |
| 201 |
callback: boxed_callback, |
| 202 |
} |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
extern "C" fn notify_proc(notification_ptr: *const MIDINotification, ref_con: *mut c_void) { |
| 207 |
let _ = catch_unwind(|| unsafe { |
| 208 |
if let Ok(notification) = Notification::from(&*notification_ptr) { |
| 209 |
BoxedCallback::call_from_raw_ptr(ref_con, ¬ification) |
| 210 |
} |
| 211 |
}); |
| 212 |
} |
| 213 |
|
| 214 |
extern "C" fn read_proc( |
| 215 |
pktlist: *const MIDIPacketList, |
| 216 |
read_proc_ref_con: *mut c_void, |
| 217 |
_src_conn_ref_con: *mut c_void, |
| 218 |
) { |
| 219 |
let _ = catch_unwind(|| unsafe { |
| 220 |
let packet_list = &*(pktlist as *const PacketList); |
| 221 |
BoxedCallback::call_from_raw_ptr(read_proc_ref_con, packet_list); |
| 222 |
}); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl Deref for Client { |
| 227 |
type Target = Object; |
| 228 |
|
| 229 |
fn deref(&self) -> &Object { |
| 230 |
&self.object |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
impl Drop for Client { |
| 235 |
fn drop(&mut self) { |
| 236 |
unsafe { MIDIClientDispose(self.object.0) }; |
| 237 |
} |
| 238 |
} |