From d2a554cc8fccd0152f9d29d2eae4fd0e634a8502 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Thu, 15 Sep 2022 02:16:02 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 359 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 18 +++ src/bin/client.rs | 126 ++++++++++++++++ src/bin/server.rs | 9 ++ 5 files changed, 513 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/bin/client.rs create mode 100644 src/bin/server.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3b15d07 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,359 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "dlib" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac1b7517328c04c2aa68422fc60a41b92208182142ed04a25879c26c8f878794" +dependencies = [ + "libloading", +] + +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + +[[package]] +name = "fastrand" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" +dependencies = [ + "instant", +] + +[[package]] +name = "futures-channel" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "lan-mouse" +version = "0.1.0" +dependencies = [ + "tempfile", + "wayland-client", + "wayland-protocols", + "wayland-protocols-wlr", +] + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + +[[package]] +name = "libloading" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + +[[package]] +name = "nix" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "195cdbc1741b8134346d515b3a56a1c94b0912758009cfd53f99ea0f57b065fc" +dependencies = [ + "bitflags", + "cfg-if", + "libc", + "memoffset", +] + +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quick-xml" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11bafc859c6815fbaffbbbf4229ecb767ac913fecb27f9ad4343662e9ef099ea" +dependencies = [ + "memchr", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +dependencies = [ + "cfg-if", + "fastrand", + "libc", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "thiserror" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c53f98874615aea268107765aa1ed8f6116782501d18e53d08b471733bea6c85" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8b463991b4eab2d801e724172285ec4195c650e8ec79b149e6c2a8e6dd3f783" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "wayland-backend" +version = "0.1.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "cc", + "downcast-rs", + "nix", + "scoped-tls", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.30.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "nix", + "thiserror", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols" +version = "0.30.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-scanner", + "wayland-server", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.1.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", + "wayland-server", +] + +[[package]] +name = "wayland-scanner" +version = "0.30.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", + "syn", +] + +[[package]] +name = "wayland-server" +version = "0.30.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "bitflags", + "downcast-rs", + "nix", + "thiserror", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-sys" +version = "0.30.0-beta.9" +source = "git+https://github.com/Smithay/wayland-rs.git#fa4be85e8f3b57c8b6e8dd2476d6b4734b810e59" +dependencies = [ + "dlib", + "log", + "pkg-config", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..102f07e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "lan-mouse" +version = "0.1.0" +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"] } +tempfile = "3.2" + +[[bin]] +name = "client" + +[[bin]] +name = "server" diff --git a/src/bin/client.rs b/src/bin/client.rs new file mode 100644 index 0000000..efcee76 --- /dev/null +++ b/src/bin/client.rs @@ -0,0 +1,126 @@ +use std::{net::UdpSocket, f64::consts::PI}; +use std::io::{self, Write}; + +use wayland_protocols_wlr::virtual_pointer::v1::client::{ + zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1 as Vp, + zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1 as VpManager, +}; + +use wayland_client::{ + protocol::wl_registry, + Connection, Dispatch, QueueHandle, EventQueue, +}; + +// App State, implements Dispatch event handlers +struct AppData{ + vpm: Option, +} + +// Implement `Dispatch event handler +// user-data = () +impl Dispatch for AppData { + fn event( + app: &mut Self, + registry: &wl_registry::WlRegistry, + event: wl_registry::Event, + _: &(), + _: &Connection, + qh: &QueueHandle, + ) { + // Match global event to get globals after requesting them in main + if let wl_registry::Event::Global { name, interface, .. } = event { + // println!("[{}] {} (v{})", name, interface, version); + match &interface[..] { + "zwlr_virtual_pointer_manager_v1" => { // virtual pointer protocol + let vpm = registry.bind::(name, 1, qh, ()); // get the vp manager + app.vpm = Some(vpm); // save it to app state + }, + _ => {} + } + } + } +} + + +// The main function of our program +fn main() { + // establish connection via environment-provided configuration. + let conn = Connection::connect_to_env().unwrap(); + + // Retrieve the wayland display object + let display = conn.display(); + + // Create an event queue for our event processing + let mut event_queue = conn.new_event_queue(); + let qh = event_queue.handle(); + + // Create a wl_registry object by sending the wl_display.get_registry request + let _registry = display.get_registry(&qh, ()); + + let mut app_data = AppData { vpm: None }; + + // use roundtrip to process this event synchronously + event_queue.roundtrip(&mut app_data).unwrap(); + if let Some(vpm) = app_data.vpm { + let pointer: Vp = vpm.create_virtual_pointer(None, &qh, ()); + udp_loop(pointer, event_queue).unwrap(); + println!(); + } else { + panic!("zwlr_virtual_pointer_manager_v1 protocol required") + }; +} + +/// main loop handling udp packets +fn udp_loop(pointer: Vp, q: EventQueue) -> std::io::Result<()>{ + let socket = UdpSocket::bind("0.0.0.0:42069")?; + // we don't care about possible dropped packets for now + + let mut buf = [0; 0]; + let rps = 1.0; + let rpms = rps * 0.001; + let radpms = rpms * 2.0 * PI; + let mut rad = 0_f64; + let mut time = 0; + loop { + let (_amt, _src) = socket.recv_from(&mut buf)?; + + let x = rad.cos(); + let y = rad.sin(); + + let scale = 100.0; + + pointer.motion(time, x * scale * radpms, y * scale * radpms); + q.flush().unwrap(); + rad += radpms; + rad %= 2.0*PI; + time+=1; + print!("{}\r", time); + io::stdout().flush().unwrap(); + } +} + +impl Dispatch for AppData { + fn event( + _: &mut Self, + _: &VpManager, + _: ::Event, + _: &(), + _: &Connection, + _: &QueueHandle, + ) { + // nothing to do here since no events are defined for VpManager + } +} + +impl Dispatch for AppData { + fn event( + _: &mut Self, + _: &Vp, + _: ::Event, + _: &(), + _: &Connection, + _: &QueueHandle, + ) { + // no events defined for vp either + } +} diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..cec6727 --- /dev/null +++ b/src/bin/server.rs @@ -0,0 +1,9 @@ +use std::{net::UdpSocket, thread, time::Duration}; + +fn main() { + let socket = UdpSocket::bind("127.0.0.1:42070").expect("couldn't bind to address"); + loop { + socket.send_to(&[0; 0], "127.0.0.1:42069").expect("couldn't send data"); + thread::sleep(Duration::from_millis(1)); + } +}