Skip to main content

midi_io/
port.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2pub struct VirtualPortId(pub(crate) u64);
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct PortId(pub(crate) u64);
6
7impl PortId {
8    /// The handle's raw value, for storing a reference to a port and matching
9    /// it against [`Source::id`] or [`Destination::id`] in a later session.
10    ///
11    /// How long the value names the same port depends on the backend:
12    ///
13    /// - CoreMIDI: the endpoint's `kMIDIPropertyUniqueID`, which the system
14    ///   persists across launches.
15    /// - ALSA: `(client_id << 32) | port_id`; the kernel assigns client numbers
16    ///   at registration, so the value can change when a device is reconnected.
17    /// - Web MIDI: a hash of the browser's `MIDIPort.id`, as stable as the
18    ///   browser keeps that id.
19    pub fn to_bits(self) -> u64 {
20        self.0
21    }
22}
23
24/// A source is a sender of MIDI messages.
25#[derive(Debug, Clone, PartialEq, Eq, Hash)]
26pub struct Source {
27    pub(crate) id: PortId,
28    pub(crate) name: String,
29    pub(crate) is_virtual: bool,
30}
31
32impl Source {
33    pub fn id(&self) -> PortId {
34        self.id
35    }
36
37    pub fn name(&self) -> &str {
38        &self.name
39    }
40
41    pub fn is_virtual(&self) -> bool {
42        self.is_virtual
43    }
44}
45
46/// Indicates when available sources have changed
47#[derive(Debug, Clone, PartialEq, Eq, Hash)]
48pub enum SourceChange {
49    Added(Source),
50    Removed(Source),
51}
52
53/// A destination is a receiver of MIDI messages.
54#[derive(Debug, Clone, PartialEq, Eq, Hash)]
55pub struct Destination {
56    pub(crate) id: PortId,
57    pub(crate) name: String,
58    pub(crate) is_virtual: bool,
59}
60
61impl Destination {
62    pub fn id(&self) -> PortId {
63        self.id
64    }
65
66    pub fn name(&self) -> &str {
67        &self.name
68    }
69
70    pub fn is_virtual(&self) -> bool {
71        self.is_virtual
72    }
73}
74
75/// Indicates when available destinations have changed
76#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77pub enum DestinationChange {
78    Added(Destination),
79    Removed(Destination),
80}