Debian Codegraph @

third_party/rust/midir/src/backend/jack/wrappers.rs

raw
1 #![allow(non_upper_case_globals, dead_code)]
2
3 use std::{ptr, slice, str};
4 use std::ffi::{CStr, CString};
5 use std::ops::Index;
6
7 use super::libc::{c_void, size_t};
8
9 use super::jack_sys::{
10 jack_get_time,
11 jack_client_t,
12 jack_client_open,
13 jack_client_close,
14 jack_get_ports,
15 jack_port_t,
16 jack_port_register,
17 jack_port_unregister,
18 jack_port_name,
19 jack_free,
20 jack_activate,
21 jack_deactivate,
22 jack_nframes_t,
23 jack_set_process_callback,
24 jack_connect,
25 jack_port_get_buffer,
26 jack_midi_data_t,
27 jack_midi_get_event_count,
28 jack_midi_event_get,
29 jack_midi_event_t,
30 jack_midi_clear_buffer,
31 jack_midi_event_reserve,
32 jack_ringbuffer_t,
33 jack_ringbuffer_create,
34 jack_ringbuffer_read_space,
35 jack_ringbuffer_read,
36 jack_ringbuffer_free,
37 jack_ringbuffer_write,
38 };
39
40 pub const JACK_DEFAULT_MIDI_TYPE: &[u8] = b"8 bit raw midi\0";
41
42 bitflags! {
43 #[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
44 pub struct JackOpenOptions: u32 {
45 const NoStartServer = 1;
46 const UseExactName = 2;
47 const ServerName = 4;
48 const SessionID = 32;
49 }
50 }
51
52 bitflags! {
53 #[derive(Debug, Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
54 pub struct PortFlags: u32 {
55 const PortIsInput = 1;
56 const PortIsOutput = 2;
57 const PortIsPhysical = 4;
58 const PortCanMonitor = 8;
59 const PortIsTerminal = 16;
60 }
61 }
62
63 // TODO: hide this type
64 pub type ProcessCallback = extern "C" fn(nframes: jack_nframes_t, arg: *mut c_void) -> i32;
65
66 pub struct Client {
67 p: *mut jack_client_t
68 }
69
70 unsafe impl Send for Client {}
71
72 impl Client {
73 pub fn get_time() -> u64 {
74 unsafe { jack_get_time() }
75 }
76
77 pub fn open(name: &str, options: JackOpenOptions) -> Result<Client, ()> {
78 let c_name = CString::new(name).ok().expect("client name must not contain null bytes");
79 let result = unsafe { jack_client_open(c_name.as_ptr(), options.bits(), ptr::null_mut()) };
80 if result.is_null() {
81 Err(())
82 } else {
83 Ok(Client { p: result })
84 }
85 }
86
87 pub fn get_midi_ports(&self, flags: PortFlags) -> PortInfos {
88 let ports_ptr = unsafe { jack_get_ports(self.p, ptr::null_mut(), JACK_DEFAULT_MIDI_TYPE.as_ptr() as *const i8, flags.bits() as u64) };
89 let slice = if ports_ptr.is_null() {
90 &[]
91 } else {
92 unsafe {
93 let count = (0isize..).find(|i| (*ports_ptr.offset(*i)).is_null()).unwrap() as usize;
94 slice::from_raw_parts(ports_ptr, count)
95 }
96 };
97 PortInfos { p: slice }
98 }
99
100 pub fn register_midi_port(&mut self, name: &str, flags: PortFlags) -> Result<MidiPort, ()> {
101 let c_name = CString::new(name).ok().expect("port name must not contain null bytes");
102 let result = unsafe { jack_port_register(self.p, c_name.as_ptr(), JACK_DEFAULT_MIDI_TYPE.as_ptr() as *const i8, flags.bits() as u64, 0) };
103 if result.is_null() {
104 Err(())
105 } else {
106 Ok(MidiPort { p: result })
107 }
108 }
109
110 /// This can not be implemented in Drop, because it needs a reference
111 /// to the client. But it consumes the MidiPort.
112 pub fn unregister_midi_port(&mut self, client: MidiPort) {
113 unsafe { jack_port_unregister(self.p, client.p) };
114 }
115
116 pub fn activate(&mut self) {
117 unsafe { jack_activate(self.p) };
118 }
119
120 pub fn deactivate(&mut self) {
121 unsafe { jack_deactivate(self.p) };
122 }
123
124 /// The code in the supplied function must be suitable for real-time
125 /// execution. That means that it cannot call functions that might block
126 /// for a long time. This includes all I/O functions (disk, TTY, network),
127 /// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
128 /// pthread_join, pthread_cond_wait, etc, etc.
129 pub fn set_process_callback(&mut self, callback: ProcessCallback, data: *mut c_void) {
130 unsafe { jack_set_process_callback(self.p, Some(callback), data) };
131 }
132
133 pub fn connect(&mut self, source_port: &CStr, destination_port: &CStr) -> Result<(), ()> {
134 let rc = unsafe { jack_connect(self.p, source_port.as_ptr(), destination_port.as_ptr()) };
135 if rc == 0 {
136 Ok(())
137 } else {
138 Err(()) // TODO: maybe handle EEXIST explicitly
139 }
140 }
141 }
142
143 impl Drop for Client {
144 fn drop(&mut self) {
145 unsafe { jack_client_close(self.p) };
146 }
147 }
148
149 pub struct PortInfos<'a> {
150 p: &'a[*const i8],
151 }
152
153 unsafe impl<'a> Send for PortInfos<'a> {}
154
155 impl<'a> PortInfos<'a> {
156 pub fn count(&self) -> usize {
157 self.p.len()
158 }
159
160 pub fn get_c_name(&self, index: usize) -> &CStr {
161 let ptr = self.p[index];
162 unsafe { CStr::from_ptr(ptr) }
163 }
164 }
165
166 impl<'a> Index<usize> for PortInfos<'a> {
167 type Output = str;
168
169 fn index(&self, index: usize) -> &Self::Output {
170 let slice = self.get_c_name(index).to_bytes();
171 str::from_utf8(slice).ok().expect("Error converting port name to UTF8")
172 }
173 }
174
175 impl<'a> Drop for PortInfos<'a> {
176 fn drop(&mut self) {
177 if self.p.len() > 0 {
178 unsafe { jack_free(self.p.as_ptr() as *mut _) }
179 }
180 }
181 }
182
183 pub struct MidiPort {
184 p: *mut jack_port_t
185 }
186
187 unsafe impl Send for MidiPort {}
188
189 impl MidiPort {
190 pub fn get_name(&self) -> &CStr {
191 unsafe { CStr::from_ptr(jack_port_name(self.p)) }
192 }
193
194 pub fn get_midi_buffer(&self, nframes: jack_nframes_t) -> MidiBuffer {
195 let buf = unsafe { jack_port_get_buffer(self.p, nframes) };
196 MidiBuffer { p: buf }
197 }
198 }
199
200 pub struct MidiBuffer {
201 p: *mut c_void
202 }
203
204 impl MidiBuffer {
205 pub fn get_event_count(&self) -> u32 {
206 unsafe { jack_midi_get_event_count(self.p) }
207 }
208
209 pub unsafe fn get_event(&self, ev: *mut jack_midi_event_t, index: u32) {
210 jack_midi_event_get(ev, self.p, index);
211 }
212
213 pub fn clear(&mut self) {
214 unsafe { jack_midi_clear_buffer(self.p) }
215 }
216
217 pub fn event_reserve(&mut self, time: jack_nframes_t, data_size: usize) -> *mut jack_midi_data_t {
218 unsafe { jack_midi_event_reserve(self.p, time, data_size as size_t) }
219 }
220 }
221
222 pub struct Ringbuffer {
223 p: *mut jack_ringbuffer_t
224 }
225
226 unsafe impl Send for Ringbuffer {}
227
228 impl Ringbuffer {
229 pub fn new(size: usize) -> Ringbuffer {
230 let result = unsafe { jack_ringbuffer_create(size as size_t) };
231 Ringbuffer { p: result }
232 }
233
234 pub fn get_read_space(&self) -> usize {
235 unsafe { jack_ringbuffer_read_space(self.p) as usize }
236 }
237
238 pub fn read(&mut self, destination: *mut u8, count: usize) -> usize {
239 let bytes_read = unsafe { jack_ringbuffer_read(self.p, destination as *mut i8, count as size_t) };
240 bytes_read as usize
241 }
242
243 pub fn write(&mut self, source: &[u8]) -> usize {
244 unsafe { jack_ringbuffer_write(self.p, source.as_ptr() as *const i8, source.len() as size_t) as usize }
245 }
246 }
247
248 impl Drop for Ringbuffer{
249 fn drop(&mut self) {
250 unsafe { jack_ringbuffer_free(self.p) }
251 }
252 }