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};
/// An object used for moving data of any type in or out of the client.
///
/// Ports may be connected in various ways.
///
/// Each port has a short name. The port's full name contains the name of the client
/// concatenated with a colon (:) followed by its short name. The jack_port_name_size()
/// is the maximum length of this full name. Exceeding that will cause port
/// registration to fail and return `ProgrammerError`.
#[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
        }
    }
    /// Modify a port's short name. May be called at any time.
    ///
    /// If the resulting full name
    /// (including the "client_name:" prefix) is longer than jack_port_name_size(), it will
    /// be truncated.
    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(())
        }
    }
    /// Get the name of a port (short or long, determined by the `short` argument).
    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())
        }
    }
    /// Get the type string of a port.
    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())
        }
    }
    /// Get the raw pointer to the name of a port.
    ///
    /// # Safety
    ///
    /// This function is **not** intended for external consumption.
    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)
        }
    }
    /// Get the JackPortFlags of the port.
    pub fn get_flags(&self) -> JackPortFlags {
        let flags = unsafe { jack_port_flags(self.ptr) };
        JackPortFlags::from_bits_truncate(flags as ::libc::c_ulong)
    }
}