Debian Codegraph @

third_party/rust/coremidi/src/ports.rs

raw
1 use core_foundation::base::OSStatus;
2
3 use coremidi_sys::{MIDIPortConnectSource, MIDIPortDisconnectSource, MIDIPortDispose, MIDISend};
4
5 use std::ops::Deref;
6 use std::ptr;
7
8 use crate::callback::BoxedCallback;
9 use crate::endpoints::destinations::Destination;
10 use crate::endpoints::sources::Source;
11 use crate::object::Object;
12 use crate::packets::PacketList;
13
14 /// A MIDI connection port owned by a client.
15 /// See [MIDIPortRef](https://developer.apple.com/reference/coremidi/midiportref).
16 ///
17 /// Ports can't be instantiated directly, but through a client.
18 ///
19 #[derive(Debug)]
20 pub struct Port {
21 pub(crate) object: Object,
22 }
23
24 impl Deref for Port {
25 type Target = Object;
26
27 fn deref(&self) -> &Object {
28 &self.object
29 }
30 }
31
32 impl Drop for Port {
33 fn drop(&mut self) {
34 unsafe { MIDIPortDispose(self.object.0) };
35 }
36 }
37
38 /// An output [MIDI port](https://developer.apple.com/reference/coremidi/midiportref) owned by a client.
39 ///
40 /// A simple example to create an output port and send a MIDI event:
41 ///
42 /// ```rust,no_run
43 /// let client = coremidi::Client::new("example-client").unwrap();
44 /// let output_port = client.output_port("example-port").unwrap();
45 /// let destination = coremidi::Destination::from_index(0).unwrap();
46 /// let packets = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]);
47 /// output_port.send(&destination, &packets).unwrap();
48 /// ```
49 #[derive(Debug)]
50 pub struct OutputPort {
51 pub(crate) port: Port,
52 }
53
54 impl OutputPort {
55 /// Send a list of packets to a destination.
56 /// See [MIDISend](https://developer.apple.com/reference/coremidi/1495289-midisend).
57 ///
58 pub fn send(
59 &self,
60 destination: &Destination,
61 packet_list: &PacketList,
62 ) -> Result<(), OSStatus> {
63 let status = unsafe {
64 MIDISend(
65 self.port.object.0,
66 destination.endpoint.object.0,
67 packet_list.as_ptr(),
68 )
69 };
70 if status == 0 {
71 Ok(())
72 } else {
73 Err(status)
74 }
75 }
76 }
77
78 impl Deref for OutputPort {
79 type Target = Port;
80
81 fn deref(&self) -> &Port {
82 &self.port
83 }
84 }
85
86 /// An input [MIDI port](https://developer.apple.com/reference/coremidi/midiportref) owned by a client.
87 ///
88 /// A simple example to create an input port:
89 ///
90 /// ```rust,no_run
91 /// let client = coremidi::Client::new("example-client").unwrap();
92 /// let input_port = client.input_port("example-port", |packet_list| println!("{}", packet_list)).unwrap();
93 /// let source = coremidi::Source::from_index(0).unwrap();
94 /// input_port.connect_source(&source);
95 /// ```
96 #[derive(Debug)]
97 pub struct InputPort {
98 // Note: the order is important here, port needs to be dropped first
99 pub(crate) port: Port,
100 pub(crate) callback: BoxedCallback<PacketList>,
101 }
102
103 impl InputPort {
104 pub fn connect_source(&self, source: &Source) -> Result<(), OSStatus> {
105 let status =
106 unsafe { MIDIPortConnectSource(self.object.0, source.object.0, ptr::null_mut()) };
107 if status == 0 {
108 Ok(())
109 } else {
110 Err(status)
111 }
112 }
113
114 pub fn disconnect_source(&self, source: &Source) -> Result<(), OSStatus> {
115 let status = unsafe { MIDIPortDisconnectSource(self.object.0, source.object.0) };
116 if status == 0 {
117 Ok(())
118 } else {
119 Err(status)
120 }
121 }
122 }
123
124 impl Deref for InputPort {
125 type Target = Port;
126
127 fn deref(&self) -> &Port {
128 &self.port
129 }
130 }