mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-28 07:30:54 +03:00
implement server side code (as a window for the time being)
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
use std::{net::UdpSocket, f64::consts::PI};
|
||||
use std::io::{self, Write};
|
||||
use std::{f64::consts::PI, net::UdpSocket};
|
||||
|
||||
use wayland_protocols_wlr::virtual_pointer::v1::client::{
|
||||
zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1 as Vp,
|
||||
zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1 as VpManager,
|
||||
zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1 as Vp,
|
||||
};
|
||||
|
||||
use wayland_client::{
|
||||
protocol::wl_registry,
|
||||
Connection, Dispatch, QueueHandle, EventQueue,
|
||||
};
|
||||
use wayland_client::{protocol::wl_registry, Connection, Dispatch, EventQueue, QueueHandle};
|
||||
|
||||
// App State, implements Dispatch event handlers
|
||||
struct AppData{
|
||||
struct AppData {
|
||||
vpm: Option<VpManager>,
|
||||
}
|
||||
|
||||
@@ -28,20 +25,23 @@ impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
|
||||
qh: &QueueHandle<AppData>,
|
||||
) {
|
||||
// Match global event to get globals after requesting them in main
|
||||
if let wl_registry::Event::Global { name, interface, .. } = event {
|
||||
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::<VpManager, _, _>(name, 1, qh, ()); // get the vp manager
|
||||
app.vpm = Some(vpm); // save it to app state
|
||||
},
|
||||
"zwlr_virtual_pointer_manager_v1" => {
|
||||
// virtual pointer protocol
|
||||
let vpm = registry.bind::<VpManager, _, _>(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.
|
||||
@@ -58,7 +58,7 @@ fn main() {
|
||||
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 {
|
||||
@@ -71,31 +71,21 @@ fn main() {
|
||||
}
|
||||
|
||||
/// main loop handling udp packets
|
||||
fn udp_loop(pointer: Vp, q: EventQueue<AppData>) -> std::io::Result<()>{
|
||||
fn udp_loop(pointer: Vp, q: EventQueue<AppData>) -> 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;
|
||||
let mut buf = [0u8; 20];
|
||||
loop {
|
||||
let (_amt, _src) = socket.recv_from(&mut buf)?;
|
||||
let (amt, _src) = socket.recv_from(&mut buf)?;
|
||||
assert!(amt == 20);
|
||||
|
||||
let x = rad.cos();
|
||||
let y = rad.sin();
|
||||
let time: u32 = u32::from_ne_bytes(buf[0..4].try_into().unwrap());
|
||||
let x: f64 = f64::from_ne_bytes(buf[4..12].try_into().unwrap());
|
||||
let y: f64 = f64::from_ne_bytes(buf[12..20].try_into().unwrap());
|
||||
|
||||
let scale = 100.0;
|
||||
|
||||
pointer.motion(time, x * scale * radpms, y * scale * radpms);
|
||||
pointer.motion(time, x, y);
|
||||
q.flush().unwrap();
|
||||
rad += radpms;
|
||||
rad %= 2.0*PI;
|
||||
time+=1;
|
||||
print!("{}\r", time);
|
||||
io::stdout().flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user