From 70b484878ec658bd880276bf876b5757b128f316 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Sun, 12 Feb 2023 19:04:28 +0100 Subject: [PATCH] initial windows support --- Cargo.lock | 1 + Cargo.toml | 13 +++++-- src/backend.rs | 2 + src/backend/wayland.rs | 2 + src/{event => backend/wayland}/consumer.rs | 1 + src/{event => backend/wayland}/producer.rs | 1 + src/backend/windows.rs | 2 + src/backend/windows/consumer.rs | 45 ++++++++++++++++++++++ src/backend/windows/producer.rs | 7 ++++ src/event.rs | 2 - src/lib.rs | 2 + src/main.rs | 22 ++++++++--- 12 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 src/backend.rs create mode 100644 src/backend/wayland.rs rename src/{event => backend/wayland}/consumer.rs (99%) rename src/{event => backend/wayland}/producer.rs (99%) create mode 100644 src/backend/windows.rs create mode 100644 src/backend/windows/consumer.rs create mode 100644 src/backend/windows/producer.rs diff --git a/Cargo.lock b/Cargo.lock index 87da707..1c73f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,6 +244,7 @@ dependencies = [ "wayland-protocols", "wayland-protocols-misc", "wayland-protocols-wlr", + "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c7140a0..2fdb92d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -wayland-client = { git="https://github.com/Smithay/wayland-rs.git" } -wayland-protocols = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "staging", "unstable"] } -wayland-protocols-wlr = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "server"] } -wayland-protocols-misc = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "server"] } tempfile = "3.2" trust-dns-resolver = "0.22" memmap = "0.7" @@ -17,3 +13,12 @@ toml = "0.5" serde = "1.0" serde_derive = "1.0" threadpool = "1.8" + +[target.'cfg(unix)'.dependencies] +wayland-client = { git="https://github.com/Smithay/wayland-rs.git" } +wayland-protocols = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "staging", "unstable"] } +wayland-protocols-wlr = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "server"] } +wayland-protocols-misc = { git="https://github.com/Smithay/wayland-rs.git", features=["client", "server"] } + +[target.'cfg(windows)'.dependencies] +winapi = { version = "0.3.9", features = ["winuser"] } diff --git a/src/backend.rs b/src/backend.rs new file mode 100644 index 0000000..3317768 --- /dev/null +++ b/src/backend.rs @@ -0,0 +1,2 @@ +pub mod windows; +pub mod wayland; diff --git a/src/backend/wayland.rs b/src/backend/wayland.rs new file mode 100644 index 0000000..934046a --- /dev/null +++ b/src/backend/wayland.rs @@ -0,0 +1,2 @@ +pub mod consumer; +pub mod producer; diff --git a/src/event/consumer.rs b/src/backend/wayland/consumer.rs similarity index 99% rename from src/event/consumer.rs rename to src/backend/wayland/consumer.rs index 3c6301d..acaba17 100644 --- a/src/event/consumer.rs +++ b/src/backend/wayland/consumer.rs @@ -1,3 +1,4 @@ +#![cfg(unix)] use crate::client::{Client, ClientHandle}; use crate::request::{self, Request}; use std::collections::HashMap; diff --git a/src/event/producer.rs b/src/backend/wayland/producer.rs similarity index 99% rename from src/event/producer.rs rename to src/backend/wayland/producer.rs index 8cb8f51..25f4bf7 100644 --- a/src/event/producer.rs +++ b/src/backend/wayland/producer.rs @@ -1,3 +1,4 @@ +#![cfg(unix)] use crate::{ client::{Client, ClientHandle, Position}, request, diff --git a/src/backend/windows.rs b/src/backend/windows.rs new file mode 100644 index 0000000..934046a --- /dev/null +++ b/src/backend/windows.rs @@ -0,0 +1,2 @@ +pub mod consumer; +pub mod producer; diff --git a/src/backend/windows/consumer.rs b/src/backend/windows/consumer.rs new file mode 100644 index 0000000..6b0ff68 --- /dev/null +++ b/src/backend/windows/consumer.rs @@ -0,0 +1,45 @@ +use std::sync::mpsc::Receiver; + +use winapi::{self, um::winuser::{INPUT, LPINPUT, INPUT_MOUSE, MOUSEINPUT, MOUSEEVENTF_MOVE}}; + +use crate::{event::Event, client::{Client, ClientHandle}}; + +fn rel_mouse(dx: i32, dy: i32) { + let mi = MOUSEINPUT { + dx, + dy, + mouseData: 0, + dwFlags: MOUSEEVENTF_MOVE, + time: 0, + dwExtraInfo: 0, + }; + + unsafe { + let mut input = INPUT { + type_: INPUT_MOUSE, + u: std::mem::transmute(mi), + }; + + winapi::um::winuser::SendInput(1 as u32, &mut input as LPINPUT, std::mem::size_of::() as i32); + } +} + + +pub fn run(event_rx: Receiver<(Event, ClientHandle)>, _clients: Vec) { + loop { + match event_rx.recv().expect("event receiver unavailable").0 { + Event::Pointer(pointer_event) => { + match pointer_event { + crate::event::PointerEvent::Motion { time: _, relative_x, relative_y } => { + rel_mouse(relative_x as i32, relative_y as i32); + }, + crate::event::PointerEvent::Button { .. } => {}, + crate::event::PointerEvent::Axis { .. } => {}, + crate::event::PointerEvent::Frame { } => {}, + } + }, + Event::Keyboard(_) => {}, + Event::Release() => {}, + } + } +} diff --git a/src/backend/windows/producer.rs b/src/backend/windows/producer.rs new file mode 100644 index 0000000..90d15a5 --- /dev/null +++ b/src/backend/windows/producer.rs @@ -0,0 +1,7 @@ +use std::sync::mpsc::SyncSender; + +use crate::{event::Event, client::{ClientHandle, Client}, request::Server}; + +pub fn run(_produce_tx: SyncSender<(Event, ClientHandle)>, _server: Server, _clients: Vec) { + +} diff --git a/src/event.rs b/src/event.rs index b73e1e6..d8bc0a9 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,7 +1,5 @@ use std::{error::Error, fmt}; -pub mod consumer; -pub mod producer; pub mod server; pub enum PointerEvent { diff --git a/src/lib.rs b/src/lib.rs index 507ebb8..62b258e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,5 @@ pub mod config; pub mod dns; pub mod event; pub mod request; + +pub mod backend; diff --git a/src/main.rs b/src/main.rs index 1536a66..51356a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,15 @@ use std::{net::SocketAddr, sync::mpsc, thread}; use lan_mouse::{ client::{ClientManager, Position}, - config, dns, - event::{self, consumer, producer}, - request, + config, dns, event, request, }; +#[cfg(windows)] +use lan_mouse::backend::windows; + +#[cfg(unix)] +use lan_mouse::backend::wayland; + fn add_client(client_manager: &mut ClientManager, client: &config::Client, pos: Position) { let ip = match client.ip { Some(ip) => ip, @@ -60,11 +64,15 @@ pub fn main() { let (request_server, request_thread) = request::Server::listen(port).unwrap(); let clients = client_manager.get_clients(); + // start producing and consuming events let event_producer = thread::Builder::new() .name("event producer".into()) .spawn(|| { - producer::run(produce_tx, request_server, clients); + #[cfg(windows)] + windows::producer::run(produce_tx, request_server, clients); + #[cfg(unix)] + wayland::producer::run(produce_tx, request_server, clients); }) .unwrap(); @@ -72,7 +80,11 @@ pub fn main() { let event_consumer = thread::Builder::new() .name("event consumer".into()) .spawn(|| { - consumer::run(consume_rx, clients); + #[cfg(windows)] + windows::consumer::run(consume_rx, clients); + + #[cfg(unix)] + wayland::consumer::run(consume_rx, clients); }) .unwrap();