Debian Codegraph @

third_party/rust/coremidi/src/properties.rs

raw
1 use std::mem::MaybeUninit;
2
3 use core_foundation::{
4 base::{CFGetRetainCount, CFIndex, CFTypeRef, OSStatus, TCFType},
5 string::{CFString, CFStringRef},
6 };
7
8 use coremidi_sys::*;
9
10 use crate::{object::Object, result_from_status, unit_result_from_status};
11
12 pub trait PropertyGetter<T> {
13 fn value_from(&self, object: &Object) -> Result<T, OSStatus>;
14 }
15
16 pub trait PropertySetter<T> {
17 fn set_value(&self, object: &Object, value: T) -> Result<(), OSStatus>;
18 }
19
20 /// Because Property structs can be constructed from strings that have been
21 /// passed in from the user or are constants CFStringRefs from CoreMidi, we
22 /// need to abstract over how we store their keys.
23 enum PropertyKeyStorage {
24 Owned(CFString),
25 Constant(CFStringRef),
26 }
27
28 impl PropertyKeyStorage {
29 /// Return a raw CFStringRef pointing to this property key
30 fn as_string_ref(&self) -> CFStringRef {
31 match self {
32 PropertyKeyStorage::Owned(owned) => owned.as_concrete_TypeRef(),
33 PropertyKeyStorage::Constant(constant) => *constant,
34 }
35 }
36
37 /// For checking the retain count when debugging
38 #[allow(dead_code)]
39 fn retain_count(&self) -> CFIndex {
40 match self {
41 PropertyKeyStorage::Owned(owned) => owned.retain_count(),
42 PropertyKeyStorage::Constant(constant) => unsafe {
43 CFGetRetainCount(*constant as CFTypeRef)
44 },
45 }
46 }
47 }
48
49 /// A MIDI object property which value is an String
50 ///
51 pub struct StringProperty(PropertyKeyStorage);
52
53 impl StringProperty {
54 pub fn new(name: &str) -> Self {
55 StringProperty(PropertyKeyStorage::Owned(CFString::new(name)))
56 }
57
58 /// Note: Should only be used internally with predefined CoreMidi constants,
59 /// since it does not bump the retain count of the CFStringRef.
60 fn from_constant_string_ref(string_ref: CFStringRef) -> Self {
61 StringProperty(PropertyKeyStorage::Constant(string_ref))
62 }
63 }
64
65 impl<T> PropertyGetter<T> for StringProperty
66 where
67 T: From<String>,
68 {
69 fn value_from(&self, object: &Object) -> Result<T, OSStatus> {
70 let property_key = self.0.as_string_ref();
71 let mut string_ref = MaybeUninit::uninit();
72 let status =
73 unsafe { MIDIObjectGetStringProperty(object.0, property_key, string_ref.as_mut_ptr()) };
74 result_from_status(status, || {
75 let string_ref = unsafe { string_ref.assume_init() };
76 if string_ref.is_null() {
77 return "".to_string().into();
78 };
79 let cf_string: CFString = unsafe { TCFType::wrap_under_create_rule(string_ref) };
80 cf_string.to_string().into()
81 })
82 }
83 }
84
85 impl<'a, T> PropertySetter<T> for StringProperty
86 where
87 T: Into<String>,
88 {
89 fn set_value(&self, object: &Object, value: T) -> Result<(), OSStatus> {
90 let property_key = self.0.as_string_ref();
91 let value: String = value.into();
92 let string = CFString::new(&value);
93 let string_ref = string.as_concrete_TypeRef();
94 let status = unsafe { MIDIObjectSetStringProperty(object.0, property_key, string_ref) };
95 unit_result_from_status(status)
96 }
97 }
98
99 /// A MIDI object property which value is an Integer
100 ///
101 pub struct IntegerProperty(PropertyKeyStorage);
102
103 impl IntegerProperty {
104 pub fn new(name: &str) -> Self {
105 IntegerProperty(PropertyKeyStorage::Owned(CFString::new(name)))
106 }
107
108 /// Note: Should only be used internally with predefined CoreMidi constants,
109 /// since it does not bump the retain count of the CFStringRef.
110 fn from_constant_string_ref(string_ref: CFStringRef) -> Self {
111 IntegerProperty(PropertyKeyStorage::Constant(string_ref))
112 }
113 }
114
115 impl<T> PropertyGetter<T> for IntegerProperty
116 where
117 T: From<SInt32>,
118 {
119 fn value_from(&self, object: &Object) -> Result<T, OSStatus> {
120 let property_key = self.0.as_string_ref();
121 let mut value = MaybeUninit::uninit();
122 let status =
123 unsafe { MIDIObjectGetIntegerProperty(object.0, property_key, value.as_mut_ptr()) };
124 result_from_status(status, || {
125 let value = unsafe { value.assume_init() };
126 value.into()
127 })
128 }
129 }
130
131 impl<T> PropertySetter<T> for IntegerProperty
132 where
133 T: Into<SInt32>,
134 {
135 fn set_value(&self, object: &Object, value: T) -> Result<(), OSStatus> {
136 let property_key = self.0.as_string_ref();
137 let status = unsafe { MIDIObjectSetIntegerProperty(object.0, property_key, value.into()) };
138 unit_result_from_status(status)
139 }
140 }
141
142 /// A MIDI object property which value is a Boolean
143 ///
144 pub struct BooleanProperty(IntegerProperty);
145
146 impl BooleanProperty {
147 pub fn new(name: &str) -> Self {
148 BooleanProperty(IntegerProperty::new(name))
149 }
150
151 /// Note: Should only be used internally with predefined CoreMidi constants,
152 /// since it does not bump the retain count of the CFStringRef.
153 fn from_constant_string_ref(string_ref: CFStringRef) -> Self {
154 BooleanProperty(IntegerProperty::from_constant_string_ref(string_ref))
155 }
156 }
157
158 impl<T> PropertyGetter<T> for BooleanProperty
159 where
160 T: From<bool>,
161 {
162 fn value_from(&self, object: &Object) -> Result<T, OSStatus> {
163 self.0
164 .value_from(object)
165 .map(|value: SInt32| (value == 1).into())
166 }
167 }
168
169 impl<T> PropertySetter<T> for BooleanProperty
170 where
171 T: Into<bool>,
172 {
173 fn set_value(&self, object: &Object, value: T) -> Result<(), OSStatus> {
174 let value: SInt32 = if value.into() { 1 } else { 0 };
175 self.0.set_value(object, value)
176 }
177 }
178
179 /// The set of properties that might be available for MIDI objects.
180 ///
181 pub struct Properties;
182
183 impl Properties {
184 /// See [kMIDIPropertyName](https://developer.apple.com/reference/coremidi/kmidipropertyname)
185 pub fn name() -> StringProperty {
186 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyName })
187 }
188
189 /// See [kMIDIPropertyManufacturer](https://developer.apple.com/reference/coremidi/kmidipropertymanufacturer)
190 pub fn manufacturer() -> StringProperty {
191 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyManufacturer })
192 }
193
194 /// See [kMIDIPropertyModel](https://developer.apple.com/reference/coremidi/kmidipropertymodel)
195 pub fn model() -> StringProperty {
196 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyModel })
197 }
198
199 /// See [kMIDIPropertyUniqueID](https://developer.apple.com/reference/coremidi/kmidipropertyuniqueid)
200 pub fn unique_id() -> IntegerProperty {
201 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyUniqueID })
202 }
203
204 /// See [kMIDIPropertyDeviceID](https://developer.apple.com/reference/coremidi/kmidipropertydeviceid)
205 pub fn device_id() -> IntegerProperty {
206 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyDeviceID })
207 }
208
209 /// See [kMIDIPropertyReceiveChannels](https://developer.apple.com/reference/coremidi/kmidipropertyreceivechannels)
210 pub fn receive_channels() -> IntegerProperty {
211 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceiveChannels })
212 }
213
214 /// See [kMIDIPropertyTransmitChannels](https://developer.apple.com/reference/coremidi/kmidipropertytransmitchannels)
215 pub fn transmit_channels() -> IntegerProperty {
216 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitChannels })
217 }
218
219 /// See [kMIDIPropertyMaxSysExSpeed](https://developer.apple.com/reference/coremidi/kmidipropertymaxsysexspeed)
220 pub fn max_sysex_speed() -> IntegerProperty {
221 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyMaxSysExSpeed })
222 }
223
224 /// See [kMIDIPropertyAdvanceScheduleTimeMuSec](https://developer.apple.com/reference/coremidi/kMIDIPropertyAdvanceScheduleTimeMuSec)
225 pub fn advance_schedule_time_musec() -> IntegerProperty {
226 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyAdvanceScheduleTimeMuSec })
227 }
228
229 /// See [kMIDIPropertyIsEmbeddedEntity](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsEmbeddedEntity)
230 pub fn is_embedded_entity() -> BooleanProperty {
231 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsEmbeddedEntity })
232 }
233
234 /// See [kMIDIPropertyIsBroadcast](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsBroadcast)
235 pub fn is_broadcast() -> BooleanProperty {
236 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsBroadcast })
237 }
238
239 /// See [kMIDIPropertySingleRealtimeEntity](https://developer.apple.com/reference/coremidi/kMIDIPropertySingleRealtimeEntity)
240 pub fn single_realtime_entity() -> IntegerProperty {
241 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertySingleRealtimeEntity })
242 }
243
244 /// See [kMIDIPropertyConnectionUniqueID](https://developer.apple.com/reference/coremidi/kMIDIPropertyConnectionUniqueID)
245 pub fn connection_unique_id() -> IntegerProperty {
246 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyConnectionUniqueID })
247 }
248
249 /// See [kMIDIPropertyOffline](https://developer.apple.com/reference/coremidi/kMIDIPropertyOffline)
250 pub fn offline() -> BooleanProperty {
251 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyOffline })
252 }
253
254 /// See [kMIDIPropertyPrivate](https://developer.apple.com/reference/coremidi/kMIDIPropertyPrivate)
255 pub fn private() -> BooleanProperty {
256 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyPrivate })
257 }
258
259 /// See [kMIDIPropertyDriverOwner](https://developer.apple.com/reference/coremidi/kMIDIPropertyDriverOwner)
260 pub fn driver_owner() -> StringProperty {
261 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyDriverOwner })
262 }
263
264 // /// See [kMIDIPropertyNameConfiguration](https://developer.apple.com/reference/coremidi/kMIDIPropertyNameConfiguration)
265 // pub fn name_configuration() -> Property { unsafe { Property(kMIDIPropertyNameConfiguration) } }
266
267 // /// See [kMIDIPropertyImage](https://developer.apple.com/reference/coremidi/kMIDIPropertyImage)
268 // pub fn image() -> Property { unsafe { Property(kMIDIPropertyImage) } }
269
270 /// See [kMIDIPropertyDriverVersion](https://developer.apple.com/reference/coremidi/kMIDIPropertyDriverVersion)
271 pub fn driver_version() -> IntegerProperty {
272 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyDriverVersion })
273 }
274
275 /// See [kMIDIPropertySupportsGeneralMIDI](https://developer.apple.com/reference/coremidi/kMIDIPropertySupportsGeneralMIDI)
276 pub fn supports_general_midi() -> BooleanProperty {
277 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertySupportsGeneralMIDI })
278 }
279
280 /// See [kMIDIPropertySupportsMMC](https://developer.apple.com/reference/coremidi/kMIDIPropertySupportsMMC)
281 pub fn supports_mmc() -> BooleanProperty {
282 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertySupportsMMC })
283 }
284
285 /// See [kMIDIPropertyCanRoute](https://developer.apple.com/reference/coremidi/kMIDIPropertyCanRoute)
286 pub fn can_route() -> BooleanProperty {
287 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyCanRoute })
288 }
289
290 /// See [kMIDIPropertyReceivesClock](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesClock)
291 pub fn receives_clock() -> BooleanProperty {
292 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesClock })
293 }
294
295 /// See [kMIDIPropertyReceivesMTC](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesMTC)
296 pub fn receives_mtc() -> BooleanProperty {
297 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesMTC })
298 }
299
300 /// See [kMIDIPropertyReceivesNotes](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesNotes)
301 pub fn receives_notes() -> BooleanProperty {
302 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesNotes })
303 }
304
305 /// See [kMIDIPropertyReceivesProgramChanges](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesProgramChanges)
306 pub fn receives_program_changes() -> BooleanProperty {
307 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesProgramChanges })
308 }
309
310 /// See [kMIDIPropertyReceivesBankSelectMSB](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesBankSelectMSB)
311 pub fn receives_bank_select_msb() -> BooleanProperty {
312 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesBankSelectMSB })
313 }
314
315 /// See [kMIDIPropertyReceivesBankSelectLSB](https://developer.apple.com/reference/coremidi/kMIDIPropertyReceivesBankSelectLSB)
316 pub fn receives_bank_select_lsb() -> BooleanProperty {
317 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyReceivesBankSelectLSB })
318 }
319
320 /// See [kMIDIPropertyTransmitsBankSelectMSB](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsBankSelectMSB)
321 pub fn transmits_bank_select_msb() -> BooleanProperty {
322 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsBankSelectMSB })
323 }
324
325 /// See [kMIDIPropertyTransmitsBankSelectLSB](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsBankSelectLSB)
326 pub fn transmits_bank_select_lsb() -> BooleanProperty {
327 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsBankSelectLSB })
328 }
329
330 /// See [kMIDIPropertyTransmitsClock](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsClock)
331 pub fn transmits_clock() -> BooleanProperty {
332 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsClock })
333 }
334
335 /// See [kMIDIPropertyTransmitsMTC](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsMTC)
336 pub fn transmits_mtc() -> BooleanProperty {
337 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsMTC })
338 }
339
340 /// See [kMIDIPropertyTransmitsNotes](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsNotes)
341 pub fn transmits_notes() -> BooleanProperty {
342 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsNotes })
343 }
344
345 /// See [kMIDIPropertyTransmitsProgramChanges](https://developer.apple.com/reference/coremidi/kMIDIPropertyTransmitsProgramChanges)
346 pub fn transmits_program_changes() -> BooleanProperty {
347 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyTransmitsProgramChanges })
348 }
349
350 /// See [kMIDIPropertyPanDisruptsStereo](https://developer.apple.com/reference/coremidi/kMIDIPropertyPanDisruptsStereo)
351 pub fn pan_disrupts_stereo() -> BooleanProperty {
352 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyPanDisruptsStereo })
353 }
354
355 /// See [kMIDIPropertyIsSampler](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsSampler)
356 pub fn is_sampler() -> BooleanProperty {
357 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsSampler })
358 }
359
360 /// See [kMIDIPropertyIsDrumMachine](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsDrumMachine)
361 pub fn is_drum_machine() -> BooleanProperty {
362 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsDrumMachine })
363 }
364
365 /// See [kMIDIPropertyIsMixer](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsMixer)
366 pub fn is_mixer() -> BooleanProperty {
367 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsMixer })
368 }
369
370 /// See [kMIDIPropertyIsEffectUnit](https://developer.apple.com/reference/coremidi/kMIDIPropertyIsEffectUnit)
371 pub fn is_effect_unit() -> BooleanProperty {
372 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertyIsEffectUnit })
373 }
374
375 /// See [kMIDIPropertyMaxReceiveChannels](https://developer.apple.com/reference/coremidi/kMIDIPropertyMaxReceiveChannels)
376 pub fn max_receive_channels() -> IntegerProperty {
377 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyMaxReceiveChannels })
378 }
379
380 /// See [kMIDIPropertyMaxTransmitChannels](https://developer.apple.com/reference/coremidi/kMIDIPropertyMaxTransmitChannels)
381 pub fn max_transmit_channels() -> IntegerProperty {
382 IntegerProperty::from_constant_string_ref(unsafe { kMIDIPropertyMaxTransmitChannels })
383 }
384
385 /// See [kMIDIPropertyDriverDeviceEditorApp](https://developer.apple.com/reference/coremidi/kMIDIPropertyDriverDeviceEditorApp)
386 pub fn driver_device_editor_app() -> StringProperty {
387 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyDriverDeviceEditorApp })
388 }
389
390 /// See [kMIDIPropertySupportsShowControl](https://developer.apple.com/reference/coremidi/kMIDIPropertySupportsShowControl)
391 pub fn supports_show_control() -> BooleanProperty {
392 BooleanProperty::from_constant_string_ref(unsafe { kMIDIPropertySupportsShowControl })
393 }
394
395 /// See [kMIDIPropertyDisplayName](https://developer.apple.com/reference/coremidi/kMIDIPropertyDisplayName)
396 pub fn display_name() -> StringProperty {
397 StringProperty::from_constant_string_ref(unsafe { kMIDIPropertyDisplayName })
398 }
399 }
400
401 #[cfg(test)]
402 mod tests {
403 use super::*;
404
405 use crate::{endpoints::destinations::VirtualDestination, Client};
406
407 const NAME_ORIG: &str = "A";
408
409 fn setup() -> (Client, VirtualDestination) {
410 let client = Client::new("Test Client").unwrap();
411 let dest = client.virtual_destination(NAME_ORIG, |_| ()).unwrap();
412 (client, dest)
413 }
414
415 mod string {
416 use super::*;
417
418 const NAME_MODIFIED: &str = "B";
419
420 // Test getting the original value of the "name" property
421 fn check_get_original(property: &StringProperty, dest: &VirtualDestination) {
422 let name: String = property.value_from(dest).unwrap();
423
424 assert_eq!(name, NAME_ORIG);
425 }
426
427 // Test setting then getting the "name" property
428 fn check_roundtrip(property: &StringProperty, dest: &VirtualDestination) {
429 property.set_value(dest, NAME_MODIFIED).unwrap();
430 let name: String = property.value_from(dest).unwrap();
431
432 assert_eq!(name, NAME_MODIFIED);
433 }
434
435 #[test]
436 fn test_from_constant() {
437 let (_client, dest) = setup();
438 let property = Properties::name();
439
440 check_get_original(&property, &dest);
441 check_roundtrip(&property, &dest);
442 }
443
444 #[test]
445 fn test_new() {
446 let (_client, dest) = setup();
447 // "name" is the value of the CoreMidi constant kMIDIPropertyName
448 let property = StringProperty::new("name");
449
450 check_get_original(&property, &dest);
451 check_roundtrip(&property, &dest);
452 }
453 }
454
455 mod integer {
456 use super::*;
457
458 const ADVANCED_SCHEDULE_TIME: i32 = 44;
459
460 #[test]
461 fn test_not_set() {
462 let (_client, dest) = setup();
463 // Is not set by default for Virtual Destinations
464 let property = Properties::advance_schedule_time_musec();
465
466 let value: Result<i32, _> = property.value_from(&dest);
467
468 assert!(value.is_err())
469 }
470
471 #[test]
472 fn test_roundtrip() {
473 let (_client, dest) = setup();
474 let property = Properties::advance_schedule_time_musec();
475
476 property.set_value(&dest, ADVANCED_SCHEDULE_TIME).unwrap();
477 let num: i32 = property.value_from(&dest).unwrap();
478
479 assert_eq!(num, ADVANCED_SCHEDULE_TIME);
480 }
481 }
482
483 mod boolean {
484 use super::*;
485
486 #[test]
487 fn test_not_set() {
488 let (_client, dest) = setup();
489 // Not set by default on Virtual Destinations
490 let property = Properties::transmits_program_changes();
491
492 let value: Result<bool, _> = property.value_from(&dest);
493
494 assert!(value.is_err())
495 }
496
497 #[test]
498 fn test_roundtrip() {
499 let (_client, dest) = setup();
500 let property = Properties::private();
501
502 property.set_value(&dest, true).unwrap();
503 let value: bool = property.value_from(&dest).unwrap();
504
505 assert_eq!(value, true);
506 }
507 }
508 }