Skip to content

Commit

Permalink
Refactoring and Upgrading (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-zen authored Jan 4, 2022
1 parent 14eff2e commit fc68464
Show file tree
Hide file tree
Showing 25 changed files with 854 additions and 747 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target
target
Cargo.lock
.idea
1 change: 1 addition & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coremidi"
version = "0.0.0"
version = "0.6.0"
authors = ["Christian Perez-Llamas"]
description = "CoreMIDI library for Rust"
license = "MIT"
Expand All @@ -9,9 +9,10 @@ repository = "https://github.com/chris-zen/coremidi"
documentation = "https://chris-zen.github.io/coremidi/coremidi/"
readme = "README.md"
keywords = ["CoreMIDI", "MIDI", "OSX", "macOS", "music"]
edition = "2018"


[dependencies]
core-foundation-sys = "0.2"
core-foundation = "0.2"
coremidi-sys = "2.0"
core-foundation-sys = "0.8.2"
core-foundation = "0.9.1"
coremidi-sys = "3.0.1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The library is published into [crates.io](https://crates.io/crates/coremidi), so

```toml
[dependencies]
coremidi = "^0.3.1"
coremidi = "^0.6.0"
```

If you prefer to live in the edge ;-) you can use the master branch by including this instead:
Expand Down
2 changes: 1 addition & 1 deletion ci/update-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -ex

VERSION=${TRAVIS_TAG:-$(git describe --tags)}

sed -i '' "s/version = \"0.0.0\"/version = \"$VERSION\"/g" Cargo.toml
sed -i '' "s/version = \".*\"/version = \"$VERSION\"/g" Cargo.toml
6 changes: 4 additions & 2 deletions examples/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
println!("[{}] {}", i, display_name);
}

println!("");
println!();
println!("System sources:");

for (i, source) in coremidi::Sources.into_iter().enumerate() {
Expand All @@ -18,5 +18,7 @@ fn main() {
}

fn get_display_name(endpoint: &coremidi::Endpoint) -> String {
endpoint.display_name().unwrap_or("[Unknown Display Name]".to_string())
endpoint
.display_name()
.unwrap_or_else(|| "[Unknown Display Name]".to_string())
}
22 changes: 7 additions & 15 deletions examples/notifications.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
extern crate coremidi;
extern crate core_foundation;
extern crate coremidi;

use coremidi::{
Client,
Notification,
};
use coremidi::{Client, Notification};

use core_foundation::runloop::{
CFRunLoopRunInMode,
kCFRunLoopDefaultMode,
};
use core_foundation::runloop::{kCFRunLoopDefaultMode, CFRunLoopRunInMode};

fn main() {
println!("Logging MIDI Client Notifications");
println!("Will Quit Automatically After 10 Seconds");
println!("");
println!();

let _client = Client::new_with_notifications("example-client", print_notification).unwrap();

Expand All @@ -23,17 +17,15 @@ fn main() {
// Client was created.
//
// In order to actually receive the notifications, a run loop must be
// running. Since this sample app does not use an app framework like
// running. Since this sample app does not use an app framework like
// UIApplication or NSApplication, it does not have a run loop running yet.
// So we start one that lasts for 10 seconds with the following line.
//
// You may not have to do this in your app - see https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW24
// for information about when run loops are running automatically.
unsafe {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, 0)
};
unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, 0) };
}

fn print_notification(notification: &Notification) {
println!("Received Notification: {:?} \r", notification);
}
}
12 changes: 4 additions & 8 deletions examples/properties.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
extern crate coremidi;

use coremidi::{
Client,
PacketList,
Properties,
PropertyGetter,
PropertySetter,
};
use coremidi::{Client, PacketList, Properties, PropertyGetter, PropertySetter};

