use json::{object, JsonValue};
mod utils;
#[cfg(target_os = "windows")]
pub mod win;
#[cfg(target_os = "windows")]
use win::Win;
#[cfg(target_os = "macos")]
pub mod mac;
#[cfg(target_os = "macos")]
use mac::Mac;
pub fn run<F>(factory: F)
where
F: FnOnce() -> Box<dyn Handler>,
{
#[cfg(target_os = "windows")]
Win::new().run(factory);
#[cfg(target_os = "macos")]
Mac::new().run(|| factory())
}
pub fn connect<F>(id: String, factory: F)
where
F: FnOnce() -> Box<dyn Handler>,
{
#[cfg(target_os = "windows")]
Win::new().connect(id, factory);
#[cfg(target_os = "macos")]
Mac::new().connect(id, || factory())
}
pub trait Handler {
fn on_discover(&mut self, device: Device);
fn on_data(&mut self, id: String, value: JsonValue);
fn on_is_close(&mut self, id: String) -> bool;
fn on_disconnected(&mut self, id: String, error: String);
fn on_connect(&mut self, id: String);
}
#[derive(Clone)]
pub struct Device {
pub id: String,
pub vid: String,
pub pid: String,
pub types: Types,
pub manufacturer: String,
pub name: String,
pub count: u8,
pub value: JsonValue,
}
impl Device {
pub fn new(id: String, types:Types,name:String) -> Self {
Self {
id,
vid:"".to_string(),
pid:"".to_string(),
manufacturer:"".to_string(),
name,
count:0,
types,
value: JsonValue::from("".to_string()),
}
}
pub fn json(&mut self) -> JsonValue {
object! {
"id": self.id.clone(),
"name": self.name.clone(),
"vid": self.vid.clone(),
"pid": self.pid.clone(),
"manufacturer": self.manufacturer.clone(),
"types": self.types.str(),
}
}
}
#[derive(Clone, Debug)]
pub enum Types {
Keyboard,
Mouse,
None,
}
impl Types {
fn str(&mut self) -> &'static str {
match self {
Types::Keyboard => "keyboard",
Types::Mouse => "mouse",
Types::None => "",
}
}
#[allow(dead_code)]
fn from(name: &str) -> Self {
match name {
"keyboard" => Types::Keyboard,
"mouse" => Types::Mouse,
_ => Types::None,
}
}
}