Skip to content

Commit 244e14b

Browse files
Merge pull request #31 from Boscop/master
Use types from std::os::raw instead of libc where possible
2 parents 435e14a + e3beb2e commit 244e14b

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed

examples/sine_synth/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ vst2 = { git = "https://github.com/overdrivenpotato/rust-vst2" }
88

99
[lib]
1010
name = "sine_synth"
11-
crate-type = ["dylib"]
11+
crate-type = ["cdylib"]

examples/sine_synth/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#[macro_use] extern crate vst2;
22

33
use vst2::buffer::AudioBuffer;
4-
use vst2::plugin::{Category, Plugin, Info};
5-
use vst2::event::{Event};
4+
use vst2::plugin::{Category, Plugin, Info, CanDo};
5+
use vst2::event::Event;
6+
use vst2::api::Supported;
67

78
use std::f64::consts::PI;
89

@@ -137,6 +138,13 @@ impl Plugin for SineSynth {
137138
self.time += samples as f64 * per_sample;
138139
self.note_duration += samples as f64 * per_sample;
139140
}
141+
142+
fn can_do(&self, can_do: CanDo) -> Supported {
143+
match can_do {
144+
CanDo::ReceiveMidiEvent => Supported::Yes,
145+
_ => Supported::Maybe
146+
}
147+
}
140148
}
141149

142150
plugin_main!(SineSynth);

src/api.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
//! Structures and types for interfacing with the VST 2.4 API.
22
use std::mem;
33

4-
use libc::c_void;
4+
use std::os::raw::c_void;
55

66
use plugin::Plugin;
77
use self::consts::*;
88

