From af719ede32721ecfa0002c9a04bac3f2b91f24a5 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Mon, 19 Sep 2022 20:21:28 +0200 Subject: [PATCH] remove unnecessary reencoding --- src/bin/client.rs | 39 +++++++++--- src/bin/server.rs | 31 +++++----- src/protocol.rs | 149 ++++++++++++++++++++++++++-------------------- 3 files changed, 128 insertions(+), 91 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index 658ef12..8296563 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -12,7 +12,7 @@ use wayland_protocols_misc::zwp_virtual_keyboard_v1::client::{ }; use wayland_client::{ - protocol::{wl_registry, wl_seat, wl_keyboard}, + protocol::{wl_registry, wl_seat, wl_pointer, wl_keyboard}, Connection, Dispatch, EventQueue, QueueHandle, }; @@ -113,14 +113,35 @@ fn udp_loop(connection: &protocol::Connection, pointer: &Vp, keyboard: &Vk, q: E loop { if let Some(event) = connection.receive_event() { match event { - protocol::Event::Mouse { t, x, y } => { pointer.motion(t, x, y); pointer.frame(); } - protocol::Event::Button { t, b, s } => { pointer.button(t, b, s); pointer.frame(); } - protocol::Event::Axis { t, a, v } => { pointer.axis(t, a, v); pointer.frame(); } - protocol::Event::Frame { } => { pointer.frame(); }, - protocol::Event::Key { t, k, s } => { keyboard.key(t, k, u32::from(s)); }, - protocol::Event::KeyModifier { mods_depressed, mods_latched, mods_locked, group } => { - keyboard.modifiers(mods_depressed, mods_latched, mods_locked, group); - }, + protocol::Event::Pointer(e) => { + match e { + wl_pointer::Event::Motion { time, surface_x, surface_y } => { + pointer.motion(time, surface_x, surface_y); + pointer.frame(); + } + wl_pointer::Event::Button { serial: _, time: t, button: b, state: s } => { + pointer.button( t, b, s.into_result().unwrap()); + pointer.frame(); + } + wl_pointer::Event::Axis { time: t, axis: a, value: v } => { + pointer.axis(t, a.into_result().unwrap(), v); + pointer.frame(); + } + wl_pointer::Event::Frame {} => {} + _ => todo!(), + } + } + protocol::Event::Keyboard(e) => { + match e { + wl_keyboard::Event::Key { serial: _, time: t, key: k, state: s } => { + keyboard.key(t, k, u32::from(s)); + }, + wl_keyboard::Event::Modifiers { serial: _, mods_depressed, mods_latched, mods_locked, group } => { + keyboard.modifiers(mods_depressed, mods_latched, mods_locked, group); + }, + _ => todo!(), + } + } } } q.flush().unwrap(); diff --git a/src/bin/server.rs b/src/bin/server.rs index 42f6b55..bf5c9f1 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -355,16 +355,14 @@ impl Dispatch for App { ); } } - wl_pointer::Event::Button { serial:_, time, button, state } => { - let e = protocol::Event::Button { t: (time), b: button, s: (state.into_result().unwrap()) }; - app.connection.send_event(&e); + wl_pointer::Event::Button {..} => { + app.connection.send_event(event); } - wl_pointer::Event::Axis { time, axis, value } => { - let e = protocol::Event::Axis { t: time, a: (axis.into_result().unwrap()), v: value }; - app.connection.send_event(&e); + wl_pointer::Event::Axis {..} => { + app.connection.send_event(event); } - wl_pointer::Event::Frame => { - app.connection.send_event(&protocol::Event::Frame{}); + wl_pointer::Event::Frame {..} => { + app.connection.send_event(event); } _ => {}, } @@ -381,7 +379,7 @@ impl Dispatch for App { _: &QueueHandle, ) { match event { - wl_keyboard::Event::Key { serial: _, time, key, state } => { + wl_keyboard::Event::Key { serial: _, time: _, key, state: _ } => { if key == 1 { // ESC key if let Some(pointer_lock) = app.pointer_lock.as_ref() { @@ -393,11 +391,11 @@ impl Dispatch for App { app.rel_pointer = None; } } else { - app.connection.send_event(&protocol::Event::Key{ t: (time), k: (key), s: (state.into_result().unwrap()) }); + app.connection.send_event(event); } } - wl_keyboard::Event::Modifiers { serial: _, mods_depressed, mods_latched, mods_locked, group } => { - app.connection.send_event(&protocol::Event::KeyModifier{ mods_depressed, mods_latched, mods_locked, group }); + wl_keyboard::Event::Modifiers {..} => { + app.connection.send_event(event); } wl_keyboard::Event::Keymap { format:_ , fd, size:_ } => { let mmap = unsafe { Mmap::map(&File::from_raw_fd(fd.as_raw_fd())).unwrap() }; @@ -464,12 +462,11 @@ impl Dispatch for App { utime_lo, dx: _, dy: _, - dx_unaccel, - dy_unaccel, + dx_unaccel: surface_x, + dy_unaccel: surface_y, } = event { - let time = ((utime_hi as u64) << 32 | utime_lo as u64) / 1000; - let e = protocol::Event::Mouse { t: (time as u32), x: (dx_unaccel), y: (dy_unaccel) }; - app.connection.send_event(&e); + let time = (((utime_hi as u64) << 32 | utime_lo as u64) / 1000) as u32; + app.connection.send_event(wl_pointer::Event::Motion{ time, surface_x, surface_y }); } } } diff --git a/src/protocol.rs b/src/protocol.rs index a340f40..04882d8 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -3,10 +3,10 @@ use crate::config::{self, Config}; use std::{io::prelude::*, net::TcpListener, thread, sync::{Arc, RwLock}, collections::HashMap}; use crate::dns; -use wayland_client::protocol::{ - wl_pointer::{Axis, ButtonState}, - wl_keyboard::KeyState, -}; +use wayland_client::{protocol::{ + wl_pointer, + wl_keyboard, +}, WEnum}; use std::net::{SocketAddr, UdpSocket, TcpStream}; @@ -41,93 +41,113 @@ pub struct Connection { offer_data: Arc>>, } -pub enum Event { - Mouse{t: u32, x: f64, y: f64}, - Button{t: u32, b: u32, s: ButtonState}, - Axis{t: u32, a: Axis, v: f64}, - Frame{}, - Key{t: u32, k: u32, s: KeyState}, - KeyModifier{mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32}, +pub trait Encode { + fn encode(&self) -> Vec; } -impl From> for Event { - fn from(buf: Vec) -> Self { - match buf[0] { - 0 => Self::Mouse { - t: u32::from_ne_bytes(buf[1..5].try_into().unwrap()), - x: f64::from_ne_bytes(buf[5..13].try_into().unwrap()), - y: f64::from_ne_bytes(buf[13..21].try_into().unwrap()), - }, - 1 => Self::Button { - t: (u32::from_ne_bytes(buf[1..5].try_into().unwrap())), - b: (u32::from_ne_bytes(buf[5..9].try_into().unwrap())), - s: (ButtonState::try_from(buf[9] as u32).unwrap()) - }, - 2 => Self::Axis { - t: (u32::from_ne_bytes(buf[1..5].try_into().unwrap())), - a: (Axis::try_from(buf[5] as u32).unwrap()), - v: (f64::from_ne_bytes(buf[6..14].try_into().unwrap())), - }, - 3 => Self::Frame {}, - 4 => Self::Key { - t: u32::from_ne_bytes(buf[1..5].try_into().unwrap()), - k: u32::from_ne_bytes(buf[5..9].try_into().unwrap()), - s: KeyState::try_from(buf[9] as u32).unwrap(), - }, - 5 => Self::KeyModifier { - 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()), - }, - _ => panic!("protocol violation"), - } - } +pub trait Decode { + fn decode(buf: Vec) -> Self; } -impl From<&Event> for Vec { - fn from(e: &Event) -> Self { +impl Encode for wl_pointer::Event { + fn encode(&self) -> Vec { let mut buf = Vec::new(); - match e { - Event::Mouse { t, x, y } => { + 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()); } - Event::Button { t, b, s } => { + 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); + buf.push(u32::from(s) as u8); } - Event::Axis{t, a, v} => { + 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.push(u32::from(a) as u8); buf.extend_from_slice(v.to_ne_bytes().as_ref()); } - Event::Frame{} => { + Self::Frame{} => { buf.push(3u8); } - Event::Key{t, k, s } => { + _ => 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); } - Event::KeyModifier{ mods_depressed, mods_latched, mods_locked, group } => { + 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 { + Pointer(wl_pointer::Event), + Keyboard(wl_keyboard::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()), + }), + _ => panic!("protocol violation") + } + } + + +} + #[derive(PartialEq, Eq, Hash)] pub enum DataRequest { KeyMap, @@ -142,6 +162,12 @@ impl From for DataRequest { } } +impl From<[u8;4]> for DataRequest { + fn from(buf: [u8;4]) -> Self { + DataRequest::from(u32::from_ne_bytes(buf)) + } +} + impl From for u32 { fn from(d: DataRequest) -> Self { match d { @@ -150,12 +176,6 @@ impl From for u32 { } } -impl From<[u8;4]> for DataRequest { - fn from(buf: [u8;4]) -> Self { - DataRequest::from(u32::from_ne_bytes(buf)) - } -} - fn handle_request(data: &Arc>>, mut stream: TcpStream) { let mut buf = [0u8; 4]; stream.read_exact(&mut buf).unwrap(); @@ -224,18 +244,17 @@ impl Connection { Some(data) } - pub fn send_event(&self, e: &Event) { + pub fn send_event(&self, e: E) { // TODO check which client if let Some(addr) = self.client.right { - let buf: Vec = e.into(); - self.udp_socket.send_to(&buf, addr).unwrap(); + self.udp_socket.send_to(&e.encode(), addr).unwrap(); } } pub fn receive_event(&self) -> Option { let mut buf = vec![0u8; 21]; if let Ok((_amt, _src)) = self.udp_socket.recv_from(&mut buf) { - Some(Event::from(buf)) + Some(Event::decode(buf)) } else { None }