coremidi-hotplug-notification

Simple helper providing a way to receive device updates notifications on MacOS

3 releases

0.1.3 Jan 13, 2024
0.1.1 Jan 1, 2024
0.1.0 Jan 1, 2024
Download history 13/week @ 2024-06-18 554/week @ 2024-06-25 622/week @ 2024-07-02 208/week @ 2024-07-09 369/week @ 2024-07-16 183/week @ 2024-07-23 763/week @ 2024-07-30 473/week @ 2024-08-06 135/week @ 2024-08-13 4/week @ 2024-08-20 91/week @ 2024-08-27 120/week @ 2024-09-03 33/week @ 2024-09-10 47/week @ 2024-09-17 370/week @ 2024-09-24 234/week @ 2024-10-01

728 downloads per month
Used in tauri-plugin-midi

MIT license

11KB
116 lines

coremidi-hotplug-notification

This crate is a simple helper providing a way to receive device updates notifications. It also allows users of crates such as midir to refresh the list of devices without having to restart the program, the notification is optional.

Prerequisites

[target.'cfg(target_os = "macos")'.dependencies]
coremidi-hotplug-notification = "0.1.1"

Usage

fn main() {
    #[cfg(target_os = "macos")]
    // Register for device update notifications
    coremidi_hotplug_notification::receive_device_updates(|| {
        println!("A MIDI device was connected or disconnected.");
        // Insert your handling logic here
    }).expect("Failed to register for MIDI device updates");
}

In practice, you'll most likely want to use a channel to receive updates. The closure does not receive any parameter, just re-read the list of devices from there.

If you are not interested in notifications and just intend to refresh manually, you can pass an empty closure ( || () ).

Caveats

MacOS will set the thread on which notifications are sent at the first call to create a coremidi client. Due to that, receive_device_updates will fail if you use any MIDI functionality before calling it.

This crate will spawn a thread dedicated to the runloop, which is necessary in order to receive device updates. If you are not willing to run a dedicated thread, you'll probably want to directly use the coremidi crate and call CFRunLoop::run_current or run_in_mode yourself.

Dependencies

~640KB
~13K SLoC