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
90
91
92
93
94
95
96
use std::os::raw::{c_char, c_void, c_int};
use std::mem;
use std::default::Default;
use std::fmt;
use ffi;
pub type PmDeviceId = c_int;
pub type PortMidiStream = c_void;
pub type PmMessage = c_int;
pub type PmTimestamp = u32;
pub const PM_NO_DEVICE: PmDeviceId = -1;
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct PmEvent {
pub message: PmMessage,
pub timestamp: PmTimestamp,
}
impl Default for PmEvent {
fn default() -> Self {
PmEvent {
message: 0,
timestamp: 0,
}
}
}
#[repr(C)]
pub struct PmDeviceInfo {
pub struct_version: c_int,
pub interf: *const c_char,
pub name: *const c_char,
pub input: c_int,
pub output: c_int,
pub opened: c_int,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum PmError {
PmNoError = 0,
PmGotData = 1,
PmHostError = -10000,
PmInvalidDeviceId = -9999,
PmInsufficientMemory = -9998,
PmBufferTooSmall = -9997,
PmBufferOverflow = -9996,
PmBadPtr = -9995,
PmBadData = -9994,
PmInternalError = -9993,
PmBufferMaxSize = -9992,
}
impl fmt::Display for PmError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut host_error_text: [c_char; 1024] = [0; 1024];
let str_ptr = match self {
&PmError::PmHostError => unsafe {
ffi::Pm_GetHostErrorText(host_error_text.as_mut_ptr(),
host_error_text.len() as c_int);
host_error_text.as_ptr()
},
_ => unsafe { ffi::Pm_GetErrorText(*self) },
};
write!(f, "{}", ffi::ptr_to_string(str_ptr).unwrap())
}
}
pub trait MaybeError<T> {
fn try_from(err_code: T) -> Result<T, PmError>;
}
impl MaybeError<c_int> for PmError {
fn try_from(err_code: c_int) -> Result<c_int, PmError> {
match err_code {
-10_000...-9992 | 0 => unsafe { Err(mem::transmute(err_code)) },
_ => Ok(err_code),
}
}
}