From f813261f50213383f6764f8b655bd138c8c0aa72 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Tue, 20 Sep 2022 20:27:20 +0200 Subject: [PATCH] test on plasma + now working --- src/bin/client.rs | 96 +++++++++++++++++++++------------ src/bin/server.rs | 135 ++++++++++++++++++++++++++++------------------ src/config.rs | 12 ++--- src/dns.rs | 7 ++- src/lib.rs | 2 +- src/protocol.rs | 82 ++++++++++++++++++++-------- 6 files changed, 217 insertions(+), 117 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index 8296563..9919414 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -1,5 +1,11 @@ -use std::{os::unix::prelude::AsRawFd, io::{Write, BufWriter}}; -use lan_mouse::{protocol::{self, DataRequest}, config::Config}; +use lan_mouse::{ + config::Config, + protocol::{self, DataRequest}, +}; +use std::{ + io::{BufWriter, Write}, + os::unix::prelude::AsRawFd, +}; use wayland_protocols_wlr::virtual_pointer::v1::client::{ zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1 as VpManager, @@ -12,7 +18,7 @@ use wayland_protocols_misc::zwp_virtual_keyboard_v1::client::{ }; use wayland_client::{ - protocol::{wl_registry, wl_seat, wl_pointer, wl_keyboard}, + protocol::{wl_keyboard, wl_pointer, wl_registry, wl_seat}, Connection, Dispatch, EventQueue, QueueHandle, }; @@ -85,7 +91,6 @@ fn main() { // use roundtrip to process this event synchronously event_queue.roundtrip(&mut app).unwrap(); - let vpm = app.vpm.as_ref().unwrap(); let vkm = app.vkm.as_ref().unwrap(); let seat = app.seat.as_ref().unwrap(); @@ -94,7 +99,7 @@ fn main() { let connection = protocol::Connection::new(config); let data = loop { match connection.receive_data(DataRequest::KeyMap) { - Some(data) => { break data } + Some(data) => break data, None => {} } }; @@ -109,39 +114,64 @@ fn main() { } /// main loop handling udp packets -fn udp_loop(connection: &protocol::Connection, pointer: &Vp, keyboard: &Vk, q: EventQueue) -> std::io::Result<()> { +fn udp_loop( + connection: &protocol::Connection, + pointer: &Vp, + keyboard: &Vk, + q: EventQueue, +) -> std::io::Result<()> { loop { if let Some(event) = connection.receive_event() { match event { - 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::Pointer(e) => match e { + wl_pointer::Event::Motion { + time, + surface_x, + surface_y, + } => { + pointer.motion(time, surface_x, surface_y); + pointer.frame(); } - } - 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!(), + 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 53a01e7..3e2ad49 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -8,23 +8,19 @@ use std::{ }; use wayland_protocols::wp::{ + keyboard_shortcuts_inhibit::zv1::client::{ + zwp_keyboard_shortcuts_inhibit_manager_v1, zwp_keyboard_shortcuts_inhibitor_v1, + }, pointer_constraints::zv1::client::{zwp_locked_pointer_v1, zwp_pointer_constraints_v1}, relative_pointer::zv1::client::{zwp_relative_pointer_manager_v1, zwp_relative_pointer_v1}, - keyboard_shortcuts_inhibit::zv1::client::{ - zwp_keyboard_shortcuts_inhibit_manager_v1, - zwp_keyboard_shortcuts_inhibitor_v1, - }, }; -use wayland_protocols_wlr::layer_shell::v1::client::{ - zwlr_layer_shell_v1, - zwlr_layer_surface_v1, -}; +use wayland_protocols_wlr::layer_shell::v1::client::{zwlr_layer_shell_v1, zwlr_layer_surface_v1}; use wayland_client::{ protocol::{ - wl_buffer, wl_compositor, wl_keyboard, wl_pointer, wl_registry, wl_seat, wl_shm, - wl_shm_pool, wl_surface, wl_region, + wl_buffer, wl_compositor, wl_keyboard, wl_pointer, wl_region, wl_registry, + wl_seat, wl_shm, wl_shm_pool, wl_surface, }, Connection, Dispatch, QueueHandle, WEnum, }; @@ -42,8 +38,10 @@ struct App { rel_pointer_manager: Option, pointer_lock: Option, rel_pointer: Option, - shortcut_inhibit_manager: Option, - shortcut_inhibitor: Option, + shortcut_inhibit_manager: + Option, + shortcut_inhibitor: + Option, connection: protocol::Connection, seat: Option, } @@ -94,7 +92,7 @@ fn main() { zwlr_layer_shell_v1::Layer::Top, "LAN Mouse Sharing".into(), &qh, - () + (), ); app.layer_surface = Some(layer_surface); let layer_surface = app.layer_surface.as_ref().unwrap(); @@ -156,7 +154,7 @@ impl Dispatch for App { app.buffer = Some(buffer); } "wl_seat" => { - app.seat = Some(registry.bind::(name, 8, qh, ())); + app.seat = Some(registry.bind::(name, 7, qh, ())); } "zwp_pointer_constraints_v1" => { app.pointer_constraints = Some( @@ -179,10 +177,14 @@ impl Dispatch for App { ); } "zwlr_layer_shell_v1" => { - app.layer_shell = Some(registry.bind::( - name, - 4, &qh, (), - )); + app.layer_shell = Some( + registry.bind::( + name, + 3, + &qh, + (), + ), + ); } "zwp_keyboard_shortcuts_inhibit_manager_v1" => { app.shortcut_inhibit_manager = Some(registry.bind::( @@ -217,7 +219,7 @@ impl Dispatch for App { _: &Connection, _: &QueueHandle, ) { - todo!() + // } } @@ -294,13 +296,16 @@ impl Dispatch for App { ) { match event { wl_pointer::Event::Enter { - serial: _, + serial, surface: _, surface_x: _, surface_y: _, } => { + pointer.set_cursor(serial, None, 0, 0); if let Some(s) = app.layer_surface.as_ref() { - s.set_keyboard_interactivity(zwlr_layer_surface_v1::KeyboardInteractivity::Exclusive); + s.set_keyboard_interactivity( + zwlr_layer_surface_v1::KeyboardInteractivity::Exclusive, + ); app.surface.as_ref().unwrap().commit(); } if app.pointer_lock.is_none() { @@ -323,29 +328,37 @@ impl Dispatch for App { ); } if app.shortcut_inhibitor.is_none() { - app.shortcut_inhibitor = Some(app.shortcut_inhibit_manager.as_ref().unwrap().inhibit_shortcuts( - app.surface.as_ref().unwrap(), - app.seat.as_ref().unwrap(), - qh, (), - )); + app.shortcut_inhibitor = Some( + app.shortcut_inhibit_manager + .as_ref() + .unwrap() + .inhibit_shortcuts( + app.surface.as_ref().unwrap(), + app.seat.as_ref().unwrap(), + qh, + (), + ), + ); } } - wl_pointer::Event::Leave {..} => { + wl_pointer::Event::Leave { .. } => { if let Some(s) = app.layer_surface.as_ref() { - s.set_keyboard_interactivity(zwlr_layer_surface_v1::KeyboardInteractivity::None); + s.set_keyboard_interactivity( + zwlr_layer_surface_v1::KeyboardInteractivity::None, + ); app.surface.as_ref().unwrap().commit(); } } - wl_pointer::Event::Button {..} => { + wl_pointer::Event::Button { .. } => { app.connection.send_event(event); } - wl_pointer::Event::Axis {..} => { + wl_pointer::Event::Axis { .. } => { app.connection.send_event(event); } - wl_pointer::Event::Frame {..} => { + wl_pointer::Event::Frame { .. } => { app.connection.send_event(event); } - _ => {}, + _ => {} } } } @@ -360,12 +373,13 @@ impl Dispatch for App { _: &QueueHandle, ) { match event { - wl_keyboard::Event::Key {..} => { + wl_keyboard::Event::Key { .. } => { app.connection.send_event(event); } - wl_keyboard::Event::Modifiers { mods_depressed, ..} => { + wl_keyboard::Event::Modifiers { mods_depressed, .. } => { app.connection.send_event(event); - if mods_depressed == 77 { // ctrl shift super alt + if mods_depressed == 77 { + // ctrl shift super alt if let Some(pointer_lock) = app.pointer_lock.as_ref() { pointer_lock.destroy(); app.pointer_lock = None; @@ -380,9 +394,14 @@ impl Dispatch for App { } } } - wl_keyboard::Event::Keymap { format:_ , fd, size:_ } => { + wl_keyboard::Event::Keymap { + format: _, + fd, + size: _, + } => { let mmap = unsafe { Mmap::map(&File::from_raw_fd(fd.as_raw_fd())).unwrap() }; - app.connection.offer_data(protocol::DataRequest::KeyMap, mmap); + app.connection + .offer_data(protocol::DataRequest::KeyMap, mmap); } _ => (), } @@ -422,7 +441,6 @@ impl Dispatch _: &Connection, _: &QueueHandle, ) { - // } } @@ -436,15 +454,20 @@ impl Dispatch for App { _: &QueueHandle, ) { if let zwp_relative_pointer_v1::Event::RelativeMotion { - utime_hi, - utime_lo, - dx: _, - dy: _, - dx_unaccel: surface_x, - dy_unaccel: surface_y, - } = event { + utime_hi, + utime_lo, + dx: _, + dy: _, + dx_unaccel: surface_x, + dy_unaccel: surface_y, + } = event + { 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 }); + app.connection.send_event(wl_pointer::Event::Motion { + time, + surface_x, + surface_y, + }); } } } @@ -458,7 +481,6 @@ impl Dispatch for App { _: &Connection, _: &QueueHandle, ) { - // } } @@ -474,7 +496,11 @@ impl Dispatch for App { if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event { app.surface.as_ref().unwrap().commit(); surface.ack_configure(serial); - app.surface.as_ref().unwrap().attach(Some(app.buffer.as_ref().unwrap()), 0, 0); + app.surface + .as_ref() + .unwrap() + .attach(Some(app.buffer.as_ref().unwrap()), 0, 0); + app.surface.as_ref().unwrap().commit(); } } } @@ -487,10 +513,13 @@ impl Dispatch for App { _: &(), _: &Connection, _: &QueueHandle, - ) { } + ) { + } } -impl Dispatch for App { +impl Dispatch + for App +{ fn event( _: &mut Self, _: &zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, @@ -498,7 +527,8 @@ impl Dispatch, - ) { } + ) { + } } impl Dispatch for App { @@ -509,5 +539,6 @@ impl Dispatch, - ) { } + ) { + } } diff --git a/src/config.rs b/src/config.rs index 5c75ddf..c7b3c8f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,15 +1,15 @@ -use toml; +use serde_derive::{Deserialize, Serialize}; use std::net::IpAddr; -use std::{fs, error::Error}; -use serde_derive::{Serialize, Deserialize}; +use std::{error::Error, fs}; +use toml; -#[derive(Serialize,Deserialize,Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Config { pub client: Clients, pub port: Option, } -#[derive(Serialize,Deserialize,Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Clients { pub left: Option, pub right: Option, @@ -27,6 +27,6 @@ pub struct Client { impl Config { pub fn new(path: &str) -> Result> { let config = fs::read_to_string(path)?; - Ok(toml::from_str::<_>(&config).unwrap()) + Ok(toml::from_str::<_>(&config)?) } } diff --git a/src/dns.rs b/src/dns.rs index 6fa7841..aa37bea 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -1,4 +1,4 @@ -use std::{net::IpAddr, error::Error, fmt::Display}; +use std::{error::Error, fmt::Display, net::IpAddr}; use trust_dns_resolver::Resolver; @@ -6,11 +6,10 @@ use trust_dns_resolver::Resolver; struct InvalidConfigError; #[derive(Debug, Clone)] -struct DnsError{ +struct DnsError { host: String, } - impl Error for InvalidConfigError {} impl Display for InvalidConfigError { @@ -35,6 +34,6 @@ pub fn resolve(host: &Option) -> Result> { let response = Resolver::from_system_conf()?.lookup_ip(host)?; match response.iter().next() { Some(ip) => Ok(ip), - None => Err(DnsError{host: host.clone()}.into()), + None => Err(DnsError { host: host.clone() }.into()), } } diff --git a/src/lib.rs b/src/lib.rs index d0df885..75ade56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ -pub mod dns; pub mod config; +pub mod dns; pub mod protocol; diff --git a/src/protocol.rs b/src/protocol.rs index 04882d8..b81d6a2 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,14 +1,21 @@ -use memmap::Mmap; use crate::config::{self, Config}; -use std::{io::prelude::*, net::TcpListener, thread, sync::{Arc, RwLock}, collections::HashMap}; use crate::dns; +use memmap::Mmap; +use std::{ + collections::HashMap, + io::prelude::*, + net::TcpListener, + process::exit, + sync::{Arc, RwLock}, + thread, +}; -use wayland_client::{protocol::{ - wl_pointer, - wl_keyboard, -}, WEnum}; +use wayland_client::{ + protocol::{wl_keyboard, wl_pointer}, + WEnum, +}; -use std::net::{SocketAddr, UdpSocket, TcpStream}; +use std::net::{SocketAddr, TcpStream, UdpSocket}; trait Resolve { fn resolve(&self) -> Option; @@ -22,7 +29,7 @@ impl Resolve for Option { }; let ip = match client.ip { Some(ip) => ip, - None => dns::resolve(&client.host_name).unwrap() + None => dns::resolve(&client.host_name).unwrap(), }; Some(SocketAddr::new(ip, client.port.unwrap_or(42069))) } @@ -53,25 +60,38 @@ 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 } => { + 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 } => { + 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 } => { + 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{} => { + Self::Frame {} => { buf.push(3u8); } _ => todo!(), @@ -84,13 +104,24 @@ 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 } => { + 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 } => { + 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()); @@ -141,11 +172,9 @@ impl Decode for Event { 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") + _ => panic!("protocol violation"), } } - - } #[derive(PartialEq, Eq, Hash)] @@ -162,8 +191,8 @@ impl From for DataRequest { } } -impl From<[u8;4]> for DataRequest { - fn from(buf: [u8;4]) -> Self { +impl From<[u8; 4]> for DataRequest { + fn from(buf: [u8; 4]) -> Self { DataRequest::from(u32::from_ne_bytes(buf)) } } @@ -217,8 +246,19 @@ impl Connection { } } }); + let sock = UdpSocket::bind(listen_addr); + let sock = match sock { + Ok(sock) => sock, + Err(e) => match e.kind() { + std::io::ErrorKind::AddrInUse => { + eprintln!("Server already running on port {}", port); + exit(1); + } + _ => panic!("{}", e), + }, + }; let c = Connection { - udp_socket: UdpSocket::bind(listen_addr).unwrap(), + udp_socket: sock, client: clients, offer_data: data, }; @@ -233,7 +273,7 @@ impl Connection { let mut sock = TcpStream::connect(self.client.left.unwrap()).unwrap(); sock.write(&u32::from(req).to_ne_bytes()).unwrap(); sock.flush().unwrap(); - let mut buf = [0u8;8]; + let mut buf = [0u8; 8]; sock.read_exact(&mut buf[..]).unwrap(); let len = usize::from_ne_bytes(buf); if len == 0 {