-
Notifications
You must be signed in to change notification settings - Fork 20
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
PacketBuffer rewrite #9
Changes from 8 commits
afc9f20
fbfdae7
45d0044
30f4054
effc202
f554dd3
07983d5
f72086a
2e6b442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ use std::thread; | |
let client = coremidi::Client::new("example-client").unwrap(); | ||
let output_port = client.output_port("example-port").unwrap(); | ||
let destination = coremidi::Destination::from_index(0).unwrap(); | ||
let note_on = coremidi::PacketBuffer::from_data(0, vec![0x90, 0x40, 0x7f]); | ||
let note_off = coremidi::PacketBuffer::from_data(0, vec![0x80, 0x40, 0x7f]); | ||
let note_on = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]); | ||
let note_off = coremidi::PacketBuffer::new(0, &[0x80, 0x40, 0x7f]); | ||
output_port.send(&destination, ¬e_on).unwrap(); | ||
thread::sleep(Duration::from_millis(1000)); | ||
output_port.send(&destination, ¬e_off).unwrap(); | ||
|
@@ -41,16 +41,11 @@ For handling low level MIDI data you may look into: | |
extern crate core_foundation_sys; | ||
extern crate core_foundation; | ||
extern crate coremidi_sys; | ||
extern crate libc; | ||
|
||
use core_foundation_sys::base::OSStatus; | ||
|
||
use coremidi_sys::{ | ||
MIDIObjectRef, MIDIFlushOutput, MIDIRestart | ||
}; | ||
|
||
use coremidi_sys_ext::{ | ||
MIDIPacketList | ||
MIDIObjectRef, MIDIFlushOutput, MIDIRestart, MIDIPacket, MIDIPacketList | ||
}; | ||
|
||
/// A [MIDI Object](https://developer.apple.com/reference/coremidi/midiobjectref). | ||
|
@@ -89,12 +84,12 @@ impl<T> BoxedCallback<T> { | |
BoxedCallback(::std::ptr::null_mut()) | ||
} | ||
|
||
fn raw_ptr(&mut self) -> *mut ::libc::c_void { | ||
self.0 as *mut ::libc::c_void | ||
fn raw_ptr(&mut self) -> *mut ::std::os::raw::c_void { | ||
self.0 as *mut ::std::os::raw::c_void | ||
} | ||
|
||
// must not be null | ||
unsafe fn call_from_raw_ptr(raw_ptr: *mut ::libc::c_void, arg: &T) { | ||
unsafe fn call_from_raw_ptr(raw_ptr: *mut ::std::os::raw::c_void, arg: &T) { | ||
let callback = &mut *(raw_ptr as *mut Box<FnMut(&T)>); | ||
callback(arg); | ||
} | ||
|
@@ -126,7 +121,7 @@ pub struct Port { object: Object } | |
/// let client = coremidi::Client::new("example-client").unwrap(); | ||
/// let output_port = client.output_port("example-port").unwrap(); | ||
/// let destination = coremidi::Destination::from_index(0).unwrap(); | ||
/// let packets = coremidi::PacketBuffer::from_data(0, vec![0x90, 0x40, 0x7f]); | ||
/// let packets = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]); | ||
/// output_port.send(&destination, &packets).unwrap(); | ||
/// ``` | ||
#[derive(Debug)] | ||
|
@@ -217,12 +212,37 @@ pub struct VirtualDestination { | |
#[derive(PartialEq)] | ||
pub struct Device { object: Object } | ||
|
||
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))] | ||
type AlignmentMarker = [u32; 0]; // ensures 4-byte alignment (on ARM) | ||
|
||
|
||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
type AlignmentMarker = [u8; 0]; // unaligned | ||
|
||
/// A [list of MIDI events](https://developer.apple.com/reference/coremidi/midipacketlist) being received from, or being sent to, one endpoint. | ||
/// | ||
#[derive(PartialEq)] | ||
pub struct PacketList(*const MIDIPacketList); | ||
#[repr(C)] | ||
pub struct PacketList { | ||
// NOTE: This type must only exist in the form of immutable references | ||
// pointing to valid instances of MIDIPacketList. | ||
// This type must NOT implement `Copy`! | ||
inner: PacketListInner, | ||
_do_not_construct: AlignmentMarker | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a convention. I just did this to solve two problems at once, namely (1) to have the correct alignment and (2) to give a strong hint that any code that would try to construct an instance of this type would be wrong (but this is only relevant within the library, not for users of the library). I can rename if you prefer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not feeling strong about this renaming, but given that it is not exposed to the public through the API, (1) is the main point to hint, so the proposed name looks more intuitive to me. You can focus on other comments first, and if having enough time do this ;-) |
||
} | ||
|
||
#[repr(packed)] | ||
struct PacketListInner { | ||
num_packets: u32, | ||
data: [MIDIPacket; 0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if the differentiation between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
} | ||
|
||
mod coremidi_sys_ext; | ||
impl PacketList { | ||
/// For internal usage only. | ||
/// Requires this instance to actually point to a valid MIDIPacketList | ||
unsafe fn as_ptr(&self) -> *mut MIDIPacketList { | ||
self as *const PacketList as *mut PacketList as *mut MIDIPacketList | ||
} | ||
} | ||
|
||
mod object; | ||
mod devices; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about other architectures than arm or x86 ? would it be possible to have the unaligned case for all the non arm-aarch64 architectures ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be possible. I'm just not sure wheter "unaligned" is the right default, so I did it in such a way that it would error on other architectures. Is macOS/iOS even available for other architectures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I guess that failing in case of unknown architecture is an acceptable solution here.