diff --git a/config.toml b/config.toml index 4f56ad4..13be4f6 100644 --- a/config.toml +++ b/config.toml @@ -6,6 +6,7 @@ port = 42069 [client.left] host_name = "Osmium" +ip = "192.168.178.114" port = 42069 # [client.right] diff --git a/src/event.rs b/src/event.rs index c7cfcca..ad2e82e 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,154 +1,174 @@ +use std::{error::Error, fmt::{self, format}}; + pub mod producer; pub mod consumer; pub mod server; -/* - * TODO: currently the wayland events are encoded - * directly with no generalized event format -*/ -use wayland_client::{protocol::{wl_pointer, wl_keyboard}, WEnum}; - -pub trait Encode { - fn encode(&self) -> Vec; +pub enum PointerEvent { + Motion { time: u32, relative_x: f64, relative_y: f64, }, + Button { time: u32, button: u32, state: u32, }, + Axis { time: u32, axis: u8, value: f64, }, + Frame {} } -pub trait Decode { - fn decode(buf: Vec) -> Self; -} - -impl Encode for wl_pointer::Event { - fn encode(&self) -> Vec { - let mut buf = Vec::new(); - match *self { - Self::Motion { - time: t, - surface_x: x, - surface_y: y, - } => { - buf.push(0u8); - buf.extend_from_slice(t.to_ne_bytes().as_ref()); - buf.extend_from_slice(x.to_ne_bytes().as_ref()); - buf.extend_from_slice(y.to_ne_bytes().as_ref()); - } - Self::Button { - serial: _, - time: t, - button: b, - state: s, - } => { - buf.push(1u8); - buf.extend_from_slice(t.to_ne_bytes().as_ref()); - buf.extend_from_slice(b.to_ne_bytes().as_ref()); - buf.push(u32::from(s) as u8); - } - Self::Axis { - time: t, - axis: a, - value: v, - } => { - buf.push(2u8); - buf.extend_from_slice(t.to_ne_bytes().as_ref()); - buf.push(u32::from(a) as u8); - buf.extend_from_slice(v.to_ne_bytes().as_ref()); - } - Self::Frame {} => { - buf.push(3u8); - } - _ => todo!(), - } - buf - } -} - -impl Encode for wl_keyboard::Event { - fn encode(&self) -> Vec { - let mut buf = Vec::new(); - match self { - Self::Key { - serial: _, - time: t, - key: k, - state: s, - } => { - buf.push(4u8); - buf.extend_from_slice(t.to_ne_bytes().as_ref()); - buf.extend_from_slice(k.to_ne_bytes().as_ref()); - buf.push(u32::from(*s) as u8); - } - Self::Modifiers { - serial: _, - mods_depressed, - mods_latched, - mods_locked, - group, - } => { - buf.push(5u8); - buf.extend_from_slice(mods_depressed.to_ne_bytes().as_ref()); - buf.extend_from_slice(mods_latched.to_ne_bytes().as_ref()); - buf.extend_from_slice(mods_locked.to_ne_bytes().as_ref()); - buf.extend_from_slice(group.to_ne_bytes().as_ref()); - } - _ => todo!(), - } - buf - } +pub enum KeyboardEvent { + Key { serial: u32, time: u32, key: u32, state: u8, }, + Modifiers { serial: u32, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32, }, } pub enum Event { - Pointer(wl_pointer::Event), - Keyboard(wl_keyboard::Event), + Pointer(PointerEvent), + Keyboard(KeyboardEvent), Release(), } -impl Encode for Event { - fn encode(&self) -> Vec { - match self { - Event::Pointer(p) => p.encode(), - Event::Keyboard(k) => k.encode(), - Event::Release() => vec![6u8], - } - } -} - unsafe impl Send for Event {} unsafe impl Sync for Event {} -impl Decode for Event { - fn decode(buf: Vec) -> Self { - match buf[0] { - 0 => Self::Pointer(wl_pointer::Event::Motion { - time: u32::from_ne_bytes(buf[1..5].try_into().unwrap()), - surface_x: f64::from_ne_bytes(buf[5..13].try_into().unwrap()), - surface_y: f64::from_ne_bytes(buf[13..21].try_into().unwrap()), - }), - 1 => Self::Pointer(wl_pointer::Event::Button { - serial: 0, - time: (u32::from_ne_bytes(buf[1..5].try_into().unwrap())), - button: (u32::from_ne_bytes(buf[5..9].try_into().unwrap())), - state: (WEnum::Value(wl_pointer::ButtonState::try_from(buf[9] as u32).unwrap())), - }), - 2 => Self::Pointer(wl_pointer::Event::Axis { - time: (u32::from_ne_bytes(buf[1..5].try_into().unwrap())), - axis: (WEnum::Value(wl_pointer::Axis::try_from(buf[5] as u32).unwrap())), - value: (f64::from_ne_bytes(buf[6..14].try_into().unwrap())), - }), - 3 => Self::Pointer(wl_pointer::Event::Frame {}), - 4 => Self::Keyboard(wl_keyboard::Event::Key { - serial: 0, - time: u32::from_ne_bytes(buf[1..5].try_into().unwrap()), - key: u32::from_ne_bytes(buf[5..9].try_into().unwrap()), - state: WEnum::Value(wl_keyboard::KeyState::try_from(buf[9] as u32).unwrap()), - }), - 5 => Self::Keyboard(wl_keyboard::Event::Modifiers { - serial: 0, - mods_depressed: u32::from_ne_bytes(buf[1..5].try_into().unwrap()), - mods_latched: u32::from_ne_bytes(buf[5..9].try_into().unwrap()), - mods_locked: u32::from_ne_bytes(buf[9..13].try_into().unwrap()), - group: u32::from_ne_bytes(buf[13..17].try_into().unwrap()), - }), - 6 => Self::Release(), - _ => panic!("protocol violation"), +impl Event { + fn event_type(&self) -> EventType { + match self { + Self::Pointer(_) => EventType::POINTER, + Self::Keyboard(_) => EventType::KEYBOARD, + Self::Release() => EventType::RELEASE, } } } +impl PointerEvent { + fn event_type(&self) -> PointerEventType { + match self { + Self::Axis {..} => PointerEventType::AXIS, + Self::Button {..} => PointerEventType::BUTTON, + Self::Frame {..} => PointerEventType::FRAME, + Self::Motion {..} => PointerEventType::MOTION, + } + } +} + +enum PointerEventType { MOTION, BUTTON, AXIS, FRAME } +enum KeyboardEventType { KEY, MODIFIERS } +enum EventType { POINTER, KEYBOARD, RELEASE } + + +impl Into> for &Event { + fn into(self) -> Vec { + let event_id = vec![self.event_type() as u8]; + let event_data = match self { + Event::Pointer(p) => p.into(), + Event::Keyboard(k) => k.into(), + Event::Release() => vec![], + }; + vec![event_id, event_data].concat() + } +} + +#[derive(Debug)] +struct ProtocolError { + msg: String, +} + +impl fmt::Display for ProtocolError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Protocol violation: {}", self.msg) + } +} +impl Error for ProtocolError {} + +impl TryFrom> for Event { + type Error = Box; + + fn try_from(value: Vec) -> Result { + let event_id = u8::from_be_bytes(value[..1].try_into()?); + match event_id >> 5 { + i if i == (EventType::POINTER as u8) => Ok(Event::Pointer(value[1..].try_into()?)), + i if i ==(EventType::KEYBOARD as u8) => Ok(Event::Keyboard(value[1..].try_into()?)), + i if i == (EventType::RELEASE as u8) => Ok(Event::Release()), + _ => Err(Box::new(ProtocolError{ msg: format!("invalid event_id {}", event_id) })), + } + } +} + +impl Into> for &PointerEvent { + fn into(self) -> Vec { + let id = vec![self.event_type() as u8]; + let data = match self { + PointerEvent::Motion { time, relative_x, relative_y } => { + let time = time.to_be_bytes(); + let relative_x = relative_x.to_be_bytes(); + let relative_y = relative_y.to_be_bytes(); + vec![&time[..], &relative_x[..], &relative_y[..]].concat() + }, + PointerEvent::Button { time, button, state } => { + let time = time.to_be_bytes(); + let button = button.to_be_bytes(); + let state = state.to_be_bytes(); + vec![&time[..], &button[..], &state[..]].concat() + }, + PointerEvent::Axis { time, axis, value } => { + let time = time.to_be_bytes(); + let axis = axis.to_be_bytes(); + let value = value.to_be_bytes(); + vec![&time[..], &axis[..], &value[..]].concat() + }, + PointerEvent::Frame { } => { vec![] }, + }; + vec![id, data].concat() + } +} + +impl TryFrom<&[u8]> for PointerEvent { + type Error = &'static str; + + fn try_from(data: &[u8]) -> Result { + match data.get(0) { + Some(id) => match id { + 0 => { + let time = match data.get(1..5) { + Some(d) => u32::from_be_bytes(d.try_into().unwrap()), + None => return Err("Expected 4 Bytes at index 1"), + }; + let relative_x = match data.get(5..13) { + Some(d) => f64::from_be_bytes(d.try_into().unwrap()), + None => return Err("Expected 8 Bytes at index 5"), + }; + let relative_y = match data.get(13..21) { + Some(d) => f64::from_be_bytes(d.try_into().unwrap()), + None => return Err("Expected 8 Bytes at index 13"), + }; + Ok(Self::Motion{ time, relative_x, relative_y }) + } + }, + None => Err("Expected an element at index 0"), + } + } +} + +impl Into> for &KeyboardEvent { + fn into(self) -> Vec { + match self { + KeyboardEvent::Key { serial, time, key, state } => { + let serial = serial.to_be_bytes(); + let time = time.to_be_bytes(); + let key = key.to_be_bytes(); + let state = state.to_be_bytes(); + vec![&serial[..], &time[..], &key[..], &state[..]].concat() + }, + KeyboardEvent::Modifiers { serial, mods_depressed, mods_latched, mods_locked, group } => { + let serial = serial.to_be_bytes(); + let mods_depressed = mods_depressed.to_be_bytes(); + let mods_latched = mods_latched.to_be_bytes(); + let mods_locked = mods_locked.to_be_bytes(); + vec![&serial[..], &mods_depressed[..], &mods_latched[..], &mods_locked[..]].concat() + }, + } + } +} + +impl TryFrom<&[u8]> for KeyboardEvent { + type Error = TryFromSliceError; + + fn try_from(value: &[u8]) -> Result { + todo!() + } +} diff --git a/src/event/server.rs b/src/event/server.rs index 3eeb3e2..88a047b 100644 --- a/src/event/server.rs +++ b/src/event/server.rs @@ -7,7 +7,7 @@ use std::{ use crate::client::{ClientHandle, ClientManager}; -use super::{Event, Encode, Decode}; +use super::{Event, Decode}; pub struct Server { listen_addr: SocketAddr, @@ -102,8 +102,9 @@ impl Server { Ok((receiver, sender)) } - fn send_event(tx: &UdpSocket, e: E, addr: SocketAddr) { - if let Err(e) = tx.send_to(&e.encode(), addr) { + fn send_event(tx: &UdpSocket, e: Event, addr: SocketAddr) { + let data: Vec = (&e).into(); + if let Err(e) = tx.send_to(&data[..], addr) { eprintln!("{}", e); } } @@ -111,7 +112,7 @@ impl Server { fn receive_event(rx: &UdpSocket) -> Option<(Event, SocketAddr)> { let mut buf = vec![0u8; 21]; if let Ok((_amt, src)) = rx.recv_from(&mut buf) { - Some((Event::decode(buf), src)) + Some((Event::try_from(buf), src)) } else { None }