Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MidiInputPort::id() and MidiOutputPort::id() #157

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/backend/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ pub struct MidiInputPort {
addr: Addr,
}

impl MidiInputPort {
pub fn id(&self) -> String {
format!("{}:{}", self.addr.client, self.addr.port)
}
}

pub struct MidiInputConnection<T: 'static> {
subscription: Option<PortSubscribe>,
thread: Option<JoinHandle<(HandlerData<T>, T)>>,
Expand Down Expand Up @@ -523,6 +529,12 @@ pub struct MidiOutputPort {
addr: Addr,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
format!("{}:{}", self.addr.client, self.addr.port)
}
}

pub struct MidiOutputConnection {
seq: Option<Seq>,
vport: i32,
Expand Down
22 changes: 21 additions & 1 deletion src/backend/coremidi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ pub struct MidiInputPort {
source: Arc<Source>,
}

impl MidiInputPort {
pub fn id(&self) -> String {
self.source
.unique_id()
// According to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case
.unwrap_or(0)
.to_string()
}
}

impl PartialEq for MidiInputPort {
fn eq(&self, other: &Self) -> bool {
if let (Some(id1), Some(id2)) = (self.source.unique_id(), other.source.unique_id()) {
id1 == id2
} else {
// Acording to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case
// According to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case
false
}
}
Expand Down Expand Up @@ -292,6 +302,16 @@ pub struct MidiOutputPort {
dest: Arc<Destination>,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
self.dest
.unique_id()
// According to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case
.unwrap_or(0)
.to_string()
}
}

impl PartialEq for MidiOutputPort {
fn eq(&self, other: &Self) -> bool {
if let (Some(id1), Some(id2)) = (self.dest.unique_id(), other.dest.unique_id()) {
Expand Down
12 changes: 12 additions & 0 deletions src/backend/jack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub struct MidiInputPort {
name: CString,
}

impl MidiInputPort {
pub fn id(&self) -> String {
self.name.to_string_lossy().to_string()
}
}

pub struct MidiInputConnection<T> {
handler_data: Box<InputHandlerData<T>>,
client: Option<Client>,
Expand Down Expand Up @@ -258,6 +264,12 @@ pub struct MidiOutputPort {
name: CString,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
self.name.to_string_lossy().to_string()
}
}

pub struct MidiOutputConnection {
handler_data: Box<OutputHandlerData>,
client: Option<Client>,
Expand Down
12 changes: 12 additions & 0 deletions src/backend/webmidi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ pub struct MidiInputPort {
input: web_sys::MidiInput,
}

impl MidiInputPort {
pub fn id(&self) -> String {
self.input.id()
}
}

pub struct MidiInput {
ignore_flags: Ignore,
}
Expand Down Expand Up @@ -223,6 +229,12 @@ pub struct MidiOutputPort {
output: web_sys::MidiOutput,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
self.output.id()
}
}

pub struct MidiOutput {}

impl MidiOutput {
Expand Down
12 changes: 12 additions & 0 deletions src/backend/winmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ pub struct MidiInputPort {
interface_id: Box<[u16]>,
}

impl MidiInputPort {
pub fn id(&self) -> String {
String::from_utf16_lossy(&self.interface_id)
}
}

impl PartialEq for MidiInputPort {
fn eq(&self, other: &Self) -> bool {
self.interface_id == other.interface_id
Expand Down Expand Up @@ -396,6 +402,12 @@ pub struct MidiOutputPort {
interface_id: Box<[u16]>,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
String::from_utf16_lossy(&self.interface_id)
}
}

impl PartialEq for MidiOutputPort {
fn eq(&self, other: &Self) -> bool {
self.interface_id == other.interface_id
Expand Down
12 changes: 12 additions & 0 deletions src/backend/winrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct MidiInputPort {
id: HSTRING,
}

impl MidiInputPort {
pub fn id(&self) -> String {
self.id.to_string_lossy()
}
}

pub struct MidiInput {
selector: HSTRING,
ignore_flags: Ignore,
Expand Down Expand Up @@ -189,6 +195,12 @@ pub struct MidiOutputPort {
id: HSTRING,
}

impl MidiOutputPort {
pub fn id(&self) -> String {
self.id.to_string_lossy()
}
}

pub struct MidiOutput {
selector: HSTRING,
}
Expand Down
26 changes: 26 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ pub struct MidiInputPort {
pub(crate) imp: MidiInputPortImpl,
}

impl MidiInputPort {
/// Get a unique stable identifier for this port.
/// This identifier must be treated as an opaque string.
pub fn id(&self) -> String {
self.imp.id()
}
}

/// A collection of input ports.
pub type MidiInputPorts = Vec<MidiInputPort>;

Expand Down Expand Up @@ -84,6 +92,11 @@ impl MidiInput {
self.imp.port_name(&port.imp)
}

/// Get a MIDI input port by its unique identifier.
pub fn find_port_by_id(&self, id: String) -> Option<MidiInputPort> {
self.ports().into_iter().find(|port| port.id() == id)
}

/// Connect to a specified MIDI input port in order to receive messages.
/// For each incoming MIDI message, the provided `callback` function will
/// be called. The first parameter of the callback function is a timestamp
Expand Down Expand Up @@ -199,6 +212,14 @@ pub struct MidiOutputPort {
pub(crate) imp: MidiOutputPortImpl,
}

impl MidiOutputPort {
/// Get a unique stable identifier for this port.
/// This identifier must be treated as an opaque string.
pub fn id(&self) -> String {
self.imp.id()
}
}

/// A collection of output ports.
pub type MidiOutputPorts = Vec<MidiOutputPort>;

Expand Down Expand Up @@ -235,6 +256,11 @@ impl MidiOutput {
self.imp.port_name(&port.imp)
}

/// Get a MIDI output port by its unique identifier.
pub fn find_port_by_id(&self, id: String) -> Option<MidiOutputPort> {
self.ports().into_iter().find(|port| port.id() == id)
}

/// Connect to a specified MIDI output port in order to send messages.
/// The connection will be kept open as long as the returned
/// `MidiOutputConnection` is kept alive.
Expand Down