mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-29 16:10:54 +03:00
transmit button events + stub for keyboard
This commit is contained in:
@@ -1,27 +1,38 @@
|
||||
use std::net::UdpSocket;
|
||||
use lan_mouse::protocol;
|
||||
|
||||
use wayland_protocols_wlr::virtual_pointer::v1::client::{
|
||||
zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1 as VpManager,
|
||||
zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1 as Vp,
|
||||
};
|
||||
|
||||
use wayland_client::{protocol::wl_registry, Connection, Dispatch, EventQueue, QueueHandle};
|
||||
use wayland_protocols_misc::zwp_virtual_keyboard_v1::client::{
|
||||
zwp_virtual_keyboard_manager_v1::ZwpVirtualKeyboardManagerV1 as VkManager,
|
||||
zwp_virtual_keyboard_v1::ZwpVirtualKeyboardV1 as Vk,
|
||||
};
|
||||
|
||||
use wayland_client::{
|
||||
protocol::{wl_registry, wl_seat, wl_keyboard::KeyState},
|
||||
Connection, Dispatch, EventQueue, QueueHandle,
|
||||
};
|
||||
|
||||
// App State, implements Dispatch event handlers
|
||||
struct AppData {
|
||||
struct App {
|
||||
vpm: Option<VpManager>,
|
||||
vkm: Option<VkManager>,
|
||||
seat: Option<wl_seat::WlSeat>,
|
||||
}
|
||||
|
||||
// Implement `Dispatch<WlRegistry, ()> event handler
|
||||
// user-data = ()
|
||||
impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
|
||||
impl Dispatch<wl_registry::WlRegistry, ()> for App {
|
||||
fn event(
|
||||
app: &mut Self,
|
||||
registry: &wl_registry::WlRegistry,
|
||||
event: wl_registry::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
qh: &QueueHandle<AppData>,
|
||||
qh: &QueueHandle<App>,
|
||||
) {
|
||||
// Match global event to get globals after requesting them in main
|
||||
if let wl_registry::Event::Global {
|
||||
@@ -30,10 +41,14 @@ impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
|
||||
{
|
||||
// println!("[{}] {} (v{})", name, interface, version);
|
||||
match &interface[..] {
|
||||
"wl_seat" => {
|
||||
app.seat = Some(registry.bind::<wl_seat::WlSeat, _, _>(name, 1, qh, ()));
|
||||
}
|
||||
"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
|
||||
app.vpm = Some(registry.bind::<VpManager, _, _>(name, 1, qh, ()));
|
||||
}
|
||||
"zwp_virtual_keyboard_manager_v1" => {
|
||||
app.vkm = Some(registry.bind::<VkManager, _, _>(name, 1, qh, ()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -56,39 +71,61 @@ fn main() {
|
||||
// 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 };
|
||||
let mut app = App {
|
||||
vpm: None,
|
||||
vkm: None,
|
||||
seat: 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")
|
||||
};
|
||||
event_queue.roundtrip(&mut app).unwrap();
|
||||
|
||||
let vpm = app.vpm.unwrap();
|
||||
let vkm = app.vkm.unwrap();
|
||||
let seat = app.seat.unwrap();
|
||||
let pointer: Vp = vpm.create_virtual_pointer(None, &qh, ());
|
||||
let keyboard: Vk = vkm.create_virtual_keyboard(&seat, &qh, ());
|
||||
udp_loop(&pointer, &keyboard, event_queue).unwrap();
|
||||
println!();
|
||||
}
|
||||
|
||||
/// main loop handling udp packets
|
||||
fn udp_loop(pointer: Vp, q: EventQueue<AppData>) -> std::io::Result<()> {
|
||||
fn udp_loop(pointer: &Vp, keyboard: &Vk, q: EventQueue<App>) -> 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 = [0u8; 20];
|
||||
let mut buf = [0u8; 21];
|
||||
loop {
|
||||
let (amt, _src) = socket.recv_from(&mut buf)?;
|
||||
assert!(amt == 20);
|
||||
let (_amt, _src) = socket.recv_from(&mut buf)?;
|
||||
|
||||
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());
|
||||
match protocol::Event::decode(buf) {
|
||||
protocol::Event::Mouse { t, x, y } => {
|
||||
pointer.motion(t, x, y);
|
||||
}
|
||||
protocol::Event::Button { t, b, s } => {
|
||||
pointer.button(t, b, s);
|
||||
}
|
||||
protocol::Event::Axis { t, a, v } => {
|
||||
pointer.axis(t, a, v);
|
||||
}
|
||||
protocol::Event::Key { t, k, s } => {
|
||||
// TODO send keymap fist
|
||||
// keyboard.key(t, k, match s {
|
||||
// KeyState::Released => 0,
|
||||
// KeyState::Pressed => 1,
|
||||
// _ => 1,
|
||||
// });
|
||||
},
|
||||
protocol::Event::KeyModifier { mods_depressed, mods_latched, mods_locked, group } => {
|
||||
// keyboard.modifiers(mods_depressed, mods_latched, mods_locked, group);
|
||||
},
|
||||
}
|
||||
|
||||
pointer.motion(time, x, y);
|
||||
q.flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<VpManager, ()> for AppData {
|
||||
impl Dispatch<VpManager, ()> for App {
|
||||
fn event(
|
||||
_: &mut Self,
|
||||
_: &VpManager,
|
||||
@@ -101,7 +138,7 @@ impl Dispatch<VpManager, ()> for AppData {
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<Vp, ()> for AppData {
|
||||
impl Dispatch<Vp, ()> for App {
|
||||
fn event(
|
||||
_: &mut Self,
|
||||
_: &Vp,
|
||||
@@ -113,3 +150,42 @@ impl Dispatch<Vp, ()> for AppData {
|
||||
// no events defined for vp either
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<VkManager, ()> for App {
|
||||
fn event(
|
||||
_: &mut Self,
|
||||
_: &VkManager,
|
||||
_: <VkManager as wayland_client::Proxy>::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<Vk, ()> for App {
|
||||
fn event(
|
||||
_: &mut Self,
|
||||
_: &Vk,
|
||||
_: <Vk as wayland_client::Proxy>::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<wl_seat::WlSeat, ()> for App {
|
||||
fn event(
|
||||
_: &mut Self,
|
||||
_: &wl_seat::WlSeat,
|
||||
_: <wl_seat::WlSeat as wayland_client::Proxy>::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user