Debian Codegraph @

third_party/rust/coremidi/src/endpoints/sources.rs

raw
1 use core_foundation_sys::base::OSStatus;
2
3 use coremidi_sys::{
4 ItemCount, MIDIEndpointDispose, MIDIGetNumberOfSources, MIDIGetSource, MIDIReceived,
5 };
6
7 use std::ops::Deref;
8
9 use crate::object::Object;
10 use crate::packets::PacketList;
11
12 use super::Endpoint;
13
14 /// A [MIDI source](https://developer.apple.com/reference/coremidi/midiendpointref) owned by an entity.
15 ///
16 /// A source can be created from an index like this:
17 ///
18 /// ```rust,no_run
19 /// let source = coremidi::Source::from_index(0).unwrap();
20 /// println!("The source at index 0 has display name '{}'", source.display_name().unwrap());
21 /// ```
22 ///
23 #[derive(Debug)]
24 pub struct Source {
25 endpoint: Endpoint,
26 }
27
28 impl Source {
29 /// Create a source endpoint from its index.
30 /// See [MIDIGetSource](https://developer.apple.com/reference/coremidi/1495168-midigetsource)
31 ///
32 pub fn from_index(index: usize) -> Option<Source> {
33 let endpoint_ref = unsafe { MIDIGetSource(index as ItemCount) };
34 match endpoint_ref {
35 0 => None,
36 _ => Some(Source {
37 endpoint: Endpoint {
38 object: Object(endpoint_ref),
39 },
40 }),
41 }
42 }
43 }
44
45 impl Deref for Source {
46 type Target = Endpoint;
47
48 fn deref(&self) -> &Endpoint {
49 &self.endpoint
50 }
51 }
52
53 /// Source endpoints available in the system.
54 ///
55 /// The number of sources available in the system can be retrieved with:
56 ///
57 /// ```rust,no_run
58 /// let number_of_sources = coremidi::Sources::count();
59 /// ```
60 ///
61 /// The sources in the system can be iterated as:
62 ///
63 /// ```rust,no_run
64 /// for source in coremidi::Sources {
65 /// println!("{}", source.display_name().unwrap());
66 /// }
67 /// ```
68 ///
69 pub struct Sources;
70
71 impl Sources {
72 /// Get the number of sources available in the system for receiving MIDI messages.
73 /// See [MIDIGetNumberOfSources](https://developer.apple.com/reference/coremidi/1495116-midigetnumberofsources).
74 ///
75 pub fn count() -> usize {
76 unsafe { MIDIGetNumberOfSources() as usize }
77 }
78 }
79
80 impl IntoIterator for Sources {
81 type Item = Source;
82 type IntoIter = SourcesIterator;
83
84 fn into_iter(self) -> Self::IntoIter {
85 SourcesIterator {
86 index: 0,
87 count: Self::count(),
88 }
89 }
90 }
91
92 pub struct SourcesIterator {
93 index: usize,
94 count: usize,
95 }
96
97 impl Iterator for SourcesIterator {
98 type Item = Source;
99
100 fn next(&mut self) -> Option<Source> {
101 if self.index < self.count {
102 let source = Source::from_index(self.index);
103 self.index += 1;
104 source
105 } else {
106 None
107 }
108 }
109 }
110
111 /// A [MIDI virtual source](https://developer.apple.com/reference/coremidi/1495212-midisourcecreate) owned by a client.
112 ///
113 /// A virtual source can be created like:
114 ///
115 /// ```rust,no_run
116 /// let client = coremidi::Client::new("example-client").unwrap();
117 /// let source = client.virtual_source("example-source").unwrap();
118 /// ```
119 ///
120 #[derive(Debug)]
121 pub struct VirtualSource {
122 pub(crate) endpoint: Endpoint,
123 }
124
125 impl VirtualSource {
126 /// Distributes incoming MIDI from a source to the client input ports which are connected to that source.
127 /// See [MIDIReceived](https://developer.apple.com/reference/coremidi/1495276-midireceived)
128 ///
129 pub fn received(&self, packet_list: &PacketList) -> Result<(), OSStatus> {
130 let status = unsafe { MIDIReceived(self.endpoint.object.0, packet_list.as_ptr()) };
131 if status == 0 {
132 Ok(())
133 } else {
134 Err(status)
135 }
136 }
137 }
138
139 impl Deref for VirtualSource {
140 type Target = Endpoint;
141
142 fn deref(&self) -> &Endpoint {
143 &self.endpoint
144 }
145 }
146
147 impl Drop for VirtualSource {
148 fn drop(&mut self) {
149 unsafe { MIDIEndpointDispose(self.endpoint.object.0) };
150 }
151 }