fn main() {
let client = Client::new("Example Client").unwrap();
Expand All @@ -16,7 +10,9 @@ fn main() {
};

// Creates a virtual destination, then gets its properties
let destination = client.virtual_destination("Example Destination", callback).unwrap();
let destination = client
.virtual_destination("Example Destination", callback)
.unwrap();

println!("Created Virtual Destination...");

Expand Down
24 changes: 15 additions & 9 deletions examples/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ fn main() {

let mut input_line = String::new();
println!("Press Enter to Finish");
std::io::stdin().read_line(&mut input_line).ok().expect("Failed to read line");
std::io::stdin()
.read_line(&mut input_line)
.expect("Failed to read line");

input_port.disconnect_source(&source).unwrap();
}

fn get_source_index() -> usize {
let mut args_iter = env::args();
let tool_name = args_iter.next()
.and_then(|path| path.split(std::path::MAIN_SEPARATOR).last().map(|v| v.to_string()))
.unwrap_or("receive".to_string());
let tool_name = args_iter
.next()
.and_then(|path| {
path.split(std::path::MAIN_SEPARATOR)
.last()
.map(|v| v.to_string())
})
.unwrap_or_else(|| "receive".to_string());

match args_iter.next() {
Some(arg) => match arg.parse::<usize>() {
Expand All @@ -39,15 +46,15 @@ fn get_source_index() -> usize {
std::process::exit(-1);
}
index
},
}
Err(_) => {
println!("Wrong source index: {}", arg);
std::process::exit(-1);
}
},
None => {
println!("Usage: {} <source-index>", tool_name);
println!("");
println!();
println!("Available Sources:");
print_sources();
std::process::exit(-1);
Expand All @@ -57,9 +64,8 @@ fn get_source_index() -> usize {

fn print_sources() {
for (i, source) in coremidi::Sources.into_iter().enumerate() {
match source.display_name() {
Some(display_name) => println!("[{}] {}", i, display_name),
None => ()
if let Some(display_name) = source.display_name() {
println!("[{}] {}", i, display_name)
}
}
}
39 changes: 20 additions & 19 deletions examples/send.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
extern crate coremidi;

use std::time::Duration;
use std::thread;
use std::env;
use std::thread;
use std::time::Duration;

fn main() {
let destination_index = get_destination_index();
println!("Destination index: {}", destination_index);

let destination = coremidi::Destination::from_index(destination_index).unwrap();
println!("Destination display name: {}", destination.display_name().unwrap());
println!(
"Destination display name: {}",
destination.display_name().unwrap()
);

let client = coremidi::Client::new("Example Client").unwrap();
let output_port = client.output_port("Example Port").unwrap();
Expand All @@ -30,9 +33,14 @@ fn main() {

fn get_destination_index() -> usize {
let mut args_iter = env::args();
let tool_name = args_iter.next()
.and_then(|path| path.split(std::path::MAIN_SEPARATOR).last().map(|v| v.to_string()))
.unwrap_or("send".to_string());
let tool_name = args_iter
.next()
.and_then(|path| {
path.split(std::path::MAIN_SEPARATOR)
.last()
.map(|v| v.to_string())
})
.unwrap_or_else(|| "send".to_string());

match args_iter.next() {
Some(arg) => match arg.parse::<usize>() {
Expand All @@ -42,15 +50,15 @@ fn get_destination_index() -> usize {
std::process::exit(-1);
}
index
},
}
Err(_) => {
println!("Wrong destination index: {}", arg);
std::process::exit(-1);
}
},
None => {
println!("Usage: {} <destination-index>", tool_name);
println!("");
println!();
println!("Available Destinations:");
print_destinations();
std::process::exit(-1);
Expand All @@ -60,25 +68,18 @@ fn get_destination_index() -> usize {

fn print_destinations() {
for (i, destination) in coremidi::Destinations.into_iter().enumerate() {
match destination.display_name() {
Some(display_name) => println!("[{}] {}", i, display_name),
None => ()
if let Some(display_name) = destination.display_name() {
println!("[{}] {}", i, display_name)
}
}
}

fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = &[
0x90 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
let data = &[0x90 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
coremidi::PacketBuffer::new(0, data)
}

fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = &[
0x80 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
let data = &[0x80 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
coremidi::PacketBuffer::new(0, data)
}
8 changes: 6 additions & 2 deletions examples/virtual-destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ fn main() {
println!("{}", packet_list);
};

let _destination = client.virtual_destination("Example Destination", callback).unwrap();
let _destination = client
.virtual_destination("Example Destination", callback)
.unwrap();

let mut input_line = String::new();
println!("Created Virtual Destination \"Example Destination\"");
println!("Press Enter to Finish");
std::io::stdin().read_line(&mut input_line).ok().expect("Failed to read line");
std::io::stdin()
.read_line(&mut input_line)
.expect("Failed to read line");
}
12 changes: 3 additions & 9 deletions examples/virtual-source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate coremidi;

use std::time::Duration;
use std::thread;
use std::time::Duration;

fn main() {
let client = coremidi::Client::new("Example Client").unwrap();
Expand All @@ -22,17 +22,11 @@ fn main() {
}

fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = &[
0x90 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
let data = &[0x90 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
coremidi::PacketBuffer::new(0, data)
}

fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = &[
0x80 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
let data = &[0x80 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
coremidi::PacketBuffer::new(0, data)
}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.36.0
1.51.0
35 changes: 35 additions & 0 deletions src/callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// A lifetime-managed wrapper for callback functions
#[derive(Debug, PartialEq)]
pub struct BoxedCallback<T>(*mut Box<dyn FnMut(&T)>);

impl<T> BoxedCallback<T> {
pub fn new<F: FnMut(&T) + Send + 'static>(f: F) -> BoxedCallback<T> {
BoxedCallback(Box::into_raw(Box::new(Box::new(f))))
}

pub fn null() -> BoxedCallback<T> {
BoxedCallback(::std::ptr::null_mut())
}

pub fn raw_ptr(&mut self) -> *mut ::std::os::raw::c_void {
self.0 as *mut ::std::os::raw::c_void
}

// must not be null
pub unsafe fn call_from_raw_ptr(raw_ptr: *mut ::std::os::raw::c_void, arg: &T) {
let callback = &mut *(raw_ptr as *mut Box<dyn FnMut(&T)>);
callback(arg);
}
}

unsafe impl<T> Send for BoxedCallback<T> {}

impl<T> Drop for BoxedCallback<T> {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
let _ = Box::from_raw(self.0);
}
}
}
}
Loading

0 comments on commit fc68464

Please sign in to comment.