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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#![allow(non_upper_case_globals)]
use core_foundation_sys::base::OSStatus;
use coremidi_sys::{
kMIDIObjectType_Destination, kMIDIObjectType_Device, kMIDIObjectType_Entity,
kMIDIObjectType_ExternalDestination, kMIDIObjectType_ExternalDevice,
kMIDIObjectType_ExternalEntity, kMIDIObjectType_ExternalSource, kMIDIObjectType_Other,
kMIDIObjectType_Source, MIDIObjectRef, SInt32,
};
use std::fmt;
use crate::properties::{
BooleanProperty, IntegerProperty, Properties, PropertyGetter, PropertySetter, StringProperty,
};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum ObjectType {
Other,
Device,
Entity,
Source,
Destination,
ExternalDevice,
ExternalEntity,
ExternalSource,
ExternalDestination,
}
impl ObjectType {
pub fn from(value: i32) -> Result<ObjectType, i32> {
match value {
kMIDIObjectType_Other => Ok(ObjectType::Other),
kMIDIObjectType_Device => Ok(ObjectType::Device),
kMIDIObjectType_Entity => Ok(ObjectType::Entity),
kMIDIObjectType_Source => Ok(ObjectType::Source),
kMIDIObjectType_Destination => Ok(ObjectType::Destination),
kMIDIObjectType_ExternalDevice => Ok(ObjectType::ExternalDevice),
kMIDIObjectType_ExternalEntity => Ok(ObjectType::ExternalEntity),
kMIDIObjectType_ExternalSource => Ok(ObjectType::ExternalSource),
kMIDIObjectType_ExternalDestination => Ok(ObjectType::ExternalDestination),
unknown => Err(unknown),
}
}
}
/// A [MIDI Object](https://developer.apple.com/reference/coremidi/midiobjectref).
///
/// The base class of many CoreMIDI objects.
///
#[derive(PartialEq)]
pub struct Object(pub(crate) MIDIObjectRef);
impl Object {
/// Get the name for the object.
///
pub fn name(&self) -> Option<String> {
Properties::name().value_from(self).ok()
}
/// Get the unique id for the object.
///
pub fn unique_id(&self) -> Option<u32> {
Properties::unique_id()
.value_from(self)
.ok()
.map(|v: SInt32| v as u32)
}
/// Get the display name for the object.
///
pub fn display_name(&self) -> Option<String> {
Properties::display_name().value_from(self).ok()
}
/// Sets an object's string-type property.
///
pub fn set_property_string(&self, name: &str, value: &str) -> Result<(), OSStatus> {
StringProperty::new(name).set_value(self, value)
}
/// Gets an object's string-type property.
///
pub fn get_property_string(&self, name: &str) -> Result<String, OSStatus> {
StringProperty::new(name).value_from(self)
}
/// Sets an object's integer-type property.
///
pub fn set_property_integer(&self, name: &str, value: i32) -> Result<(), OSStatus> {
IntegerProperty::new(name).set_value(self, value)
}
/// Gets an object's integer-type property.
///
pub fn get_property_integer(&self, name: &str) -> Result<i32, OSStatus> {
IntegerProperty::new(name).value_from(self)
}
/// Sets an object's boolean-type property.
///
/// CoreMIDI treats booleans as integers (0/1) but this API uses native bool types
///
pub fn set_property_boolean(&self, name: &str, value: bool) -> Result<(), OSStatus> {
BooleanProperty::new(name).set_value(self, value)
}
/// Gets an object's boolean-type property.
///
/// CoreMIDI treats booleans as integers (0/1) but this API uses native bool types
///
pub fn get_property_boolean(&self, name: &str) -> Result<bool, OSStatus> {
BooleanProperty::new(name).value_from(self)
}
}
impl fmt::Debug for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Object({:x})", self.0 as usize)
}
}
#[cfg(test)]
mod tests {
use crate::object::ObjectType;
use coremidi_sys::{
kMIDIObjectType_Destination, kMIDIObjectType_Device, kMIDIObjectType_Entity,
kMIDIObjectType_ExternalDestination, kMIDIObjectType_ExternalDevice,
kMIDIObjectType_ExternalEntity, kMIDIObjectType_ExternalSource, kMIDIObjectType_Other,
kMIDIObjectType_Source,
};
#[test]
fn objecttype_from() {
assert_eq!(
ObjectType::from(kMIDIObjectType_Other),
Ok(ObjectType::Other)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_Device),
Ok(ObjectType::Device)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_Entity),
Ok(ObjectType::Entity)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_Source),
Ok(ObjectType::Source)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_Destination),
Ok(ObjectType::Destination)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_ExternalDevice),
Ok(ObjectType::ExternalDevice)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_ExternalEntity),
Ok(ObjectType::ExternalEntity)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_ExternalSource),
Ok(ObjectType::ExternalSource)
);
assert_eq!(
ObjectType::from(kMIDIObjectType_ExternalDestination),
Ok(ObjectType::ExternalDestination)
);
}
#[test]
fn objecttype_from_error() {
assert_eq!(ObjectType::from(0xffff_i32), Err(0xffff));
}
}
|