Debian Codegraph @

third_party/rust/coremidi/src/notifications.rs

raw
1 #![allow(non_upper_case_globals)]
2 #![allow(clippy::unnecessary_cast)]
3
4 use core_foundation::base::{OSStatus, TCFType};
5 use core_foundation::string::{CFString, CFStringRef};
6
7 use coremidi_sys::{
8 kMIDIMsgIOError, kMIDIMsgObjectAdded, kMIDIMsgObjectRemoved, kMIDIMsgPropertyChanged,
9 kMIDIMsgSerialPortOwnerChanged, kMIDIMsgSetupChanged, kMIDIMsgThruConnectionsChanged,
10 MIDIIOErrorNotification, MIDINotification, MIDIObjectAddRemoveNotification,
11 MIDIObjectPropertyChangeNotification,
12 };
13
14 use crate::devices::Device;
15 use crate::object::{Object, ObjectType};
16
17 #[derive(Debug, PartialEq)]
18 pub struct AddedRemovedInfo {
19 pub parent: Object,
20 pub parent_type: ObjectType,
21 pub child: Object,
22 pub child_type: ObjectType,
23 }
24
25 #[derive(Debug, PartialEq)]
26 pub struct PropertyChangedInfo {
27 pub object: Object,
28 pub object_type: ObjectType,
29 pub property_name: String,
30 }
31
32 #[derive(Debug, PartialEq)]
33 pub struct IoErrorInfo {
34 pub driver_device: Device,
35 pub error_code: OSStatus,
36 }
37
38 /// A message describing a system state change.
39 /// See [MIDINotification](https://developer.apple.com/reference/coremidi/midinotification).
40 ///
41 #[derive(Debug, PartialEq)]
42 pub enum Notification {
43 SetupChanged,
44 ObjectAdded(AddedRemovedInfo),
45 ObjectRemoved(AddedRemovedInfo),
46 PropertyChanged(PropertyChangedInfo),
47 ThruConnectionsChanged,
48 SerialPortOwnerChanged,
49 IoError(IoErrorInfo),
50 }
51
52 impl Notification {
53 pub fn from(notification: &MIDINotification) -> Result<Notification, OSStatus> {
54 match notification.messageID as ::std::os::raw::c_uint {
55 kMIDIMsgSetupChanged => Ok(Notification::SetupChanged),
56 kMIDIMsgObjectAdded | kMIDIMsgObjectRemoved => {
57 Self::from_object_added_removed(notification)
58 }
59 kMIDIMsgPropertyChanged => Self::from_property_changed(notification),
60 kMIDIMsgThruConnectionsChanged => Ok(Notification::ThruConnectionsChanged),
61 kMIDIMsgSerialPortOwnerChanged => Ok(Notification::SerialPortOwnerChanged),
62 kMIDIMsgIOError => Ok(Self::from_io_error(notification)),
63 unknown => Err(unknown as OSStatus),
64 }
65 }
66
67 fn from_object_added_removed(
68 notification: &MIDINotification,
69 ) -> Result<Notification, OSStatus> {
70 let add_remove_notification =
71 unsafe { &*(notification as *const _ as *const MIDIObjectAddRemoveNotification) };
72 let parent_type = ObjectType::from(add_remove_notification.parentType);
73 let child_type = ObjectType::from(add_remove_notification.childType);
74 match (parent_type, child_type) {
75 (Ok(parent_type), Ok(child_type)) => {
76 let add_remove_info = AddedRemovedInfo {
77 parent: Object(add_remove_notification.parent),
78 parent_type,
79 child: Object(add_remove_notification.child),
80 child_type,
81 };
82 match notification.messageID as ::std::os::raw::c_uint {
83 kMIDIMsgObjectAdded => Ok(Notification::ObjectAdded(add_remove_info)),
84 kMIDIMsgObjectRemoved => Ok(Notification::ObjectRemoved(add_remove_info)),
85 _ => unreachable!(),
86 }
87 }
88 _ => Err(notification.messageID as OSStatus),
89 }
90 }
91
92 fn from_property_changed(notification: &MIDINotification) -> Result<Notification, i32> {
93 let property_changed_notification =
94 unsafe { &*(notification as *const _ as *const MIDIObjectPropertyChangeNotification) };
95 match ObjectType::from(property_changed_notification.objectType) {
96 Ok(object_type) => {
97 let property_name = {
98 let name_ref: CFStringRef = property_changed_notification.propertyName;
99 let name: CFString = unsafe { TCFType::wrap_under_get_rule(name_ref) };
100 name.to_string()
101 };
102 let property_changed_info = PropertyChangedInfo {
103 object: Object(property_changed_notification.object),
104 object_type,
105 property_name,
106 };
107 Ok(Notification::PropertyChanged(property_changed_info))
108 }
109 Err(_) => Err(notification.messageID as i32),
110 }
111 }
112
113 fn from_io_error(notification: &MIDINotification) -> Notification {
114 let io_error_notification =
115 unsafe { &*(notification as *const _ as *const MIDIIOErrorNotification) };
116 let io_error_info = IoErrorInfo {
117 driver_device: Device {
118 object: Object(io_error_notification.driverDevice),
119 },
120 error_code: io_error_notification.errorCode,
121 };
122 Notification::IoError(io_error_info)
123 }
124 }
125
126 #[cfg(test)]
127 mod tests {
128
129 use core_foundation::base::{OSStatus, TCFType};
130 use core_foundation::string::CFString;
131
132 use coremidi_sys::{
133 kMIDIMsgIOError, kMIDIMsgObjectAdded, kMIDIMsgObjectRemoved, kMIDIMsgPropertyChanged,
134 kMIDIMsgSerialPortOwnerChanged, kMIDIMsgSetupChanged, kMIDIMsgThruConnectionsChanged,
135 kMIDIObjectType_Device, kMIDIObjectType_Other, MIDIIOErrorNotification, MIDINotification,
136 MIDINotificationMessageID, MIDIObjectAddRemoveNotification,
137 MIDIObjectPropertyChangeNotification, MIDIObjectRef,
138 };
139
140 use crate::devices::Device;
141 use crate::notifications::{AddedRemovedInfo, IoErrorInfo, Notification, PropertyChangedInfo};
142 use crate::object::{Object, ObjectType};
143
144 #[test]
145 fn notification_from_error() {
146 let notification_raw = MIDINotification {
147 messageID: 0xffff as MIDINotificationMessageID,
148 messageSize: 8,
149 };
150 let notification = Notification::from(&notification_raw);
151 assert!(notification.is_err());
152 assert_eq!(notification.err().unwrap(), 0xffff as i32);
153 }
154
155 #[test]
156 fn notification_from_setup_changed() {
157 let notification_raw = MIDINotification {
158 messageID: kMIDIMsgSetupChanged as MIDINotificationMessageID,
159 messageSize: 8,
160 };
161 let notification = Notification::from(&notification_raw);
162 assert!(notification.is_ok());
163 assert_eq!(notification.unwrap(), Notification::SetupChanged);
164 }
165
166 #[test]
167 fn notification_from_object_added() {
168 let notification_raw = MIDIObjectAddRemoveNotification {
169 messageID: kMIDIMsgObjectAdded as MIDINotificationMessageID,
170 messageSize: 24,
171 parent: 1 as MIDIObjectRef,
172 parentType: kMIDIObjectType_Device,
173 child: 2 as MIDIObjectRef,
174 childType: kMIDIObjectType_Other,
175 };
176
177 let notification = Notification::from(unsafe {
178 &*(&notification_raw as *const _ as *const MIDINotification)
179 });
180
181 assert!(notification.is_ok());
182
183 let info = AddedRemovedInfo {
184 parent: Object(1),
185 parent_type: ObjectType::Device,
186 child: Object(2),
187 child_type: ObjectType::Other,
188 };
189
190 assert_eq!(notification.unwrap(), Notification::ObjectAdded(info));
191 }
192
193 #[test]
194 fn notification_from_object_removed() {
195 let notification_raw = MIDIObjectAddRemoveNotification {
196 messageID: kMIDIMsgObjectRemoved as MIDINotificationMessageID,
197 messageSize: 24,
198 parent: 1 as MIDIObjectRef,
199 parentType: kMIDIObjectType_Device,
200 child: 2 as MIDIObjectRef,
201 childType: kMIDIObjectType_Other,
202 };
203
204 let notification = Notification::from(unsafe {
205 &*(&notification_raw as *const _ as *const MIDINotification)
206 });
207
208 assert!(notification.is_ok());
209
210 let info = AddedRemovedInfo {
211 parent: Object(1),
212 parent_type: ObjectType::Device,
213 child: Object(2),
214 child_type: ObjectType::Other,
215 };
216
217 assert_eq!(notification.unwrap(), Notification::ObjectRemoved(info));
218 }
219
220 #[test]
221 fn notification_from_object_added_removed_err() {
222 let notification_raw = MIDIObjectAddRemoveNotification {
223 messageID: kMIDIMsgObjectAdded as MIDINotificationMessageID,
224 messageSize: 24,
225 parent: 1 as MIDIObjectRef,
226 parentType: kMIDIObjectType_Device,
227 child: 2 as MIDIObjectRef,
228 childType: 0xffff,
229 };
230
231 let notification = Notification::from(unsafe {
232 &*(&notification_raw as *const _ as *const MIDINotification)
233 });
234
235 assert!(notification.is_err());
236 assert_eq!(notification.err().unwrap(), kMIDIMsgObjectAdded as i32);
237
238 let notification_raw = MIDIObjectAddRemoveNotification {
239 messageID: kMIDIMsgObjectRemoved as MIDINotificationMessageID,
240 messageSize: 24,
241 parent: 1 as MIDIObjectRef,
242 parentType: 0xffff,
243 child: 2 as MIDIObjectRef,
244 childType: kMIDIObjectType_Device,
245 };
246
247 let notification = Notification::from(unsafe {
248 &*(&notification_raw as *const _ as *const MIDINotification)
249 });
250
251 assert!(notification.is_err());
252 assert_eq!(notification.err().unwrap(), kMIDIMsgObjectRemoved as i32);
253 }
254
255 #[test]
256 fn notification_from_property_changed() {
257 let name = CFString::new("name");
258 let notification_raw = MIDIObjectPropertyChangeNotification {
259 messageID: kMIDIMsgPropertyChanged as MIDINotificationMessageID,
260 messageSize: 24,
261 object: 1 as MIDIObjectRef,
262 objectType: kMIDIObjectType_Device,
263 propertyName: name.as_concrete_TypeRef(),
264 };
265
266 let notification = Notification::from(unsafe {
267 &*(&notification_raw as *const _ as *const MIDINotification)
268 });
269
270 assert!(notification.is_ok());
271
272 let info = PropertyChangedInfo {
273 object: Object(1),
274 object_type: ObjectType::Device,
275 property_name: "name".to_string(),
276 };
277
278 assert_eq!(notification.unwrap(), Notification::PropertyChanged(info));
279 }
280
281 #[test]
282 fn notification_from_property_changed_error() {
283 let name = CFString::new("name");
284 let notification_raw = MIDIObjectPropertyChangeNotification {
285 messageID: kMIDIMsgPropertyChanged as MIDINotificationMessageID,
286 messageSize: 24,
287 object: 1 as MIDIObjectRef,
288 objectType: 0xffff,
289 propertyName: name.as_concrete_TypeRef(),
290 };
291
292 let notification = Notification::from(unsafe {
293 &*(&notification_raw as *const _ as *const MIDINotification)
294 });
295
296 assert!(notification.is_err());
297 assert_eq!(notification.err().unwrap(), kMIDIMsgPropertyChanged as i32);
298 }
299
300 #[test]
301 fn notification_from_thru_connections_changed() {
302 let notification_raw = MIDINotification {
303 messageID: kMIDIMsgThruConnectionsChanged as MIDINotificationMessageID,
304 messageSize: 8,
305 };
306 let notification = Notification::from(&notification_raw);
307 assert!(notification.is_ok());
308 assert_eq!(notification.unwrap(), Notification::ThruConnectionsChanged);
309 }
310
311 #[test]
312 fn notification_from_serial_port_owner_changed() {
313 let notification_raw = MIDINotification {
314 messageID: kMIDIMsgSerialPortOwnerChanged as MIDINotificationMessageID,
315 messageSize: 8,
316 };
317 let notification = Notification::from(&notification_raw);
318 assert!(notification.is_ok());
319 assert_eq!(notification.unwrap(), Notification::SerialPortOwnerChanged);
320 }
321
322 #[test]
323 fn notification_from_io_error() {
324 let notification_raw = MIDIIOErrorNotification {
325 messageID: kMIDIMsgIOError as MIDINotificationMessageID,
326 messageSize: 16,
327 driverDevice: 1 as MIDIObjectRef,
328 errorCode: 123 as OSStatus,
329 };
330
331 let notification = Notification::from(unsafe {
332 &*(&notification_raw as *const _ as *const MIDINotification)
333 });
334
335 assert!(notification.is_ok());
336
337 let info = IoErrorInfo {
338 driver_device: Device { object: Object(1) },
339 error_code: 123 as OSStatus,
340 };
341
342 assert_eq!(notification.unwrap(), Notification::IoError(info));
343 }
344 }