mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-10 04:31:29 +03:00
formatting
This commit is contained in:
@@ -1,13 +1,29 @@
|
||||
use std::{os::{fd::{RawFd, FromRawFd}, unix::net::UnixStream}, collections::HashMap, time::{UNIX_EPOCH, SystemTime}, io};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io,
|
||||
os::{
|
||||
fd::{FromRawFd, RawFd},
|
||||
unix::net::UnixStream,
|
||||
},
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use futures::StreamExt;
|
||||
use ashpd::desktop::remote_desktop::{RemoteDesktop, DeviceType};
|
||||
use ashpd::desktop::remote_desktop::{DeviceType, RemoteDesktop};
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
|
||||
use reis::{ei::{self, handshake::ContextType, keyboard::KeyState, button::ButtonState}, tokio::EiEventStream, PendingRequestResult};
|
||||
use reis::{
|
||||
ei::{self, button::ButtonState, handshake::ContextType, keyboard::KeyState},
|
||||
tokio::EiEventStream,
|
||||
PendingRequestResult,
|
||||
};
|
||||
|
||||
use crate::{consumer::EventConsumer, event::Event, client::{ClientHandle, ClientEvent}};
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
consumer::EventConsumer,
|
||||
event::Event,
|
||||
};
|
||||
|
||||
pub struct LibeiConsumer {
|
||||
handshake: bool,
|
||||
@@ -32,10 +48,17 @@ async fn get_ei_fd() -> Result<RawFd, ashpd::Error> {
|
||||
let session = proxy.create_session().await?;
|
||||
|
||||
// I HATE EVERYTHING, THIS TOOK 8 HOURS OF DEBUGGING
|
||||
proxy.select_devices(&session,
|
||||
DeviceType::Pointer | DeviceType::Keyboard |DeviceType::Touchscreen).await?;
|
||||
proxy
|
||||
.select_devices(
|
||||
&session,
|
||||
DeviceType::Pointer | DeviceType::Keyboard | DeviceType::Touchscreen,
|
||||
)
|
||||
.await?;
|
||||
|
||||
proxy.start(&session, &ashpd::WindowIdentifier::default()).await?.response()?;
|
||||
proxy
|
||||
.start(&session, &ashpd::WindowIdentifier::default())
|
||||
.await?
|
||||
.response()?;
|
||||
proxy.connect_to_eis(&session).await
|
||||
}
|
||||
|
||||
@@ -59,7 +82,8 @@ impl LibeiConsumer {
|
||||
let events = EiEventStream::new(context.clone())?;
|
||||
return Ok(Self {
|
||||
handshake: false,
|
||||
context, events,
|
||||
context,
|
||||
events,
|
||||
pointer: None,
|
||||
button: None,
|
||||
scroll: None,
|
||||
@@ -72,32 +96,59 @@ impl LibeiConsumer {
|
||||
capability_mask: 0,
|
||||
sequence: 0,
|
||||
serial: 0,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventConsumer for LibeiConsumer {
|
||||
async fn consume(&mut self, event: Event, _client_handle: ClientHandle) {
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_micros() as u64;
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_micros() as u64;
|
||||
match event {
|
||||
Event::Pointer(p) => match p {
|
||||
crate::event::PointerEvent::Motion { time:_, relative_x, relative_y } => {
|
||||
if !self.has_pointer { return }
|
||||
crate::event::PointerEvent::Motion {
|
||||
time: _,
|
||||
relative_x,
|
||||
relative_y,
|
||||
} => {
|
||||
if !self.has_pointer {
|
||||
return;
|
||||
}
|
||||
if let Some((d, p)) = self.pointer.as_mut() {
|
||||
p.motion_relative(relative_x as f32, relative_y as f32);
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
},
|
||||
crate::event::PointerEvent::Button { time: _, button, state } => {
|
||||
if !self.has_button { return }
|
||||
}
|
||||
crate::event::PointerEvent::Button {
|
||||
time: _,
|
||||
button,
|
||||
state,
|
||||
} => {
|
||||
if !self.has_button {
|
||||
return;
|
||||
}
|
||||
if let Some((d, b)) = self.button.as_mut() {
|
||||
b.button(button, match state { 0 => ButtonState::Released, _ => ButtonState::Press });
|
||||
b.button(
|
||||
button,
|
||||
match state {
|
||||
0 => ButtonState::Released,
|
||||
_ => ButtonState::Press,
|
||||
},
|
||||
);
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
},
|
||||
crate::event::PointerEvent::Axis { time: _, axis, value } => {
|
||||
if !self.has_scroll { return }
|
||||
}
|
||||
crate::event::PointerEvent::Axis {
|
||||
time: _,
|
||||
axis,
|
||||
value,
|
||||
} => {
|
||||
if !self.has_scroll {
|
||||
return;
|
||||
}
|
||||
if let Some((d, s)) = self.scroll.as_mut() {
|
||||
match axis {
|
||||
0 => s.scroll(0., value as f32),
|
||||
@@ -105,18 +156,30 @@ impl EventConsumer for LibeiConsumer {
|
||||
}
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
},
|
||||
crate::event::PointerEvent::Frame { } => {},
|
||||
}
|
||||
crate::event::PointerEvent::Frame {} => {}
|
||||
},
|
||||
Event::Keyboard(k) => match k {
|
||||
crate::event::KeyboardEvent::Key { time: _, key, state } => {
|
||||
if !self.has_keyboard { return }
|
||||
crate::event::KeyboardEvent::Key {
|
||||
time: _,
|
||||
key,
|
||||
state,
|
||||
} => {
|
||||
if !self.has_keyboard {
|
||||
return;
|
||||
}
|
||||
if let Some((d, k)) = &mut self.keyboard {
|
||||
k.key(key, match state { 0 => KeyState::Released, _ => KeyState::Press });
|
||||
k.key(
|
||||
key,
|
||||
match state {
|
||||
0 => KeyState::Released,
|
||||
_ => KeyState::Press,
|
||||
},
|
||||
);
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
},
|
||||
crate::event::KeyboardEvent::Modifiers { .. } => { },
|
||||
}
|
||||
crate::event::KeyboardEvent::Modifiers { .. } => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
@@ -130,13 +193,17 @@ impl EventConsumer for LibeiConsumer {
|
||||
};
|
||||
let event = match event {
|
||||
PendingRequestResult::Request(result) => result,
|
||||
PendingRequestResult::ProtocolError(e) => return Err(anyhow!("libei protocol violation: {e}")),
|
||||
PendingRequestResult::ProtocolError(e) => {
|
||||
return Err(anyhow!("libei protocol violation: {e}"))
|
||||
}
|
||||
PendingRequestResult::InvalidObject(e) => return Err(anyhow!("invalid object {e}")),
|
||||
};
|
||||
match event {
|
||||
ei::Event::Handshake(handshake, request) => match request {
|
||||
ei::handshake::Event::HandshakeVersion { version } => {
|
||||
if self.handshake { return Ok(()) }
|
||||
if self.handshake {
|
||||
return Ok(());
|
||||
}
|
||||
log::info!("libei version {}", version);
|
||||
// sender means we are sending events _to_ the eis server
|
||||
handshake.handshake_version(version); // FIXME
|
||||
@@ -163,8 +230,8 @@ impl EventConsumer for LibeiConsumer {
|
||||
connection.sync(1);
|
||||
self.serial = serial;
|
||||
}
|
||||
_ => unreachable!()
|
||||
}
|
||||
_ => unreachable!(),
|
||||
},
|
||||
ei::Event::Connection(_connection, request) => match request {
|
||||
ei::connection::Event::Seat { seat } => {
|
||||
log::debug!("connected to seat: {seat:?}");
|
||||
@@ -172,20 +239,45 @@ impl EventConsumer for LibeiConsumer {
|
||||
ei::connection::Event::Ping { ping } => {
|
||||
ping.done(0);
|
||||
}
|
||||
ei::connection::Event::Disconnected { last_serial: _, reason, explanation } => {
|
||||
ei::connection::Event::Disconnected {
|
||||
last_serial: _,
|
||||
reason,
|
||||
explanation,
|
||||
} => {
|
||||
log::debug!("ei - disconnected: reason: {reason:?}: {explanation}")
|
||||
}
|
||||
ei::connection::Event::InvalidObject { last_serial, invalid_id } => {
|
||||
return Err(anyhow!("invalid object: id: {invalid_id}, serial: {last_serial}"));
|
||||
ei::connection::Event::InvalidObject {
|
||||
last_serial,
|
||||
invalid_id,
|
||||
} => {
|
||||
return Err(anyhow!(
|
||||
"invalid object: id: {invalid_id}, serial: {last_serial}"
|
||||
));
|
||||
}
|
||||
_ => unreachable!()
|
||||
}
|
||||
_ => unreachable!(),
|
||||
},
|
||||
ei::Event::Device(device, request) => match request {
|
||||
ei::device::Event::Destroyed { serial } => { log::debug!("device destroyed: {device:?} - serial: {serial}") },
|
||||
ei::device::Event::Name { name } => {log::debug!("device name: {name}")},
|
||||
ei::device::Event::DeviceType { device_type } => log::debug!("device type: {device_type:?}"),
|
||||
ei::device::Event::Dimensions { width, height } => log::debug!("device dimensions: {width}x{height}"),
|
||||
ei::device::Event::Region { offset_x, offset_y, width, hight, scale } => log::debug!("device region: {width}x{hight} @ ({offset_x},{offset_y}), scale: {scale}"),
|
||||
ei::device::Event::Destroyed { serial } => {
|
||||
log::debug!("device destroyed: {device:?} - serial: {serial}")
|
||||
}
|
||||
ei::device::Event::Name { name } => {
|
||||
log::debug!("device name: {name}")
|
||||
}
|
||||
ei::device::Event::DeviceType { device_type } => {
|
||||
log::debug!("device type: {device_type:?}")
|
||||
}
|
||||
ei::device::Event::Dimensions { width, height } => {
|
||||
log::debug!("device dimensions: {width}x{height}")
|
||||
}
|
||||
ei::device::Event::Region {
|
||||
offset_x,
|
||||
offset_y,
|
||||
width,
|
||||
hight,
|
||||
scale,
|
||||
} => log::debug!(
|
||||
"device region: {width}x{hight} @ ({offset_x},{offset_y}), scale: {scale}"
|
||||
),
|
||||
ei::device::Event::Interface { object } => {
|
||||
log::debug!("device interface: {object:?}");
|
||||
if object.interface().eq("ei_pointer") {
|
||||
@@ -204,31 +296,31 @@ impl EventConsumer for LibeiConsumer {
|
||||
}
|
||||
ei::device::Event::Done => {
|
||||
log::debug!("device: done {device:?}");
|
||||
},
|
||||
}
|
||||
ei::device::Event::Resumed { serial } => {
|
||||
self.serial = serial;
|
||||
device.start_emulating(serial, self.sequence);
|
||||
self.sequence += 1;
|
||||
log::debug!("resumed: {device:?}");
|
||||
if let Some((d,_)) = &mut self.pointer {
|
||||
if let Some((d, _)) = &mut self.pointer {
|
||||
if d == &device {
|
||||
log::debug!("pointer resumed {serial}");
|
||||
self.has_pointer = true;
|
||||
}
|
||||
}
|
||||
if let Some((d,_)) = &mut self.button {
|
||||
if let Some((d, _)) = &mut self.button {
|
||||
if d == &device {
|
||||
log::debug!("button resumed {serial}");
|
||||
self.has_button = true;
|
||||
}
|
||||
}
|
||||
if let Some((d,_)) = &mut self.scroll {
|
||||
if let Some((d, _)) = &mut self.scroll {
|
||||
if d == &device {
|
||||
log::debug!("scroll resumed {serial}");
|
||||
self.has_scroll = true;
|
||||
}
|
||||
}
|
||||
if let Some((d,_)) = &mut self.keyboard {
|
||||
if let Some((d, _)) = &mut self.keyboard {
|
||||
if d == &device {
|
||||
log::debug!("keyboard resumed {serial}");
|
||||
self.has_keyboard = true;
|
||||
@@ -239,38 +331,44 @@ impl EventConsumer for LibeiConsumer {
|
||||
self.has_pointer = false;
|
||||
self.has_button = false;
|
||||
self.serial = serial;
|
||||
},
|
||||
ei::device::Event::StartEmulating { serial, sequence } => log::debug!("start emulating {serial}, {sequence}"),
|
||||
ei::device::Event::StopEmulating { serial } => log::debug!("stop emulating {serial}"),
|
||||
}
|
||||
ei::device::Event::StartEmulating { serial, sequence } => {
|
||||
log::debug!("start emulating {serial}, {sequence}")
|
||||
}
|
||||
ei::device::Event::StopEmulating { serial } => {
|
||||
log::debug!("stop emulating {serial}")
|
||||
}
|
||||
ei::device::Event::Frame { serial, timestamp } => {
|
||||
log::debug!("frame: {serial}, {timestamp}");
|
||||
}
|
||||
ei::device::Event::RegionMappingId { mapping_id } => log::debug!("RegionMappingId {mapping_id}"),
|
||||
ei::device::Event::RegionMappingId { mapping_id } => {
|
||||
log::debug!("RegionMappingId {mapping_id}")
|
||||
}
|
||||
e => log::debug!("invalid event: {e:?}"),
|
||||
}
|
||||
},
|
||||
ei::Event::Seat(seat, request) => match request {
|
||||
ei::seat::Event::Destroyed { serial } => {
|
||||
self.serial = serial;
|
||||
log::debug!("seat destroyed: {seat:?}");
|
||||
},
|
||||
}
|
||||
ei::seat::Event::Name { name } => {
|
||||
log::debug!("seat name: {name}");
|
||||
},
|
||||
}
|
||||
ei::seat::Event::Capability { mask, interface } => {
|
||||
log::debug!("seat capabilities: {mask}, interface: {interface:?}");
|
||||
self.capabilities.insert(interface, mask);
|
||||
self.capability_mask |= mask;
|
||||
},
|
||||
}
|
||||
ei::seat::Event::Done => {
|
||||
log::debug!("seat done");
|
||||
log::debug!("binding capabilities: {}", self.capability_mask);
|
||||
seat.bind(self.capability_mask);
|
||||
},
|
||||
}
|
||||
ei::seat::Event::Device { device } => {
|
||||
log::debug!("seat: new device - {device:?}");
|
||||
},
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
},
|
||||
e => log::debug!("unhandled event: {e:?}"),
|
||||
}
|
||||
self.context.flush()?;
|
||||
@@ -281,4 +379,3 @@ impl EventConsumer for LibeiConsumer {
|
||||
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user