sqa_jack/
port.rs

1use libc;
2use errors::{JackError, JackResult};
3use super::{JackPortFlags, JackPortPtr, str_to_cstr};
4use std::borrow::Cow;
5use std::ffi::CStr;
6use jack_sys::{jack_port_set_name, jack_port_type, jack_port_flags,
7jack_port_short_name, jack_port_name};
8/// An object used for moving data of any type in or out of the client.
9///
10/// Ports may be connected in various ways.
11///
12/// Each port has a short name. The port's full name contains the name of the client
13/// concatenated with a colon (:) followed by its short name. The jack_port_name_size()
14/// is the maximum length of this full name. Exceeding that will cause port
15/// registration to fail and return `ProgrammerError`.
16#[derive(Copy, Clone, Debug)]
17pub struct JackPort {
18    ptr: JackPortPtr,
19}
20unsafe impl Send for JackPort {}
21
22impl JackPort {
23    pub fn as_ptr(&self) -> JackPortPtr {
24        self.ptr
25    }
26    pub unsafe fn from_ptr(ptr: JackPortPtr) -> Self {
27        JackPort {
28            ptr: ptr
29        }
30    }
31    /// Modify a port's short name. May be called at any time.
32    ///
33    /// If the resulting full name
34    /// (including the "client_name:" prefix) is longer than jack_port_name_size(), it will
35    /// be truncated.
36    pub fn set_short_name(&mut self, name: &str) -> JackResult<()> {
37        let code = unsafe {
38            let name = str_to_cstr(name)?;
39            jack_port_set_name(self.ptr, name.as_ptr())
40        };
41        if code != 0 {
42            Err(JackError::ProgrammerError)?
43        }
44        else {
45            Ok(())
46        }
47    }
48    /// Get the name of a port (short or long, determined by the `short` argument).
49    pub fn get_name(&self, short: bool) -> JackResult<Cow<str>> {
50        unsafe {
51            let ptr = self.get_name_raw(short)?;
52            Ok(CStr::from_ptr(ptr).to_string_lossy())
53        }
54    }
55    /// Get the type string of a port.
56    pub fn get_type(&self) -> JackResult<Cow<str>> {
57        unsafe {
58            let ptr = jack_port_type(self.ptr);
59            if ptr.is_null() {
60                Err(JackError::InvalidPort)?
61            }
62            Ok(CStr::from_ptr(ptr).to_string_lossy())
63        }
64    }
65    /// Get the raw pointer to the name of a port.
66    ///
67    /// # Safety
68    ///
69    /// This function is **not** intended for external consumption.
70    pub unsafe fn get_name_raw(&self, short: bool) -> JackResult<*const libc::c_char> {
71        let ptr = if short {
72            jack_port_short_name(self.ptr)
73        }
74        else {
75            jack_port_name(self.ptr)
76        };
77        if ptr.is_null() {
78            Err(JackError::InvalidPort)?
79        }
80        else {
81            Ok(ptr)
82        }
83    }
84    /// Get the JackPortFlags of the port.
85    pub fn get_flags(&self) -> JackPortFlags {
86        let flags = unsafe { jack_port_flags(self.ptr) };
87        JackPortFlags::from_bits_truncate(flags as ::libc::c_ulong)
88    }
89}