mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-17 10:10:54 +03:00
Compare commits
7 Commits
input-emul
...
cleanup-ca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
092d875bc2 | ||
|
|
e67d820ee4 | ||
|
|
266ad28c6b | ||
|
|
096567640c | ||
|
|
8f7890c9be | ||
|
|
68361b25d1 | ||
|
|
22dc33367b |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1306,7 +1306,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lan-mouse"
|
name = "lan-mouse"
|
||||||
version = "0.9.0"
|
version = "0.9.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-channel",
|
"async-channel",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ members = ["input-capture", "input-emulation", "input-event"]
|
|||||||
[package]
|
[package]
|
||||||
name = "lan-mouse"
|
name = "lan-mouse"
|
||||||
description = "Software KVM Switch / mouse & keyboard sharing software for Local Area Networks"
|
description = "Software KVM Switch / mouse & keyboard sharing software for Local Area Networks"
|
||||||
version = "0.9.0"
|
version = "0.9.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
repository = "https://github.com/feschber/lan-mouse"
|
repository = "https://github.com/feschber/lan-mouse"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use input_event::Event;
|
|||||||
|
|
||||||
use crate::CaptureError;
|
use crate::CaptureError;
|
||||||
|
|
||||||
use super::{CaptureHandle, InputCapture, Position};
|
use super::{Capture, CaptureHandle, Position};
|
||||||
|
|
||||||
pub struct DummyInputCapture {}
|
pub struct DummyInputCapture {}
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ impl Default for DummyInputCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputCapture for DummyInputCapture {
|
impl Capture for DummyInputCapture {
|
||||||
async fn create(&mut self, _handle: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, _handle: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,32 @@
|
|||||||
use std::fmt::Display;
|
use std::{collections::HashSet, fmt::Display, task::Poll};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use futures::StreamExt;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
|
||||||
use input_event::Event;
|
use input_event::{scancode, Event, KeyboardEvent};
|
||||||
|
|
||||||
pub use error::{CaptureCreationError, CaptureError, InputCaptureError};
|
pub use error::{CaptureCreationError, CaptureError, InputCaptureError};
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||||
pub mod libei;
|
mod libei;
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub mod macos;
|
mod macos;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||||
pub mod wayland;
|
mod wayland;
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
pub mod windows;
|
mod windows;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||||
pub mod x11;
|
mod x11;
|
||||||
|
|
||||||
/// fallback input capture (does not produce events)
|
/// fallback input capture (does not produce events)
|
||||||
pub mod dummy;
|
mod dummy;
|
||||||
|
|
||||||
pub type CaptureHandle = u64;
|
pub type CaptureHandle = u64;
|
||||||
|
|
||||||
@@ -93,10 +94,79 @@ impl Display for Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct InputCapture {
|
||||||
|
capture: Box<dyn Capture>,
|
||||||
|
pressed_keys: HashSet<scancode::Linux>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputCapture {
|
||||||
|
/// create a new client with the given id
|
||||||
|
pub async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||||
|
self.capture.create(id, pos).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// destroy the client with the given id, if it exists
|
||||||
|
pub async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError> {
|
||||||
|
self.capture.destroy(id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// release mouse
|
||||||
|
pub async fn release(&mut self) -> Result<(), CaptureError> {
|
||||||
|
self.pressed_keys.clear();
|
||||||
|
self.capture.release().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// destroy the input capture
|
||||||
|
pub async fn terminate(&mut self) -> Result<(), CaptureError> {
|
||||||
|
self.capture.terminate().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// creates a new [`InputCapture`]
|
||||||
|
pub async fn new(backend: Option<Backend>) -> Result<Self, CaptureCreationError> {
|
||||||
|
let capture = create(backend).await?;
|
||||||
|
Ok(Self {
|
||||||
|
capture,
|
||||||
|
pressed_keys: HashSet::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// check whether the given keys are pressed
|
||||||
|
pub fn keys_pressed(&self, keys: &[scancode::Linux]) -> bool {
|
||||||
|
keys.iter().all(|k| self.pressed_keys.contains(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_pressed_keys(&mut self, key: u32, state: u8) {
|
||||||
|
if let Ok(scancode) = scancode::Linux::try_from(key) {
|
||||||
|
log::debug!("key: {key}, state: {state}, scancode: {scancode:?}");
|
||||||
|
match state {
|
||||||
|
1 => self.pressed_keys.insert(scancode),
|
||||||
|
_ => self.pressed_keys.remove(&scancode),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stream for InputCapture {
|
||||||
|
type Item = Result<(CaptureHandle, Event), CaptureError>;
|
||||||
|
|
||||||
|
fn poll_next(
|
||||||
|
mut self: std::pin::Pin<&mut Self>,
|
||||||
|
cx: &mut std::task::Context<'_>,
|
||||||
|
) -> Poll<Option<Self::Item>> {
|
||||||
|
match self.capture.poll_next_unpin(cx) {
|
||||||
|
Poll::Ready(e) => {
|
||||||
|
if let Some(Ok((_, Event::Keyboard(KeyboardEvent::Key { key, state, .. })))) = e {
|
||||||
|
self.update_pressed_keys(key, state);
|
||||||
|
}
|
||||||
|
Poll::Ready(e)
|
||||||
|
}
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait InputCapture:
|
trait Capture: Stream<Item = Result<(CaptureHandle, Event), CaptureError>> + Unpin {
|
||||||
Stream<Item = Result<(CaptureHandle, Event), CaptureError>> + Unpin
|
|
||||||
{
|
|
||||||
/// create a new client with the given id
|
/// create a new client with the given id
|
||||||
async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError>;
|
async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError>;
|
||||||
|
|
||||||
@@ -110,10 +180,10 @@ pub trait InputCapture:
|
|||||||
async fn terminate(&mut self) -> Result<(), CaptureError>;
|
async fn terminate(&mut self) -> Result<(), CaptureError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_backend(
|
async fn create_backend(
|
||||||
backend: Backend,
|
backend: Backend,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
Box<dyn InputCapture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
|
Box<dyn Capture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
|
||||||
CaptureCreationError,
|
CaptureCreationError,
|
||||||
> {
|
> {
|
||||||
match backend {
|
match backend {
|
||||||
@@ -131,10 +201,10 @@ pub async fn create_backend(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(
|
async fn create(
|
||||||
backend: Option<Backend>,
|
backend: Option<Backend>,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
Box<dyn InputCapture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
|
Box<dyn Capture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
|
||||||
CaptureCreationError,
|
CaptureCreationError,
|
||||||
> {
|
> {
|
||||||
if let Some(backend) = backend {
|
if let Some(backend) = backend {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ use input_event::Event;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
|
error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
|
||||||
CaptureHandle, InputCapture as LanMouseInputCapture, Position,
|
Capture as LanMouseInputCapture, CaptureHandle, Position,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* there is a bug in xdg-remote-desktop-portal-gnome / mutter that
|
/* there is a bug in xdg-remote-desktop-portal-gnome / mutter that
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::MacOSInputCaptureCreationError, CaptureError, CaptureHandle, InputCapture, Position,
|
error::MacOSInputCaptureCreationError, Capture, CaptureError, CaptureHandle, Position,
|
||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
@@ -24,7 +24,7 @@ impl Stream for MacOSInputCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputCapture for MacOSInputCapture {
|
impl Capture for MacOSInputCapture {
|
||||||
async fn create(&mut self, _id: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, _id: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use memmap::MmapOptions;
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
env,
|
env,
|
||||||
io::{self, ErrorKind},
|
io::{self, ErrorKind},
|
||||||
os::fd::{AsFd, OwnedFd, RawFd},
|
os::fd::{AsFd, RawFd},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{ready, Context, Poll},
|
task::{ready, Context, Poll},
|
||||||
};
|
};
|
||||||
@@ -14,7 +13,7 @@ use tokio::io::unix::AsyncFd;
|
|||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufWriter, Write},
|
io::{BufWriter, Write},
|
||||||
os::unix::prelude::{AsRawFd, FromRawFd},
|
os::unix::prelude::AsRawFd,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -59,15 +58,13 @@ use wayland_client::{
|
|||||||
Connection, Dispatch, DispatchError, EventQueue, QueueHandle, WEnum,
|
Connection, Dispatch, DispatchError, EventQueue, QueueHandle, WEnum,
|
||||||
};
|
};
|
||||||
|
|
||||||
use tempfile;
|
|
||||||
|
|
||||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||||
|
|
||||||
use crate::CaptureError;
|
use crate::CaptureError;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error::{LayerShellCaptureCreationError, WaylandBindError},
|
error::{LayerShellCaptureCreationError, WaylandBindError},
|
||||||
CaptureHandle, InputCapture, Position,
|
Capture, CaptureHandle, Position,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Globals {
|
struct Globals {
|
||||||
@@ -108,7 +105,7 @@ struct State {
|
|||||||
client_for_window: Vec<(Arc<Window>, CaptureHandle)>,
|
client_for_window: Vec<(Arc<Window>, CaptureHandle)>,
|
||||||
focused: Option<(Arc<Window>, CaptureHandle)>,
|
focused: Option<(Arc<Window>, CaptureHandle)>,
|
||||||
g: Globals,
|
g: Globals,
|
||||||
wayland_fd: OwnedFd,
|
wayland_fd: RawFd,
|
||||||
read_guard: Option<ReadEventsGuard>,
|
read_guard: Option<ReadEventsGuard>,
|
||||||
qh: QueueHandle<Self>,
|
qh: QueueHandle<Self>,
|
||||||
pending_events: VecDeque<(CaptureHandle, Event)>,
|
pending_events: VecDeque<(CaptureHandle, Event)>,
|
||||||
@@ -123,7 +120,7 @@ struct Inner {
|
|||||||
|
|
||||||
impl AsRawFd for Inner {
|
impl AsRawFd for Inner {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.state.wayland_fd.as_raw_fd()
|
self.state.wayland_fd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,10 +305,7 @@ impl WaylandInputCapture {
|
|||||||
// flush outgoing events
|
// flush outgoing events
|
||||||
queue.flush()?;
|
queue.flush()?;
|
||||||
|
|
||||||
// prepare reading wayland events
|
let wayland_fd = queue.as_fd().as_raw_fd();
|
||||||
let read_guard = queue.prepare_read().unwrap(); // there can not yet be events to dispatch
|
|
||||||
let wayland_fd = read_guard.connection_fd().try_clone_to_owned().unwrap();
|
|
||||||
std::mem::drop(read_guard);
|
|
||||||
|
|
||||||
let mut state = State {
|
let mut state = State {
|
||||||
pointer: None,
|
pointer: None,
|
||||||
@@ -565,7 +559,7 @@ impl Inner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputCapture for WaylandInputCapture {
|
impl Capture for WaylandInputCapture {
|
||||||
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||||
self.add_client(handle, pos);
|
self.add_client(handle, pos);
|
||||||
let inner = self.0.get_mut();
|
let inner = self.0.get_mut();
|
||||||
@@ -820,15 +814,6 @@ impl Dispatch<WlKeyboard, ()> for State {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wl_keyboard::Event::Keymap {
|
|
||||||
format: _,
|
|
||||||
fd,
|
|
||||||
size: _,
|
|
||||||
} => {
|
|
||||||
let fd = unsafe { &File::from_raw_fd(fd.as_raw_fd()) };
|
|
||||||
let _mmap = unsafe { MmapOptions::new().map_copy(fd).unwrap() };
|
|
||||||
// TODO keymap
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ use input_event::{
|
|||||||
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{CaptureError, CaptureHandle, InputCapture, Position};
|
use super::{Capture, CaptureError, CaptureHandle, Position};
|
||||||
|
|
||||||
enum Request {
|
enum Request {
|
||||||
Create(CaptureHandle, Position),
|
Create(CaptureHandle, Position),
|
||||||
@@ -64,7 +64,7 @@ unsafe fn signal_message_thread(event_type: EventType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputCapture for WindowsInputCapture {
|
impl Capture for WindowsInputCapture {
|
||||||
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use futures_core::Stream;
|
|||||||
|
|
||||||
use crate::CaptureError;
|
use crate::CaptureError;
|
||||||
|
|
||||||
use super::InputCapture;
|
use super::Capture;
|
||||||
use input_event::Event;
|
use input_event::Event;
|
||||||
|
|
||||||
use super::error::X11InputCaptureCreationError;
|
use super::error::X11InputCaptureCreationError;
|
||||||
@@ -20,7 +20,7 @@ impl X11InputCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputCapture for X11InputCapture {
|
impl Capture for X11InputCapture {
|
||||||
async fn create(&mut self, _id: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, _id: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,19 @@ use input_event::Event;
|
|||||||
|
|
||||||
use crate::error::EmulationError;
|
use crate::error::EmulationError;
|
||||||
|
|
||||||
use super::{EmulationHandle, InputEmulation};
|
use super::{Emulation, EmulationHandle};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DummyEmulation;
|
pub(crate) struct DummyEmulation;
|
||||||
|
|
||||||
impl DummyEmulation {
|
impl DummyEmulation {
|
||||||
pub fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputEmulation for DummyEmulation {
|
impl Emulation for DummyEmulation {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
|
|||||||
@@ -1,31 +1,34 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use std::fmt::Display;
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
fmt::Display,
|
||||||
|
};
|
||||||
|
|
||||||
use input_event::Event;
|
use input_event::{Event, KeyboardEvent};
|
||||||
|
|
||||||
pub use self::error::{EmulationCreationError, EmulationError, InputEmulationError};
|
pub use self::error::{EmulationCreationError, EmulationError, InputEmulationError};
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
pub mod windows;
|
mod windows;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||||
pub mod x11;
|
mod x11;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||||
pub mod wlroots;
|
mod wlroots;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||||
pub mod xdg_desktop_portal;
|
mod xdg_desktop_portal;
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||||
pub mod libei;
|
mod libei;
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub mod macos;
|
mod macos;
|
||||||
|
|
||||||
/// fallback input emulation (logs events)
|
/// fallback input emulation (logs events)
|
||||||
pub mod dummy;
|
mod dummy;
|
||||||
pub mod error;
|
mod error;
|
||||||
|
|
||||||
pub type EmulationHandle = u64;
|
pub type EmulationHandle = u64;
|
||||||
|
|
||||||
@@ -66,8 +69,166 @@ impl Display for Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct InputEmulation {
|
||||||
|
emulation: Box<dyn Emulation>,
|
||||||
|
handles: HashSet<EmulationHandle>,
|
||||||
|
pressed_keys: HashMap<EmulationHandle, HashSet<u32>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputEmulation {
|
||||||
|
async fn with_backend(backend: Backend) -> Result<InputEmulation, EmulationCreationError> {
|
||||||
|
let emulation: Box<dyn Emulation> = match backend {
|
||||||
|
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||||
|
Backend::Wlroots => Box::new(wlroots::WlrootsEmulation::new()?),
|
||||||
|
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||||
|
Backend::Libei => Box::new(libei::LibeiEmulation::new().await?),
|
||||||
|
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||||
|
Backend::X11 => Box::new(x11::X11Emulation::new()?),
|
||||||
|
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||||
|
Backend::Xdp => Box::new(xdg_desktop_portal::DesktopPortalEmulation::new().await?),
|
||||||
|
#[cfg(windows)]
|
||||||
|
Backend::Windows => Box::new(windows::WindowsEmulation::new()?),
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
Backend::MacOs => Box::new(macos::MacOSEmulation::new()?),
|
||||||
|
Backend::Dummy => Box::new(dummy::DummyEmulation::new()),
|
||||||
|
};
|
||||||
|
Ok(Self {
|
||||||
|
emulation,
|
||||||
|
handles: HashSet::new(),
|
||||||
|
pressed_keys: HashMap::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new(backend: Option<Backend>) -> Result<InputEmulation, EmulationCreationError> {
|
||||||
|
if let Some(backend) = backend {
|
||||||
|
let b = Self::with_backend(backend).await;
|
||||||
|
if b.is_ok() {
|
||||||
|
log::info!("using emulation backend: {backend}");
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
for backend in [
|
||||||
|
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||||
|
Backend::Wlroots,
|
||||||
|
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||||
|
Backend::Libei,
|
||||||
|
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||||
|
Backend::Xdp,
|
||||||
|
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||||
|
Backend::X11,
|
||||||
|
#[cfg(windows)]
|
||||||
|
Backend::Windows,
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
Backend::MacOs,
|
||||||
|
Backend::Dummy,
|
||||||
|
] {
|
||||||
|
match Self::with_backend(backend).await {
|
||||||
|
Ok(b) => {
|
||||||
|
log::info!("using emulation backend: {backend}");
|
||||||
|
return Ok(b);
|
||||||
|
}
|
||||||
|
Err(e) if e.cancelled_by_user() => return Err(e),
|
||||||
|
Err(e) => log::warn!("{e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(EmulationCreationError::NoAvailableBackend)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn consume(
|
||||||
|
&mut self,
|
||||||
|
event: Event,
|
||||||
|
handle: EmulationHandle,
|
||||||
|
) -> Result<(), EmulationError> {
|
||||||
|
match event {
|
||||||
|
Event::Keyboard(KeyboardEvent::Key { key, state, .. }) => {
|
||||||
|
// prevent double pressed / released keys
|
||||||
|
if self.update_pressed_keys(handle, key, state) {
|
||||||
|
self.emulation.consume(event, handle).await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ => self.emulation.consume(event, handle).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(&mut self, handle: EmulationHandle) -> bool {
|
||||||
|
if self.handles.insert(handle) {
|
||||||
|
self.pressed_keys.insert(handle, HashSet::new());
|
||||||
|
self.emulation.create(handle).await;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn destroy(&mut self, handle: EmulationHandle) {
|
||||||
|
let _ = self.release_keys(handle).await;
|
||||||
|
if self.handles.remove(&handle) {
|
||||||
|
self.pressed_keys.remove(&handle);
|
||||||
|
self.emulation.destroy(handle).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn terminate(&mut self) {
|
||||||
|
for handle in self.handles.iter().cloned().collect::<Vec<_>>() {
|
||||||
|
self.destroy(handle).await
|
||||||
|
}
|
||||||
|
self.emulation.terminate().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn release_keys(&mut self, handle: EmulationHandle) -> Result<(), EmulationError> {
|
||||||
|
if let Some(keys) = self.pressed_keys.get_mut(&handle) {
|
||||||
|
let keys = keys.drain().collect::<Vec<_>>();
|
||||||
|
for key in keys {
|
||||||
|
let event = Event::Keyboard(KeyboardEvent::Key {
|
||||||
|
time: 0,
|
||||||
|
key,
|
||||||
|
state: 0,
|
||||||
|
});
|
||||||
|
self.emulation.consume(event, handle).await?;
|
||||||
|
if let Ok(key) = input_event::scancode::Linux::try_from(key) {
|
||||||
|
log::warn!("releasing stuck key: {key:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let event = Event::Keyboard(KeyboardEvent::Modifiers {
|
||||||
|
mods_depressed: 0,
|
||||||
|
mods_latched: 0,
|
||||||
|
mods_locked: 0,
|
||||||
|
group: 0,
|
||||||
|
});
|
||||||
|
self.emulation.consume(event, handle).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_pressed_keys(&self, handle: EmulationHandle) -> bool {
|
||||||
|
self.pressed_keys
|
||||||
|
.get(&handle)
|
||||||
|
.is_some_and(|p| !p.is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// update the pressed_keys for the given handle
|
||||||
|
/// returns whether the event should be processed
|
||||||
|
fn update_pressed_keys(&mut self, handle: EmulationHandle, key: u32, state: u8) -> bool {
|
||||||
|
let Some(pressed_keys) = self.pressed_keys.get_mut(&handle) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if state == 0 {
|
||||||
|
// currently pressed => can release
|
||||||
|
pressed_keys.remove(&key)
|
||||||
|
} else {
|
||||||
|
// currently not pressed => can press
|
||||||
|
pressed_keys.insert(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait InputEmulation: Send {
|
trait Emulation: Send {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
@@ -77,64 +238,3 @@ pub trait InputEmulation: Send {
|
|||||||
async fn destroy(&mut self, handle: EmulationHandle);
|
async fn destroy(&mut self, handle: EmulationHandle);
|
||||||
async fn terminate(&mut self);
|
async fn terminate(&mut self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_backend(
|
|
||||||
backend: Backend,
|
|
||||||
) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
|
|
||||||
match backend {
|
|
||||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
|
||||||
Backend::Wlroots => Ok(Box::new(wlroots::WlrootsEmulation::new()?)),
|
|
||||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
|
||||||
Backend::Libei => Ok(Box::new(libei::LibeiEmulation::new().await?)),
|
|
||||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
|
||||||
Backend::X11 => Ok(Box::new(x11::X11Emulation::new()?)),
|
|
||||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
|
||||||
Backend::Xdp => Ok(Box::new(
|
|
||||||
xdg_desktop_portal::DesktopPortalEmulation::new().await?,
|
|
||||||
)),
|
|
||||||
#[cfg(windows)]
|
|
||||||
Backend::Windows => Ok(Box::new(windows::WindowsEmulation::new()?)),
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
Backend::MacOs => Ok(Box::new(macos::MacOSEmulation::new()?)),
|
|
||||||
Backend::Dummy => Ok(Box::new(dummy::DummyEmulation::new())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create(
|
|
||||||
backend: Option<Backend>,
|
|
||||||
) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
|
|
||||||
if let Some(backend) = backend {
|
|
||||||
let b = create_backend(backend).await;
|
|
||||||
if b.is_ok() {
|
|
||||||
log::info!("using emulation backend: {backend}");
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
for backend in [
|
|
||||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
|
||||||
Backend::Wlroots,
|
|
||||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
|
||||||
Backend::Libei,
|
|
||||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
|
||||||
Backend::Xdp,
|
|
||||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
|
||||||
Backend::X11,
|
|
||||||
#[cfg(windows)]
|
|
||||||
Backend::Windows,
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
Backend::MacOs,
|
|
||||||
Backend::Dummy,
|
|
||||||
] {
|
|
||||||
match create_backend(backend).await {
|
|
||||||
Ok(b) => {
|
|
||||||
log::info!("using emulation backend: {backend}");
|
|
||||||
return Ok(b);
|
|
||||||
}
|
|
||||||
Err(e) if e.cancelled_by_user() => return Err(e),
|
|
||||||
Err(e) => log::warn!("{e}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(EmulationCreationError::NoAvailableBackend)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ use input_event::{Event, KeyboardEvent, PointerEvent};
|
|||||||
|
|
||||||
use crate::error::{EmulationError, ReisConvertStreamError};
|
use crate::error::{EmulationError, ReisConvertStreamError};
|
||||||
|
|
||||||
use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation};
|
use super::{error::LibeiEmulationCreationError, Emulation, EmulationHandle};
|
||||||
|
|
||||||
static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
|
static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
@@ -60,7 +60,7 @@ struct Devices {
|
|||||||
keyboard: Arc<RwLock<Option<(ei::Device, ei::Keyboard)>>>,
|
keyboard: Arc<RwLock<Option<(ei::Device, ei::Keyboard)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LibeiEmulation<'a> {
|
pub(crate) struct LibeiEmulation<'a> {
|
||||||
context: ei::Context,
|
context: ei::Context,
|
||||||
devices: Devices,
|
devices: Devices,
|
||||||
ei_task: JoinHandle<()>,
|
ei_task: JoinHandle<()>,
|
||||||
@@ -99,7 +99,7 @@ async fn get_ei_fd<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> LibeiEmulation<'a> {
|
impl<'a> LibeiEmulation<'a> {
|
||||||
pub async fn new() -> Result<Self, LibeiEmulationCreationError> {
|
pub(crate) async fn new() -> Result<Self, LibeiEmulationCreationError> {
|
||||||
let (_remote_desktop, session, eifd) = get_ei_fd().await?;
|
let (_remote_desktop, session, eifd) = get_ei_fd().await?;
|
||||||
let stream = UnixStream::from(eifd);
|
let stream = UnixStream::from(eifd);
|
||||||
stream.set_nonblocking(true)?;
|
stream.set_nonblocking(true)?;
|
||||||
@@ -147,7 +147,7 @@ impl<'a> Drop for LibeiEmulation<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'a> InputEmulation for LibeiEmulation<'a> {
|
impl<'a> Emulation for LibeiEmulation<'a> {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use super::{error::EmulationError, EmulationHandle, InputEmulation};
|
use super::{error::EmulationError, Emulation, EmulationHandle};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
|
use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
|
||||||
use core_graphics::event::{
|
use core_graphics::event::{
|
||||||
@@ -16,8 +16,8 @@ use super::error::MacOSEmulationCreationError;
|
|||||||
const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500);
|
const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500);
|
||||||
const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
||||||
|
|
||||||
pub struct MacOSEmulation {
|
pub(crate) struct MacOSEmulation {
|
||||||
pub event_source: CGEventSource,
|
event_source: CGEventSource,
|
||||||
repeat_task: Option<AbortHandle>,
|
repeat_task: Option<AbortHandle>,
|
||||||
button_state: ButtonState,
|
button_state: ButtonState,
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ impl IndexMut<CGMouseButton> for ButtonState {
|
|||||||
unsafe impl Send for MacOSEmulation {}
|
unsafe impl Send for MacOSEmulation {}
|
||||||
|
|
||||||
impl MacOSEmulation {
|
impl MacOSEmulation {
|
||||||
pub fn new() -> Result<Self, MacOSEmulationCreationError> {
|
pub(crate) fn new() -> Result<Self, MacOSEmulationCreationError> {
|
||||||
let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
|
let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
|
||||||
.map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?;
|
.map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?;
|
||||||
let button_state = ButtonState {
|
let button_state = ButtonState {
|
||||||
@@ -106,7 +106,7 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputEmulation for MacOSEmulation {
|
impl Emulation for MacOSEmulation {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
|
|||||||
@@ -19,23 +19,23 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{
|
|||||||
};
|
};
|
||||||
use windows::Win32::UI::WindowsAndMessaging::{XBUTTON1, XBUTTON2};
|
use windows::Win32::UI::WindowsAndMessaging::{XBUTTON1, XBUTTON2};
|
||||||
|
|
||||||
use super::{EmulationHandle, InputEmulation};
|
use super::{Emulation, EmulationHandle};
|
||||||
|
|
||||||
const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500);
|
const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500);
|
||||||
const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
||||||
|
|
||||||
pub struct WindowsEmulation {
|
pub(crate) struct WindowsEmulation {
|
||||||
repeat_task: Option<AbortHandle>,
|
repeat_task: Option<AbortHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowsEmulation {
|
impl WindowsEmulation {
|
||||||
pub fn new() -> Result<Self, WindowsEmulationCreationError> {
|
pub(crate) fn new() -> Result<Self, WindowsEmulationCreationError> {
|
||||||
Ok(Self { repeat_task: None })
|
Ok(Self { repeat_task: None })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputEmulation for WindowsEmulation {
|
impl Emulation for WindowsEmulation {
|
||||||
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
|
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
|
||||||
match event {
|
match event {
|
||||||
Event::Pointer(pointer_event) => match pointer_event {
|
Event::Pointer(pointer_event) => match pointer_event {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::error::EmulationError;
|
use crate::error::EmulationError;
|
||||||
|
|
||||||
use super::{error::WlrootsEmulationCreationError, InputEmulation};
|
use super::{error::WlrootsEmulationCreationError, Emulation};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io;
|
use std::io;
|
||||||
@@ -50,7 +50,7 @@ pub(crate) struct WlrootsEmulation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WlrootsEmulation {
|
impl WlrootsEmulation {
|
||||||
pub fn new() -> Result<Self, WlrootsEmulationCreationError> {
|
pub(crate) fn new() -> Result<Self, WlrootsEmulationCreationError> {
|
||||||
let conn = Connection::connect_to_env()?;
|
let conn = Connection::connect_to_env()?;
|
||||||
let (globals, queue) = registry_queue_init::<State>(&conn)?;
|
let (globals, queue) = registry_queue_init::<State>(&conn)?;
|
||||||
let qh = queue.handle();
|
let qh = queue.handle();
|
||||||
@@ -116,7 +116,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputEmulation for WlrootsEmulation {
|
impl Emulation for WlrootsEmulation {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ use input_event::{
|
|||||||
|
|
||||||
use crate::error::EmulationError;
|
use crate::error::EmulationError;
|
||||||
|
|
||||||
use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation};
|
use super::{error::X11EmulationCreationError, Emulation, EmulationHandle};
|
||||||
|
|
||||||
pub struct X11Emulation {
|
pub(crate) struct X11Emulation {
|
||||||
display: *mut xlib::Display,
|
display: *mut xlib::Display,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for X11Emulation {}
|
unsafe impl Send for X11Emulation {}
|
||||||
|
|
||||||
impl X11Emulation {
|
impl X11Emulation {
|
||||||
pub fn new() -> Result<Self, X11EmulationCreationError> {
|
pub(crate) fn new() -> Result<Self, X11EmulationCreationError> {
|
||||||
let display = unsafe {
|
let display = unsafe {
|
||||||
match xlib::XOpenDisplay(ptr::null()) {
|
match xlib::XOpenDisplay(ptr::null()) {
|
||||||
d if d == ptr::null::<xlib::Display>() as *mut xlib::Display => {
|
d if d == ptr::null::<xlib::Display>() as *mut xlib::Display => {
|
||||||
@@ -99,7 +99,7 @@ impl Drop for X11Emulation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl InputEmulation for X11Emulation {
|
impl Emulation for X11Emulation {
|
||||||
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
|
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
|
||||||
match event {
|
match event {
|
||||||
Event::Pointer(pointer_event) => match pointer_event {
|
Event::Pointer(pointer_event) => match pointer_event {
|
||||||
|
|||||||
@@ -16,15 +16,15 @@ use input_event::{
|
|||||||
|
|
||||||
use crate::error::EmulationError;
|
use crate::error::EmulationError;
|
||||||
|
|
||||||
use super::{error::XdpEmulationCreationError, EmulationHandle, InputEmulation};
|
use super::{error::XdpEmulationCreationError, Emulation, EmulationHandle};
|
||||||
|
|
||||||
pub struct DesktopPortalEmulation<'a> {
|
pub(crate) struct DesktopPortalEmulation<'a> {
|
||||||
proxy: RemoteDesktop<'a>,
|
proxy: RemoteDesktop<'a>,
|
||||||
session: Session<'a, RemoteDesktop<'a>>,
|
session: Session<'a, RemoteDesktop<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> DesktopPortalEmulation<'a> {
|
impl<'a> DesktopPortalEmulation<'a> {
|
||||||
pub async fn new() -> Result<DesktopPortalEmulation<'a>, XdpEmulationCreationError> {
|
pub(crate) async fn new() -> Result<DesktopPortalEmulation<'a>, XdpEmulationCreationError> {
|
||||||
log::debug!("connecting to org.freedesktop.portal.RemoteDesktop portal ...");
|
log::debug!("connecting to org.freedesktop.portal.RemoteDesktop portal ...");
|
||||||
let proxy = RemoteDesktop::new().await?;
|
let proxy = RemoteDesktop::new().await?;
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ impl<'a> DesktopPortalEmulation<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
impl<'a> Emulation for DesktopPortalEmulation<'a> {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: input_event::Event,
|
event: input_event::Event,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ async fn input_capture_test(config: Config) -> Result<(), InputCaptureError> {
|
|||||||
log::info!("creating input capture");
|
log::info!("creating input capture");
|
||||||
let backend = config.capture_backend.map(|b| b.into());
|
let backend = config.capture_backend.map(|b| b.into());
|
||||||
loop {
|
loop {
|
||||||
let mut input_capture = input_capture::create(backend).await?;
|
let mut input_capture = InputCapture::new(backend).await?;
|
||||||
log::info!("creating clients");
|
log::info!("creating clients");
|
||||||
input_capture.create(0, Position::Left).await?;
|
input_capture.create(0, Position::Left).await?;
|
||||||
input_capture.create(1, Position::Right).await?;
|
input_capture.create(1, Position::Right).await?;
|
||||||
@@ -33,7 +33,7 @@ async fn input_capture_test(config: Config) -> Result<(), InputCaptureError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn do_capture(input_capture: &mut Box<dyn InputCapture>) -> Result<(), CaptureError> {
|
async fn do_capture(input_capture: &mut InputCapture) -> Result<(), CaptureError> {
|
||||||
loop {
|
loop {
|
||||||
let (client, event) = input_capture
|
let (client, event) = input_capture
|
||||||
.next()
|
.next()
|
||||||
|
|||||||
@@ -125,8 +125,8 @@ pub struct ClientState {
|
|||||||
/// e.g. Laptops usually have at least an ethernet and a wifi port
|
/// e.g. Laptops usually have at least an ethernet and a wifi port
|
||||||
/// which have different ip addresses
|
/// which have different ip addresses
|
||||||
pub ips: HashSet<IpAddr>,
|
pub ips: HashSet<IpAddr>,
|
||||||
/// keys currently pressed by this client
|
/// client has pressed keys
|
||||||
pub pressed_keys: HashSet<u32>,
|
pub has_pressed_keys: bool,
|
||||||
/// dns resolving in progress
|
/// dns resolving in progress
|
||||||
pub resolving: bool,
|
pub resolving: bool,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use input_emulation::InputEmulation;
|
||||||
use input_event::{Event, PointerEvent};
|
use input_event::{Event, PointerEvent};
|
||||||
use std::f64::consts::PI;
|
use std::f64::consts::PI;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -22,7 +23,7 @@ const RADIUS: f64 = 100.0;
|
|||||||
|
|
||||||
async fn input_emulation_test(config: Config) -> Result<()> {
|
async fn input_emulation_test(config: Config) -> Result<()> {
|
||||||
let backend = config.emulation_backend.map(|b| b.into());
|
let backend = config.emulation_backend.map(|b| b.into());
|
||||||
let mut emulation = input_emulation::create(backend).await?;
|
let mut emulation = InputEmulation::new(backend).await?;
|
||||||
emulation.create(0).await;
|
emulation.create(0).await;
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let mut offset = (0, 0);
|
let mut offset = (0, 0);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use capture_task::CaptureEvent;
|
use capture_task::CaptureRequest;
|
||||||
use emulation_task::EmulationEvent;
|
use emulation_task::EmulationRequest;
|
||||||
use log;
|
use log;
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, RefCell},
|
cell::{Cell, RefCell},
|
||||||
@@ -253,7 +253,7 @@ impl Server {
|
|||||||
self.notifies.capture.notify_waiters()
|
self.notifies.capture.notify_waiters()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn capture_notified(&self) {
|
async fn capture_enabled(&self) {
|
||||||
self.notifies.capture.notified().await
|
self.notifies.capture.notified().await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,8 +306,8 @@ impl Server {
|
|||||||
|
|
||||||
async fn handle_request(
|
async fn handle_request(
|
||||||
&self,
|
&self,
|
||||||
capture: &Sender<CaptureEvent>,
|
capture: &Sender<CaptureRequest>,
|
||||||
emulate: &Sender<EmulationEvent>,
|
emulate: &Sender<EmulationRequest>,
|
||||||
event: FrontendRequest,
|
event: FrontendRequest,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
log::debug!("frontend: {event:?}");
|
log::debug!("frontend: {event:?}");
|
||||||
@@ -372,8 +372,8 @@ impl Server {
|
|||||||
|
|
||||||
async fn deactivate_client(
|
async fn deactivate_client(
|
||||||
&self,
|
&self,
|
||||||
capture: &Sender<CaptureEvent>,
|
capture: &Sender<CaptureRequest>,
|
||||||
emulate: &Sender<EmulationEvent>,
|
emulate: &Sender<EmulationRequest>,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
) {
|
) {
|
||||||
log::debug!("deactivating client {handle}");
|
log::debug!("deactivating client {handle}");
|
||||||
@@ -382,15 +382,15 @@ impl Server {
|
|||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
let _ = capture.send(CaptureRequest::Destroy(handle)).await;
|
||||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
let _ = emulate.send(EmulationRequest::Destroy(handle)).await;
|
||||||
log::debug!("deactivating client {handle} done");
|
log::debug!("deactivating client {handle} done");
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn activate_client(
|
async fn activate_client(
|
||||||
&self,
|
&self,
|
||||||
capture: &Sender<CaptureEvent>,
|
capture: &Sender<CaptureRequest>,
|
||||||
emulate: &Sender<EmulationEvent>,
|
emulate: &Sender<EmulationRequest>,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
) {
|
) {
|
||||||
log::debug!("activating client");
|
log::debug!("activating client");
|
||||||
@@ -415,15 +415,17 @@ impl Server {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* notify emulation, capture and frontends */
|
/* notify emulation, capture and frontends */
|
||||||
let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
|
let _ = capture
|
||||||
let _ = emulate.send(EmulationEvent::Create(handle)).await;
|
.send(CaptureRequest::Create(handle, pos.into()))
|
||||||
|
.await;
|
||||||
|
let _ = emulate.send(EmulationRequest::Create(handle)).await;
|
||||||
log::debug!("activating client {handle} done");
|
log::debug!("activating client {handle} done");
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn remove_client(
|
async fn remove_client(
|
||||||
&self,
|
&self,
|
||||||
capture: &Sender<CaptureEvent>,
|
capture: &Sender<CaptureRequest>,
|
||||||
emulate: &Sender<EmulationEvent>,
|
emulate: &Sender<EmulationRequest>,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
) {
|
) {
|
||||||
let Some(active) = self
|
let Some(active) = self
|
||||||
@@ -436,8 +438,14 @@ impl Server {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if active {
|
if active {
|
||||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
let _ = capture.send(CaptureRequest::Destroy(handle)).await;
|
||||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
let _ = emulate.send(EmulationRequest::Destroy(handle)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_pressed_keys(&self, handle: ClientHandle, has_pressed_keys: bool) {
|
||||||
|
if let Some((_, s)) = self.client_manager.borrow_mut().get_mut(handle) {
|
||||||
|
s.has_pressed_keys = has_pressed_keys;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,8 +504,8 @@ impl Server {
|
|||||||
async fn update_pos(
|
async fn update_pos(
|
||||||
&self,
|
&self,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
capture: &Sender<CaptureEvent>,
|
capture: &Sender<CaptureRequest>,
|
||||||
emulate: &Sender<EmulationEvent>,
|
emulate: &Sender<EmulationRequest>,
|
||||||
pos: Position,
|
pos: Position,
|
||||||
) {
|
) {
|
||||||
let (changed, active) = {
|
let (changed, active) = {
|
||||||
@@ -514,11 +522,13 @@ impl Server {
|
|||||||
// update state in event input emulator & input capture
|
// update state in event input emulator & input capture
|
||||||
if changed {
|
if changed {
|
||||||
if active {
|
if active {
|
||||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
let _ = capture.send(CaptureRequest::Destroy(handle)).await;
|
||||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
let _ = emulate.send(EmulationRequest::Destroy(handle)).await;
|
||||||
}
|
}
|
||||||
let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
|
let _ = capture
|
||||||
let _ = emulate.send(EmulationEvent::Create(handle)).await;
|
.send(CaptureRequest::Create(handle, pos.into()))
|
||||||
|
.await;
|
||||||
|
let _ = emulate.send(EmulationRequest::Create(handle)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,6 +567,27 @@ impl Server {
|
|||||||
.get_mut(handle)
|
.get_mut(handle)
|
||||||
.and_then(|(c, _)| c.hostname.clone())
|
.and_then(|(c, _)| c.hostname.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_state(&self) -> State {
|
||||||
|
self.state.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_state(&self, state: State) {
|
||||||
|
log::debug!("state => {state:?}");
|
||||||
|
self.state.replace(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_active(&self, handle: Option<u64>) {
|
||||||
|
log::debug!("active client => {handle:?}");
|
||||||
|
self.active_client.replace(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn active_addr(&self, handle: u64) -> Option<SocketAddr> {
|
||||||
|
self.client_manager
|
||||||
|
.borrow()
|
||||||
|
.get(handle)
|
||||||
|
.and_then(|(_, s)| s.active_addr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn listen_frontend(
|
async fn listen_frontend(
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use std::{collections::HashSet, net::SocketAddr};
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
process::Command,
|
process::Command,
|
||||||
@@ -9,14 +9,14 @@ use tokio::{
|
|||||||
|
|
||||||
use input_capture::{self, CaptureError, CaptureHandle, InputCapture, InputCaptureError, Position};
|
use input_capture::{self, CaptureError, CaptureHandle, InputCapture, InputCaptureError, Position};
|
||||||
|
|
||||||
use input_event::{scancode, Event, KeyboardEvent};
|
use input_event::Event;
|
||||||
|
|
||||||
use crate::{client::ClientHandle, frontend::Status, server::State};
|
use crate::{client::ClientHandle, frontend::Status, server::State};
|
||||||
|
|
||||||
use super::Server;
|
use super::Server;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub(crate) enum CaptureEvent {
|
pub(crate) enum CaptureRequest {
|
||||||
/// capture must release the mouse
|
/// capture must release the mouse
|
||||||
Release,
|
Release,
|
||||||
/// add a capture client
|
/// add a capture client
|
||||||
@@ -27,7 +27,7 @@ pub(crate) enum CaptureEvent {
|
|||||||
|
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
server: Server,
|
server: Server,
|
||||||
capture_rx: Receiver<CaptureEvent>,
|
capture_rx: Receiver<CaptureRequest>,
|
||||||
udp_send: Sender<(Event, SocketAddr)>,
|
udp_send: Sender<(Event, SocketAddr)>,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
let backend = server.config.capture_backend.map(|b| b.into());
|
let backend = server.config.capture_backend.map(|b| b.into());
|
||||||
@@ -38,7 +38,7 @@ async fn capture_task(
|
|||||||
server: Server,
|
server: Server,
|
||||||
backend: Option<input_capture::Backend>,
|
backend: Option<input_capture::Backend>,
|
||||||
sender_tx: Sender<(Event, SocketAddr)>,
|
sender_tx: Sender<(Event, SocketAddr)>,
|
||||||
mut notify_rx: Receiver<CaptureEvent>,
|
mut notify_rx: Receiver<CaptureRequest>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = do_capture(backend, &server, &sender_tx, &mut notify_rx).await {
|
if let Err(e) = do_capture(backend, &server, &sender_tx, &mut notify_rx).await {
|
||||||
@@ -53,7 +53,7 @@ async fn capture_task(
|
|||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = notify_rx.recv() => continue, /* need to ignore requests here! */
|
_ = notify_rx.recv() => continue, /* need to ignore requests here! */
|
||||||
_ = server.capture_notified() => break,
|
_ = server.capture_enabled() => break,
|
||||||
_ = server.cancelled() => return,
|
_ = server.cancelled() => return,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,50 +64,48 @@ async fn do_capture(
|
|||||||
backend: Option<input_capture::Backend>,
|
backend: Option<input_capture::Backend>,
|
||||||
server: &Server,
|
server: &Server,
|
||||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||||
notify_rx: &mut Receiver<CaptureEvent>,
|
notify_rx: &mut Receiver<CaptureRequest>,
|
||||||
) -> Result<(), InputCaptureError> {
|
) -> Result<(), InputCaptureError> {
|
||||||
/* allow cancelling capture request */
|
/* allow cancelling capture request */
|
||||||
let mut capture = tokio::select! {
|
let mut capture = tokio::select! {
|
||||||
r = input_capture::create(backend) => {
|
r = InputCapture::new(backend) => r?,
|
||||||
r?
|
|
||||||
},
|
|
||||||
_ = server.cancelled() => return Ok(()),
|
_ = server.cancelled() => return Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
server.set_capture_status(Status::Enabled);
|
server.set_capture_status(Status::Enabled);
|
||||||
|
|
||||||
// FIXME DUPLICATES
|
let clients = server.active_clients();
|
||||||
let clients = server
|
let clients = clients.iter().copied().map(|handle| {
|
||||||
.client_manager
|
(
|
||||||
.borrow()
|
handle,
|
||||||
.get_client_states()
|
server
|
||||||
.map(|(h, s)| (h, s.clone()))
|
.client_manager
|
||||||
.collect::<Vec<_>>();
|
.borrow()
|
||||||
log::info!("{clients:?}");
|
.get(handle)
|
||||||
for (handle, (config, _state)) in clients {
|
.map(|(c, _)| c.pos)
|
||||||
capture.create(handle, config.pos.into()).await?;
|
.expect("no such client"),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
for (handle, pos) in clients {
|
||||||
|
capture.create(handle, pos.into()).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pressed_keys = HashSet::new();
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
event = capture.next() => {
|
event = capture.next() => match event {
|
||||||
match event {
|
Some(event) => handle_capture_event(server, &mut capture, sender_tx, event?).await?,
|
||||||
Some(Ok(event)) => handle_capture_event(server, &mut capture, sender_tx, event, &mut pressed_keys).await?,
|
None => return Ok(()),
|
||||||
Some(Err(e)) => return Err(e.into()),
|
},
|
||||||
None => return Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e = notify_rx.recv() => {
|
e = notify_rx.recv() => {
|
||||||
log::debug!("input capture notify rx: {e:?}");
|
log::debug!("input capture notify rx: {e:?}");
|
||||||
match e {
|
match e {
|
||||||
Some(e) => match e {
|
Some(e) => match e {
|
||||||
CaptureEvent::Release => {
|
CaptureRequest::Release => {
|
||||||
capture.release().await?;
|
capture.release().await?;
|
||||||
server.state.replace(State::Receiving);
|
server.state.replace(State::Receiving);
|
||||||
}
|
}
|
||||||
CaptureEvent::Create(h, p) => capture.create(h, p).await?,
|
CaptureRequest::Create(h, p) => capture.create(h, p).await?,
|
||||||
CaptureEvent::Destroy(h) => capture.destroy(h).await?,
|
CaptureRequest::Destroy(h) => capture.destroy(h).await?,
|
||||||
},
|
},
|
||||||
None => break,
|
None => break,
|
||||||
}
|
}
|
||||||
@@ -119,92 +117,44 @@ async fn do_capture(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_pressed_keys(pressed_keys: &mut HashSet<scancode::Linux>, key: u32, state: u8) {
|
|
||||||
if let Ok(scancode) = scancode::Linux::try_from(key) {
|
|
||||||
log::debug!("key: {key}, state: {state}, scancode: {scancode:?}");
|
|
||||||
match state {
|
|
||||||
1 => pressed_keys.insert(scancode),
|
|
||||||
_ => pressed_keys.remove(&scancode),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_capture_event(
|
async fn handle_capture_event(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
capture: &mut Box<dyn InputCapture>,
|
capture: &mut InputCapture,
|
||||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||||
event: (CaptureHandle, Event),
|
event: (CaptureHandle, Event),
|
||||||
pressed_keys: &mut HashSet<scancode::Linux>,
|
|
||||||
) -> Result<(), CaptureError> {
|
) -> Result<(), CaptureError> {
|
||||||
let (handle, mut e) = event;
|
let (handle, event) = event;
|
||||||
log::trace!("({handle}) {e:?}");
|
log::trace!("({handle}) {event:?}");
|
||||||
|
|
||||||
if let Event::Keyboard(KeyboardEvent::Key { key, state, .. }) = e {
|
// capture started
|
||||||
update_pressed_keys(pressed_keys, key, state);
|
if event == Event::Enter() {
|
||||||
log::debug!("{pressed_keys:?}");
|
server.set_state(State::AwaitingLeave);
|
||||||
if server.release_bind.iter().all(|k| pressed_keys.contains(k)) {
|
server.set_active(Some(handle));
|
||||||
pressed_keys.clear();
|
|
||||||
log::info!("releasing pointer");
|
|
||||||
capture.release().await?;
|
|
||||||
server.state.replace(State::Receiving);
|
|
||||||
log::trace!("STATE ===> Receiving");
|
|
||||||
// send an event to release all the modifiers
|
|
||||||
e = Event::Disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let info = {
|
|
||||||
let mut enter = false;
|
|
||||||
let mut start_timer = false;
|
|
||||||
|
|
||||||
// get client state for handle
|
|
||||||
let mut client_manager = server.client_manager.borrow_mut();
|
|
||||||
let client_state = client_manager.get_mut(handle).map(|(_, s)| s);
|
|
||||||
if let Some(client_state) = client_state {
|
|
||||||
// if we just entered the client we want to send additional enter events until
|
|
||||||
// we get a leave event
|
|
||||||
if let Event::Enter() = e {
|
|
||||||
server.state.replace(State::AwaitingLeave);
|
|
||||||
server.active_client.replace(Some(handle));
|
|
||||||
log::trace!("Active client => {}", handle);
|
|
||||||
start_timer = true;
|
|
||||||
log::trace!("STATE ===> AwaitingLeave");
|
|
||||||
enter = true;
|
|
||||||
} else {
|
|
||||||
// ignore any potential events in receiving mode
|
|
||||||
if server.state.get() == State::Receiving && e != Event::Disconnect() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some((client_state.active_addr, enter, start_timer))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (addr, enter, start_timer) = match info {
|
|
||||||
Some(i) => i,
|
|
||||||
None => {
|
|
||||||
// should not happen
|
|
||||||
log::warn!("unknown client!");
|
|
||||||
capture.release().await?;
|
|
||||||
server.state.replace(State::Receiving);
|
|
||||||
log::trace!("STATE ===> Receiving");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if start_timer {
|
|
||||||
server.restart_ping_timer();
|
server.restart_ping_timer();
|
||||||
}
|
|
||||||
if enter {
|
|
||||||
spawn_hook_command(server, handle);
|
spawn_hook_command(server, handle);
|
||||||
}
|
}
|
||||||
if let Some(addr) = addr {
|
|
||||||
if enter {
|
// release capture if emulation set state to Receiveing
|
||||||
let _ = sender_tx.send((Event::Enter(), addr)).await;
|
if server.get_state() == State::Receiving {
|
||||||
}
|
capture.release().await?;
|
||||||
let _ = sender_tx.send((e, addr)).await;
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// check release bind
|
||||||
|
if capture.keys_pressed(&server.release_bind) {
|
||||||
|
capture.release().await?;
|
||||||
|
server.set_state(State::Receiving);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(addr) = server.active_addr(handle) {
|
||||||
|
let event = match server.get_state() {
|
||||||
|
State::Sending => event,
|
||||||
|
/* send additional enter events until acknowleged */
|
||||||
|
State::AwaitingLeave => Event::Enter(),
|
||||||
|
/* released capture */
|
||||||
|
State::Receiving => Event::Disconnect(),
|
||||||
|
};
|
||||||
|
sender_tx.send((event, addr)).await.expect("sender closed");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ use crate::{
|
|||||||
server::State,
|
server::State,
|
||||||
};
|
};
|
||||||
use input_emulation::{self, EmulationError, EmulationHandle, InputEmulation, InputEmulationError};
|
use input_emulation::{self, EmulationError, EmulationHandle, InputEmulation, InputEmulationError};
|
||||||
use input_event::{Event, KeyboardEvent};
|
use input_event::Event;
|
||||||
|
|
||||||
use super::{network_task::NetworkError, CaptureEvent, Server};
|
use super::{network_task::NetworkError, CaptureRequest, Server};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) enum EmulationEvent {
|
pub(crate) enum EmulationRequest {
|
||||||
/// create a new client
|
/// create a new client
|
||||||
Create(EmulationHandle),
|
Create(EmulationHandle),
|
||||||
/// destroy a client
|
/// destroy a client
|
||||||
@@ -27,10 +27,10 @@ pub(crate) enum EmulationEvent {
|
|||||||
|
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
server: Server,
|
server: Server,
|
||||||
emulation_rx: Receiver<EmulationEvent>,
|
emulation_rx: Receiver<EmulationRequest>,
|
||||||
udp_rx: Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
udp_rx: Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
||||||
sender_tx: Sender<(Event, SocketAddr)>,
|
sender_tx: Sender<(Event, SocketAddr)>,
|
||||||
capture_tx: Sender<CaptureEvent>,
|
capture_tx: Sender<CaptureRequest>,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
let emulation_task = emulation_task(server, emulation_rx, udp_rx, sender_tx, capture_tx);
|
let emulation_task = emulation_task(server, emulation_rx, udp_rx, sender_tx, capture_tx);
|
||||||
tokio::task::spawn_local(emulation_task)
|
tokio::task::spawn_local(emulation_task)
|
||||||
@@ -38,10 +38,10 @@ pub(crate) fn new(
|
|||||||
|
|
||||||
async fn emulation_task(
|
async fn emulation_task(
|
||||||
server: Server,
|
server: Server,
|
||||||
mut rx: Receiver<EmulationEvent>,
|
mut rx: Receiver<EmulationRequest>,
|
||||||
mut udp_rx: Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
mut udp_rx: Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
||||||
sender_tx: Sender<(Event, SocketAddr)>,
|
sender_tx: Sender<(Event, SocketAddr)>,
|
||||||
capture_tx: Sender<CaptureEvent>,
|
capture_tx: Sender<CaptureRequest>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = do_emulation(&server, &mut rx, &mut udp_rx, &sender_tx, &capture_tx).await {
|
if let Err(e) = do_emulation(&server, &mut rx, &mut udp_rx, &sender_tx, &capture_tx).await {
|
||||||
@@ -65,17 +65,15 @@ async fn emulation_task(
|
|||||||
|
|
||||||
async fn do_emulation(
|
async fn do_emulation(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
rx: &mut Receiver<EmulationEvent>,
|
rx: &mut Receiver<EmulationRequest>,
|
||||||
udp_rx: &mut Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
udp_rx: &mut Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
||||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||||
capture_tx: &Sender<CaptureEvent>,
|
capture_tx: &Sender<CaptureRequest>,
|
||||||
) -> Result<(), InputEmulationError> {
|
) -> Result<(), InputEmulationError> {
|
||||||
let backend = server.config.emulation_backend.map(|b| b.into());
|
let backend = server.config.emulation_backend.map(|b| b.into());
|
||||||
log::info!("creating input emulation...");
|
log::info!("creating input emulation...");
|
||||||
let mut emulation = tokio::select! {
|
let mut emulation = tokio::select! {
|
||||||
r = input_emulation::create(backend) => {
|
r = InputEmulation::new(backend) => r?,
|
||||||
r?
|
|
||||||
}
|
|
||||||
_ = server.cancelled() => return Ok(()),
|
_ = server.cancelled() => return Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -87,23 +85,17 @@ async fn do_emulation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let res = do_emulation_session(server, &mut emulation, rx, udp_rx, sender_tx, capture_tx).await;
|
let res = do_emulation_session(server, &mut emulation, rx, udp_rx, sender_tx, capture_tx).await;
|
||||||
|
emulation.terminate().await; // manual drop
|
||||||
// release potentially still pressed keys
|
res
|
||||||
release_all_keys(server, &mut emulation).await?;
|
|
||||||
|
|
||||||
emulation.terminate().await;
|
|
||||||
res?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn do_emulation_session(
|
async fn do_emulation_session(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
emulation: &mut Box<dyn InputEmulation>,
|
emulation: &mut InputEmulation,
|
||||||
rx: &mut Receiver<EmulationEvent>,
|
rx: &mut Receiver<EmulationRequest>,
|
||||||
udp_rx: &mut Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
udp_rx: &mut Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
||||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||||
capture_tx: &Sender<CaptureEvent>,
|
capture_tx: &Sender<CaptureRequest>,
|
||||||
) -> Result<(), InputEmulationError> {
|
) -> Result<(), InputEmulationError> {
|
||||||
let mut last_ignored = None;
|
let mut last_ignored = None;
|
||||||
|
|
||||||
@@ -121,9 +113,9 @@ async fn do_emulation_session(
|
|||||||
}
|
}
|
||||||
emulate_event = rx.recv() => {
|
emulate_event = rx.recv() => {
|
||||||
match emulate_event.expect("channel closed") {
|
match emulate_event.expect("channel closed") {
|
||||||
EmulationEvent::Create(h) => emulation.create(h).await,
|
EmulationRequest::Create(h) => { let _ = emulation.create(h).await; },
|
||||||
EmulationEvent::Destroy(h) => emulation.destroy(h).await,
|
EmulationRequest::Destroy(h) => emulation.destroy(h).await,
|
||||||
EmulationEvent::ReleaseKeys(c) => release_keys(server, emulation, c).await?,
|
EmulationRequest::ReleaseKeys(c) => emulation.release_keys(c).await?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = server.notifies.cancel.cancelled() => break Ok(()),
|
_ = server.notifies.cancel.cancelled() => break Ok(()),
|
||||||
@@ -133,8 +125,8 @@ async fn do_emulation_session(
|
|||||||
|
|
||||||
async fn handle_udp_rx(
|
async fn handle_udp_rx(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
capture_tx: &Sender<CaptureEvent>,
|
capture_tx: &Sender<CaptureRequest>,
|
||||||
emulate: &mut Box<dyn InputEmulation>,
|
emulate: &mut InputEmulation,
|
||||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||||
last_ignored: &mut Option<SocketAddr>,
|
last_ignored: &mut Option<SocketAddr>,
|
||||||
event: (Event, SocketAddr),
|
event: (Event, SocketAddr),
|
||||||
@@ -155,9 +147,7 @@ async fn handle_udp_rx(
|
|||||||
(Event::Ping(), addr) => {
|
(Event::Ping(), addr) => {
|
||||||
let _ = sender_tx.send((Event::Pong(), addr)).await;
|
let _ = sender_tx.send((Event::Pong(), addr)).await;
|
||||||
}
|
}
|
||||||
(Event::Disconnect(), _) => {
|
(Event::Disconnect(), _) => emulate.release_keys(handle).await?,
|
||||||
release_keys(server, emulate, handle).await?;
|
|
||||||
}
|
|
||||||
(event, addr) => {
|
(event, addr) => {
|
||||||
// tell clients that we are ready to receive events
|
// tell clients that we are ready to receive events
|
||||||
if let Event::Enter() = event {
|
if let Event::Enter() = event {
|
||||||
@@ -172,32 +162,17 @@ async fn handle_udp_rx(
|
|||||||
} else {
|
} else {
|
||||||
// upon receiving any event, we go back to receiving mode
|
// upon receiving any event, we go back to receiving mode
|
||||||
server.state.replace(State::Receiving);
|
server.state.replace(State::Receiving);
|
||||||
let _ = capture_tx.send(CaptureEvent::Release).await;
|
let _ = capture_tx.send(CaptureRequest::Release).await;
|
||||||
log::trace!("STATE ===> Receiving");
|
log::trace!("STATE ===> Receiving");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Receiving => {
|
State::Receiving => {
|
||||||
let ignore_event =
|
log::trace!("{event} => emulate");
|
||||||
if let Event::Keyboard(KeyboardEvent::Key { key, state, .. }) = event {
|
emulate.consume(event, handle).await?;
|
||||||
let (ignore_event, restart_timer) = update_client_keys(
|
let has_pressed_keys = emulate.has_pressed_keys(handle);
|
||||||
&mut server.client_manager.borrow_mut(),
|
server.update_pressed_keys(handle, has_pressed_keys);
|
||||||
handle,
|
if has_pressed_keys {
|
||||||
key,
|
server.restart_ping_timer();
|
||||||
state,
|
|
||||||
);
|
|
||||||
// restart timer if necessary
|
|
||||||
if restart_timer {
|
|
||||||
server.restart_ping_timer();
|
|
||||||
}
|
|
||||||
ignore_event
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
// workaround buggy rdp backend.
|
|
||||||
if !ignore_event {
|
|
||||||
// consume event
|
|
||||||
emulate.consume(event, handle).await?;
|
|
||||||
log::trace!("{event} => emulate");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::AwaitingLeave => {
|
State::AwaitingLeave => {
|
||||||
@@ -214,7 +189,7 @@ async fn handle_udp_rx(
|
|||||||
// event should still be possible
|
// event should still be possible
|
||||||
if let Event::Enter() = event {
|
if let Event::Enter() = event {
|
||||||
server.state.replace(State::Receiving);
|
server.state.replace(State::Receiving);
|
||||||
let _ = capture_tx.send(CaptureEvent::Release).await;
|
let _ = capture_tx.send(CaptureRequest::Release).await;
|
||||||
log::trace!("STATE ===> Receiving");
|
log::trace!("STATE ===> Receiving");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,57 +199,6 @@ async fn handle_udp_rx(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn release_all_keys(
|
|
||||||
server: &Server,
|
|
||||||
emulation: &mut Box<dyn InputEmulation>,
|
|
||||||
) -> Result<(), EmulationError> {
|
|
||||||
let clients = server
|
|
||||||
.client_manager
|
|
||||||
.borrow()
|
|
||||||
.get_client_states()
|
|
||||||
.map(|(h, _)| h)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
for client in clients {
|
|
||||||
release_keys(server, emulation, client).await?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn release_keys(
|
|
||||||
server: &Server,
|
|
||||||
emulate: &mut Box<dyn InputEmulation>,
|
|
||||||
client: ClientHandle,
|
|
||||||
) -> Result<(), EmulationError> {
|
|
||||||
let keys = server
|
|
||||||
.client_manager
|
|
||||||
.borrow_mut()
|
|
||||||
.get_mut(client)
|
|
||||||
.iter_mut()
|
|
||||||
.flat_map(|(_, s)| s.pressed_keys.drain())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for key in keys {
|
|
||||||
let event = Event::Keyboard(KeyboardEvent::Key {
|
|
||||||
time: 0,
|
|
||||||
key,
|
|
||||||
state: 0,
|
|
||||||
});
|
|
||||||
emulate.consume(event, client).await?;
|
|
||||||
if let Ok(key) = input_event::scancode::Linux::try_from(key) {
|
|
||||||
log::warn!("releasing stuck key: {key:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let event = Event::Keyboard(KeyboardEvent::Modifiers {
|
|
||||||
mods_depressed: 0,
|
|
||||||
mods_latched: 0,
|
|
||||||
mods_locked: 0,
|
|
||||||
group: 0,
|
|
||||||
});
|
|
||||||
emulate.consume(event, client).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn activate_client_if_exists(
|
fn activate_client_if_exists(
|
||||||
client_manager: &mut ClientManager,
|
client_manager: &mut ClientManager,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
@@ -299,25 +223,3 @@ fn activate_client_if_exists(
|
|||||||
client_state.active_addr = Some(addr);
|
client_state.active_addr = Some(addr);
|
||||||
Some(handle)
|
Some(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_client_keys(
|
|
||||||
client_manager: &mut ClientManager,
|
|
||||||
handle: ClientHandle,
|
|
||||||
key: u32,
|
|
||||||
state: u8,
|
|
||||||
) -> (bool, bool) {
|
|
||||||
let Some(client_state) = client_manager.get_mut(handle).map(|(_, s)| s) else {
|
|
||||||
return (true, false);
|
|
||||||
};
|
|
||||||
|
|
||||||
// ignore double press / release events
|
|
||||||
let ignore_event = if state == 0 {
|
|
||||||
// ignore release event if key not pressed
|
|
||||||
!client_state.pressed_keys.remove(&key)
|
|
||||||
} else {
|
|
||||||
// ignore press event if key not released
|
|
||||||
!client_state.pressed_keys.insert(key)
|
|
||||||
};
|
|
||||||
let restart_timer = !client_state.pressed_keys.is_empty();
|
|
||||||
(ignore_event, restart_timer)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ use input_event::Event;
|
|||||||
|
|
||||||
use crate::client::ClientHandle;
|
use crate::client::ClientHandle;
|
||||||
|
|
||||||
use super::{capture_task::CaptureEvent, emulation_task::EmulationEvent, Server, State};
|
use super::{capture_task::CaptureRequest, emulation_task::EmulationRequest, Server, State};
|
||||||
|
|
||||||
const MAX_RESPONSE_TIME: Duration = Duration::from_millis(500);
|
const MAX_RESPONSE_TIME: Duration = Duration::from_millis(500);
|
||||||
|
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
server: Server,
|
server: Server,
|
||||||
sender_ch: Sender<(Event, SocketAddr)>,
|
sender_ch: Sender<(Event, SocketAddr)>,
|
||||||
emulate_notify: Sender<EmulationEvent>,
|
emulate_notify: Sender<EmulationRequest>,
|
||||||
capture_notify: Sender<CaptureEvent>,
|
capture_notify: Sender<CaptureRequest>,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
// timer task
|
// timer task
|
||||||
tokio::task::spawn_local(async move {
|
tokio::task::spawn_local(async move {
|
||||||
@@ -28,8 +28,8 @@ pub(crate) fn new(
|
|||||||
async fn ping_task(
|
async fn ping_task(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
sender_ch: Sender<(Event, SocketAddr)>,
|
sender_ch: Sender<(Event, SocketAddr)>,
|
||||||
emulate_notify: Sender<EmulationEvent>,
|
emulate_notify: Sender<EmulationRequest>,
|
||||||
capture_notify: Sender<CaptureEvent>,
|
capture_notify: Sender<CaptureRequest>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
// wait for wake up signal
|
// wait for wake up signal
|
||||||
@@ -42,8 +42,8 @@ async fn ping_task(
|
|||||||
let ping_clients: Vec<ClientHandle> = if receiving {
|
let ping_clients: Vec<ClientHandle> = if receiving {
|
||||||
// if receiving we care about clients with pressed keys
|
// if receiving we care about clients with pressed keys
|
||||||
client_manager
|
client_manager
|
||||||
.get_client_states_mut()
|
.get_client_states()
|
||||||
.filter(|(_, (_, s))| !s.pressed_keys.is_empty())
|
.filter(|(_, (_, s))| s.has_pressed_keys)
|
||||||
.map(|(h, _)| h)
|
.map(|(h, _)| h)
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
@@ -123,14 +123,14 @@ async fn ping_task(
|
|||||||
if receiving {
|
if receiving {
|
||||||
for h in unresponsive_clients {
|
for h in unresponsive_clients {
|
||||||
log::warn!("device not responding, releasing keys!");
|
log::warn!("device not responding, releasing keys!");
|
||||||
let _ = emulate_notify.send(EmulationEvent::ReleaseKeys(h)).await;
|
let _ = emulate_notify.send(EmulationRequest::ReleaseKeys(h)).await;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// release pointer if the active client has not responded
|
// release pointer if the active client has not responded
|
||||||
if !unresponsive_clients.is_empty() {
|
if !unresponsive_clients.is_empty() {
|
||||||
log::warn!("client not responding, releasing pointer!");
|
log::warn!("client not responding, releasing pointer!");
|
||||||
server.state.replace(State::Receiving);
|
server.state.replace(State::Receiving);
|
||||||
let _ = capture_notify.send(CaptureEvent::Release).await;
|
let _ = capture_notify.send(CaptureRequest::Release).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user