mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-14 01:51:29 +03:00
start abstracting event type
This commit is contained in:
@@ -6,6 +6,7 @@ port = 42069
|
|||||||
|
|
||||||
[client.left]
|
[client.left]
|
||||||
host_name = "Osmium"
|
host_name = "Osmium"
|
||||||
|
ip = "192.168.178.114"
|
||||||
port = 42069
|
port = 42069
|
||||||
|
|
||||||
# [client.right]
|
# [client.right]
|
||||||
|
|||||||
290
src/event.rs
290
src/event.rs
@@ -1,154 +1,174 @@
|
|||||||
|
use std::{error::Error, fmt::{self, format}};
|
||||||
|
|
||||||
pub mod producer;
|
pub mod producer;
|
||||||
pub mod consumer;
|
pub mod consumer;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
||||||
/*
|
pub enum PointerEvent {
|
||||||
* TODO: currently the wayland events are encoded
|
Motion { time: u32, relative_x: f64, relative_y: f64, },
|
||||||
* directly with no generalized event format
|
Button { time: u32, button: u32, state: u32, },
|
||||||
*/
|
Axis { time: u32, axis: u8, value: f64, },
|
||||||
use wayland_client::{protocol::{wl_pointer, wl_keyboard}, WEnum};
|
Frame {}
|
||||||
|
|
||||||
pub trait Encode {
|
|
||||||
fn encode(&self) -> Vec<u8>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Decode {
|
pub enum KeyboardEvent {
|
||||||
fn decode(buf: Vec<u8>) -> Self;
|
Key { serial: u32, time: u32, key: u32, state: u8, },
|
||||||
}
|
Modifiers { serial: u32, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32, },
|
||||||
|
|
||||||
impl Encode for wl_pointer::Event {
|
|
||||||
fn encode(&self) -> Vec<u8> {
|
|
||||||
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<u8> {
|
|
||||||
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 Event {
|
pub enum Event {
|
||||||
Pointer(wl_pointer::Event),
|
Pointer(PointerEvent),
|
||||||
Keyboard(wl_keyboard::Event),
|
Keyboard(KeyboardEvent),
|
||||||
Release(),
|
Release(),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encode for Event {
|
|
||||||
fn encode(&self) -> Vec<u8> {
|
|
||||||
match self {
|
|
||||||
Event::Pointer(p) => p.encode(),
|
|
||||||
Event::Keyboard(k) => k.encode(),
|
|
||||||
Event::Release() => vec![6u8],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl Send for Event {}
|
unsafe impl Send for Event {}
|
||||||
unsafe impl Sync for Event {}
|
unsafe impl Sync for Event {}
|
||||||
|
|
||||||
impl Decode for Event {
|
impl Event {
|
||||||
fn decode(buf: Vec<u8>) -> Self {
|
fn event_type(&self) -> EventType {
|
||||||
match buf[0] {
|
match self {
|
||||||
0 => Self::Pointer(wl_pointer::Event::Motion {
|
Self::Pointer(_) => EventType::POINTER,
|
||||||
time: u32::from_ne_bytes(buf[1..5].try_into().unwrap()),
|
Self::Keyboard(_) => EventType::KEYBOARD,
|
||||||
surface_x: f64::from_ne_bytes(buf[5..13].try_into().unwrap()),
|
Self::Release() => EventType::RELEASE,
|
||||||
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 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<Vec<u8>> for &Event {
|
||||||
|
fn into(self) -> Vec<u8> {
|
||||||
|
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<Vec<u8>> for Event {
|
||||||
|
type Error = Box<dyn Error>;
|
||||||
|
|
||||||
|
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||||
|
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<Vec<u8>> for &PointerEvent {
|
||||||
|
fn into(self) -> Vec<u8> {
|
||||||
|
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<Self, Self::Error> {
|
||||||
|
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<Vec<u8>> for &KeyboardEvent {
|
||||||
|
fn into(self) -> Vec<u8> {
|
||||||
|
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<Self, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use std::{
|
|||||||
|
|
||||||
use crate::client::{ClientHandle, ClientManager};
|
use crate::client::{ClientHandle, ClientManager};
|
||||||
|
|
||||||
use super::{Event, Encode, Decode};
|
use super::{Event, Decode};
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
listen_addr: SocketAddr,
|
listen_addr: SocketAddr,
|
||||||
@@ -102,8 +102,9 @@ impl Server {
|
|||||||
Ok((receiver, sender))
|
Ok((receiver, sender))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_event<E: Encode>(tx: &UdpSocket, e: E, addr: SocketAddr) {
|
fn send_event(tx: &UdpSocket, e: Event, addr: SocketAddr) {
|
||||||
if let Err(e) = tx.send_to(&e.encode(), addr) {
|
let data: Vec<u8> = (&e).into();
|
||||||
|
if let Err(e) = tx.send_to(&data[..], addr) {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,7 +112,7 @@ impl Server {
|
|||||||
fn receive_event(rx: &UdpSocket) -> Option<(Event, SocketAddr)> {
|
fn receive_event(rx: &UdpSocket) -> Option<(Event, SocketAddr)> {
|
||||||
let mut buf = vec![0u8; 21];
|
let mut buf = vec![0u8; 21];
|
||||||
if let Ok((_amt, src)) = rx.recv_from(&mut buf) {
|
if let Ok((_amt, src)) = rx.recv_from(&mut buf) {
|
||||||
Some((Event::decode(buf), src))
|
Some((Event::try_from(buf), src))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user