1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use libc;
use errors::{JackError, JackResult};
use super::{JackPortFlags, JackPortPtr, str_to_cstr};
use std::borrow::Cow;
use std::ffi::CStr;
use jack_sys::{jack_port_set_name, jack_port_type, jack_port_flags,
jack_port_short_name, jack_port_name};
#[derive(Copy, Clone, Debug)]
pub struct JackPort {
ptr: JackPortPtr,
}
unsafe impl Send for JackPort {}
impl JackPort {
pub fn as_ptr(&self) -> JackPortPtr {
self.ptr
}
pub unsafe fn from_ptr(ptr: JackPortPtr) -> Self {
JackPort {
ptr: ptr
}
}
pub fn set_short_name(&mut self, name: &str) -> JackResult<()> {
let code = unsafe {
let name = str_to_cstr(name)?;
jack_port_set_name(self.ptr, name.as_ptr())
};
if code != 0 {
Err(JackError::ProgrammerError)?
}
else {
Ok(())
}
}
pub fn get_name(&self, short: bool) -> JackResult<Cow<str>> {
unsafe {
let ptr = self.get_name_raw(short)?;
Ok(CStr::from_ptr(ptr).to_string_lossy())
}
}
pub fn get_type(&self) -> JackResult<Cow<str>> {
unsafe {
let ptr = jack_port_type(self.ptr);
if ptr.is_null() {
Err(JackError::InvalidPort)?
}
Ok(CStr::from_ptr(ptr).to_string_lossy())
}
}
pub unsafe fn get_name_raw(&self, short: bool) -> JackResult<*const libc::c_char> {
let ptr = if short {
jack_port_short_name(self.ptr)
}
else {
jack_port_name(self.ptr)
};
if ptr.is_null() {
Err(JackError::InvalidPort)?
}
else {
Ok(ptr)
}
}
pub fn get_flags(&self) -> JackPortFlags {
let flags = unsafe { jack_port_flags(self.ptr) };
JackPortFlags::from_bits_truncate(flags as ::libc::c_ulong)
}
}