use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub enum ServerEvent<'a> {
Switch(SwitchEvent),
Input(Vec<InputEvent>),
#[serde(borrow)]
ClipboardTypes(ClipboardTypes<'a>),
}
impl<'a> std::fmt::Display for ServerEvent<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ServerEvent::Switch(e) => e.fmt(f),
ServerEvent::Input(e) => f.write_str(format!("{:?}", e).as_str()),
ServerEvent::ClipboardTypes(e) => e.fmt(f),
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub enum ClientEvent<'a> {
#[serde(borrow)]
ClipboardTypes(ClipboardTypes<'a>),
}
impl<'a> std::fmt::Display for ClientEvent<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ClientEvent::ClipboardTypes(e) => e.fmt(f),
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SwitchEvent {
pub enabled: bool,
}
impl std::fmt::Display for SwitchEvent {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(format!("SwitchEvent(enabled={})", self.enabled).as_str())
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InputEvent {
pub inputi32: Option<InputI32>,
pub inputf64: Option<InputF64>,
}
impl std::fmt::Display for InputEvent {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(evt) = &self.inputi32 {
f.write_str(format!("InputEvent(inputi32={})", evt).as_str())
} else if let Some(evt) = &self.inputf64 {
f.write_str(format!("InputEvent(inputf64={})", evt).as_str())
} else {
f.write_str("InputEvent(?)")
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InputI32 {
pub type_: u16,
pub code: u16,
pub value: i32,
}
impl std::fmt::Display for InputI32 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(
format!(
"InputI32(type={}, code={}, value={})",
self.type_, self.code, self.value
)
.as_str(),
)
}
}
impl InputI32 {
pub fn from_evdev(e: evdev::InputEvent) -> InputI32 {
InputI32 {
type_: e.event_type().0,
code: e.code(),
value: e.value(),
}
}
pub fn to_evdev(&self) -> evdev::InputEvent {
evdev::InputEvent::new(evdev::EventType(self.type_), self.code, self.value)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InputF64 {
pub type_: u16,
pub code: u16,
pub value: f64,
}
impl std::fmt::Display for InputF64 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(
format!(
"InputF64(type={}, code={}, value={})",
self.type_, self.code, self.value
)
.as_str(),
)
}
}
impl InputF64 {
pub fn from_evdev(e: evdev::InputEvent, min: i32, max: i32) -> InputF64 {
InputF64 {
type_: e.event_type().0,
code: e.code(),
value: ((e.value() - min) as f64) / ((max - min) as f64),
}
}
pub fn to_evdev(&self, min: i32, max: i32) -> evdev::InputEvent {
evdev::InputEvent::new(
evdev::EventType(self.type_),
self.code,
(self.value * ((max - min) as f64)) as i32 + min,
)
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ClipboardTypes<'a> {
pub types: &'a str,
pub max_size_bytes: u64,
}
impl<'a> std::fmt::Display for ClipboardTypes<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(
format!(
"ClipboardTypes(types=[{}], max_size_bytes={})",
self.types, self.max_size_bytes
)
.as_str(),
)
}
}