99
/// Constant values
1010
#[allow(missing_docs)] // For obvious constants
1111
pub mod consts {
12-
use libc::size_t;
1312

14-
pub const MAX_PRESET_NAME_LEN: size_t = 24;
15-
pub const MAX_PARAM_STR_LEN: size_t = 32;
13+
pub const MAX_PRESET_NAME_LEN: usize = 24;
14+
pub const MAX_PARAM_STR_LEN: usize = 32;
1615
pub const MAX_LABEL: usize = 64;
1716
pub const MAX_SHORT_LABEL: usize = 8;
18-
pub const MAX_PRODUCT_STR_LEN: size_t = 64;
19-
pub const MAX_VENDOR_STR_LEN: size_t = 64;
17+
pub const MAX_PRODUCT_STR_LEN: usize = 64;
18+
pub const MAX_VENDOR_STR_LEN: usize = 64;
2019

2120
/// VST plugins are identified by a magic number. This corresponds to 0x56737450.
2221
pub const VST_MAGIC: i32 = ('V' as i32) << 24 |

src/editor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! All VST plugin editor related functionality.
22
3-
use libc::c_void;
3+
use std::os::raw::c_void;
44

55
/// Implemented by plugin editors.
66
#[allow(unused_variables)]

src/host.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{fmt, mem, ptr, slice};
77
use std::ffi::CString;
88

99
use libloading::Library;
10-
use libc::c_void;
10+
use std::os::raw::c_void;
1111

1212
use interfaces;
1313
use plugin::{self, Plugin, Info, Category};
@@ -339,7 +339,7 @@ impl<T: Host> PluginLoader<T> {
339339

340340
/// Call the VST entry point and retrieve a (possibly null) pointer.
341341
unsafe fn call_main(&mut self) -> *mut AEffect {
342-
load_pointer = Box::into_raw(Box::new(self.host.clone())) as *mut c_void;
342+
LOAD_POINTER = Box::into_raw(Box::new(self.host.clone())) as *mut c_void;
343343
(self.main)(callback_wrapper::<T>)
344344
}
345345

@@ -666,7 +666,7 @@ impl Plugin for PluginInstance {
666666
/// this is a rare situation as you normally won't have 2 seperate host instances loading at once.
667667
///
668668
/// [reserved field]: ../api/struct.AEffect.html#structfield.reserved1
669-
static mut load_pointer: *mut c_void = 0 as *mut c_void;
669+
static mut LOAD_POINTER: *mut c_void = 0 as *mut c_void;
670670

671671
/// Function passed to plugin to handle dispatching host opcodes.
672672
fn callback_wrapper<T: Host>(effect: *mut AEffect, opcode: i32, index: i32,
@@ -681,11 +681,11 @@ fn callback_wrapper<T: Host>(effect: *mut AEffect, opcode: i32, index: i32,
681681
let host = &mut *host.lock().unwrap();
682682

683683
interfaces::host_dispatch(host, effect, opcode, index, value, ptr, opt)
684-
// In this case, the plugin is still undergoing initialization and so `load_pointer` is
684+
// In this case, the plugin is still undergoing initialization and so `LOAD_POINTER` is
685685
// dereferenced
686686
} else {
687687
// Used only during the plugin initialization
688-
let host = load_pointer as *const Arc<Mutex<T>>;
688+
let host = LOAD_POINTER as *const Arc<Mutex<T>>;
689689
let host = &*host;
690690
let host = &mut *host.lock().unwrap();
691691

src/interfaces.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
33
#![doc(hidden)]
44

5-
use std::ffi::{CStr, CString};
65
use std::{mem, slice};
76

8-
use libc::{self, size_t, c_char, c_void};
7+
use std::os::raw::{c_char, c_void};
98

109
use buffer::AudioBuffer;
1110
use api::consts::*;
@@ -60,6 +59,20 @@ pub fn get_parameter(effect: *mut AEffect, index: i32) -> f32 {
6059
unsafe { (*effect).get_plugin() }.get_parameter(index)
6160
}
6261

62+
/// Copy a string into a destination buffer.
63+
///
64+
/// String will be cut at `max` characters.
65+
fn copy_string(dst: *mut c_void, src: &str, max: usize) {
66+
unsafe {
67+
use std::cmp::min;
68+
use libc::{c_void, memset, memcpy};
69+
70+
let dst = dst as *mut c_void;
71+
memset(dst, 0, max);
72+
memcpy(dst, src.as_ptr() as *const c_void, min(max, src.len()));
73+
}
74+
}
75+
6376
/// VST2.4 dispatch function. This function handles dispatching all opcodes to the vst plugin.
6477
pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr: *mut c_void, opt: f32) -> isize {
6578
use plugin::{CanDo, OpCode};
@@ -69,15 +82,6 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
6982
// Plugin handle
7083
let mut plugin = unsafe { (*effect).get_plugin() };
7184

72-
// Copy a string into the `ptr` buffer
73-
let copy_string = |string: &String, max: size_t| {
74-
unsafe {
75-
libc::strncpy(ptr as *mut c_char,
76-
CString::new(string.clone()).unwrap().as_ptr(),
77-
max);
78-
}
79-
};
80-
8185
match opcode {
8286
OpCode::Initialize => plugin.init(),
8387
OpCode::Shutdown => unsafe {
@@ -90,12 +94,12 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
9094
OpCode::SetCurrentPresetName => plugin.set_preset_name(read_string(ptr)),
9195
OpCode::GetCurrentPresetName => {
9296
let num = plugin.get_preset_num();
93-
copy_string(&plugin.get_preset_name(num), MAX_PRESET_NAME_LEN);
97+
copy_string(ptr, &plugin.get_preset_name(num), MAX_PRESET_NAME_LEN);
9498
}
9599

96-
OpCode::GetParameterLabel => copy_string(&plugin.get_parameter_label(index), MAX_PARAM_STR_LEN),
97-
OpCode::GetParameterDisplay => copy_string(&plugin.get_parameter_text(index), MAX_PARAM_STR_LEN),
98-
OpCode::GetParameterName => copy_string(&plugin.get_parameter_name(index), MAX_PARAM_STR_LEN),
100+
OpCode::GetParameterLabel => copy_string(ptr, &plugin.get_parameter_label(index), MAX_PARAM_STR_LEN),
101+
OpCode::GetParameterDisplay => copy_string(ptr, &plugin.get_parameter_text(index), MAX_PARAM_STR_LEN),
102+
OpCode::GetParameterName => copy_string(ptr, &plugin.get_parameter_name(index), MAX_PARAM_STR_LEN),
99103

100104
OpCode::SetSampleRate => plugin.set_sample_rate(opt),
101105
OpCode::SetBlockSize => plugin.set_block_size(value as i64),
@@ -184,7 +188,7 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
184188
OpCode::CanBeAutomated => return plugin.can_be_automated(index) as isize,
185189
OpCode::StringToParameter => return plugin.string_to_parameter(index, read_string(ptr)) as isize,
186190

187-
OpCode::GetPresetName => copy_string(&plugin.get_preset_name(index), MAX_PRESET_NAME_LEN),
191+
OpCode::GetPresetName => copy_string(ptr, &plugin.get_preset_name(index), MAX_PRESET_NAME_LEN),
188192

189193
OpCode::GetInputInfo => {
190194
if index >= 0 && index < plugin.get_info().inputs {
@@ -206,8 +210,8 @@ pub fn dispatch(effect: *mut AEffect, opcode: i32, index: i32, value: isize, ptr
206210
return plugin.get_info().category.into();
207211
}
208212

209-
OpCode::GetVendorName => copy_string(&plugin.get_info().vendor, MAX_VENDOR_STR_LEN),
210-
OpCode::GetProductName => copy_string(&plugin.get_info().name, MAX_PRODUCT_STR_LEN),
213+
OpCode::GetVendorName => copy_string(ptr, &plugin.get_info().vendor, MAX_VENDOR_STR_LEN),
214+
OpCode::GetProductName => copy_string(ptr, &plugin.get_info().name, MAX_PRODUCT_STR_LEN),
211215
OpCode::GetVendorVersion => return plugin.get_info().version as isize,
212216
OpCode::VendorSpecific => return plugin.vendor_specific(index, value, ptr, opt),
213217
OpCode::CanDo => {
@@ -265,15 +269,6 @@ pub fn host_dispatch(host: &mut Host,
265269
opt: f32) -> isize {
266270
use host::OpCode;
267271

268-
// Copy a string into the `ptr` buffer
269-
let copy_string = |string: &String, max: size_t| {
270-
unsafe {
271-
libc::strncpy(ptr as *mut c_char,
272-
CString::new(string.clone()).unwrap().as_ptr(),
273-
max);
274-
}
275-
};
276-
277272
match OpCode::from(opcode) {
278273
OpCode::Version => return 2400,
279274
OpCode::Automate => host.automate(index, opt),
@@ -287,8 +282,8 @@ pub fn host_dispatch(host: &mut Host,
287282
}
288283

289284
OpCode::GetVendorVersion => return host.get_info().0,
290-
OpCode::GetVendorString => copy_string(&host.get_info().1, MAX_VENDOR_STR_LEN),
291-
OpCode::GetProductString => copy_string(&host.get_info().2, MAX_PRODUCT_STR_LEN),
285+
OpCode::GetVendorString => copy_string(ptr, &host.get_info().1, MAX_VENDOR_STR_LEN),
286+
OpCode::GetProductString => copy_string(ptr, &host.get_info().2, MAX_PRODUCT_STR_LEN),
292287
OpCode::ProcessEvents => {
293288
let events: *const api::Events = ptr as *const api::Events;
294289

@@ -313,6 +308,8 @@ pub fn host_dispatch(host: &mut Host,
313308

314309
// Read a string from the `ptr` buffer
315310
fn read_string(ptr: *mut c_void) -> String {
311+
use std::ffi::CStr;
312+
316313
String::from_utf8_lossy(
317314
unsafe { CStr::from_ptr(ptr as *mut c_char).to_bytes() }
318315
).into_owned()

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn main<T: Plugin + Default>(callback: HostCallbackProc) -> *mut AEffect {
252252
mod tests {
253253
use std::ptr;
254254

255-
use libc::c_void;
255+
use std::os::raw::c_void;
256256

257257
use interfaces;
258258
use api::AEffect;

src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{mem, ptr};
44

5-
use libc::c_void;
5+
use std::os::raw::c_void;
66

77
use channels::ChannelInfo;
88
use host::{self, Host};
@@ -850,7 +850,7 @@ mod tests {
850850
/// This is a macro to allow you to specify attributes on the created struct.
851851
macro_rules! make_plugin {
852852
($($attr:meta) *) => {
853-
use libc::c_void;
853+
use std::os::raw::c_void;
854854

855855
use main;
856856
use api::AEffect;

0 commit comments

Comments
 (0)