Created
February 23, 2022 20:53
-
-
Save amenophis/33341dfdb2cb051f9ab7f394f2e2ead9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::thread; | |
use std::thread::sleep; | |
use std::time::Duration; | |
use streamdeck::StreamDeck; | |
const ELGATO_VENDOR_ID: u16 = 0x0fd9; | |
use rusb::{Context, Device, HotplugBuilder, UsbContext}; | |
struct HotPlugHandler; | |
impl<T: UsbContext> rusb::Hotplug<T> for HotPlugHandler { | |
fn device_arrived(&mut self, device: Device<T>) { | |
println!("device arrived {:?}", device); | |
} | |
fn device_left(&mut self, device: Device<T>) { | |
println!("device left {:?}", device); | |
} | |
} | |
impl Drop for HotPlugHandler { | |
fn drop(&mut self) { | |
println!("HotPlugHandler dropped"); | |
} | |
} | |
pub struct DeviceManager { | |
pub devices: HashMap<String, StreamDeck> | |
} | |
impl DeviceManager { | |
pub fn new() -> Self { | |
if rusb::has_hotplug() { | |
let context = Context::new().unwrap(); | |
let handler = HotPlugHandler {}; | |
let builder = HotplugBuilder::new() | |
.enumerate(true) | |
.vendor_id(ELGATO_VENDOR_ID) | |
.register::<Context, &Context>(&context, Box::new(handler)) | |
.unwrap() | |
; | |
if let mut reg = Some(builder) { | |
thread::spawn(move || { | |
loop { | |
context.handle_events(None).unwrap(); | |
if let Some(reg) = reg.take() {} | |
sleep(Duration::from_millis(100)); | |
} | |
}); | |
} | |
} else { | |
panic!("libusb hotplug api unsupported"); | |
} | |
Self { | |
devices: HashMap::new() | |
} | |
} | |
pub fn device(&mut self, serial_number: String) -> Option<&mut StreamDeck> { | |
self.devices.get_mut(&serial_number) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment