mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-08 04:20:01 +03:00
Compare commits
13 Commits
input-even
...
configurab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a038a24317 | ||
|
|
a4cdc138f0 | ||
|
|
db2004fd02 | ||
|
|
3c2f9da5b7 | ||
|
|
1599e6e76a | ||
|
|
aba9ddbdcb | ||
|
|
984efe7dd7 | ||
|
|
160b2532ff | ||
|
|
51d98cc2eb | ||
|
|
b1d9180cca | ||
|
|
08f24293f8 | ||
|
|
e2162b905a | ||
|
|
2ac193a5b2 |
820
Cargo.lock
generated
820
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
53
Cargo.toml
53
Cargo.toml
@@ -1,10 +1,3 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"input-capture",
|
||||
"input-emulation",
|
||||
"input-event",
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "lan-mouse"
|
||||
description = "Software KVM Switch / mouse & keyboard sharing software for Local Area Networks"
|
||||
@@ -13,16 +6,16 @@ edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
repository = "https://github.com/ferdinandschober/lan-mouse"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
lto = "fat"
|
||||
|
||||
[dependencies]
|
||||
input-event = { path = "input-event", version = "0.1.0" }
|
||||
input-emulation = { path = "input-emulation", version = "0.1.0", default-features = false }
|
||||
input-capture = { path = "input-capture", version = "0.1.0", default-features = false }
|
||||
|
||||
tempfile = "3.8"
|
||||
hickory-resolver = "0.24.1"
|
||||
memmap = "0.7"
|
||||
toml = "0.8"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
anyhow = "1.0.71"
|
||||
@@ -30,11 +23,16 @@ log = "0.4.20"
|
||||
env_logger = "0.11.3"
|
||||
serde_json = "1.0.107"
|
||||
tokio = {version = "1.32.0", features = ["io-util", "io-std", "macros", "net", "process", "rt", "sync", "signal"] }
|
||||
async-trait = "0.1.73"
|
||||
futures-core = "0.3.28"
|
||||
futures = "0.3.28"
|
||||
clap = { version="4.4.11", features = ["derive"] }
|
||||
gtk = { package = "gtk4", version = "0.8.1", features = ["v4_2"], optional = true }
|
||||
adw = { package = "libadwaita", version = "0.6.0", features = ["v1_1"], optional = true }
|
||||
async-channel = { version = "2.1.1", optional = true }
|
||||
keycode = "0.4.0"
|
||||
once_cell = "1.19.0"
|
||||
num_enum = "0.7.2"
|
||||
hostname = "0.4.0"
|
||||
slab = "0.4.9"
|
||||
endi = "1.1.0"
|
||||
@@ -43,13 +41,36 @@ thiserror = "1.0.61"
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2.148"
|
||||
|
||||
[target.'cfg(all(unix, not(target_os="macos")))'.dependencies]
|
||||
wayland-client = { version="0.31.1", optional = true }
|
||||
wayland-protocols = { version="0.31.0", features=["client", "staging", "unstable"], optional = true }
|
||||
wayland-protocols-wlr = { version="0.2.0", features=["client"], optional = true }
|
||||
wayland-protocols-misc = { version="0.2.0", features=["client"], optional = true }
|
||||
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
||||
ashpd = { version = "0.8", default-features = false, features = ["tokio"], optional = true }
|
||||
reis = { version = "0.2", features = [ "tokio" ], optional = true }
|
||||
|
||||
[target.'cfg(target_os="macos")'.dependencies]
|
||||
core-graphics = { version = "0.23", features = ["highsierra"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.54.0", features = [
|
||||
"Win32_System_LibraryLoader",
|
||||
"Win32_System_Threading",
|
||||
"Win32_Foundation",
|
||||
"Win32_Graphics",
|
||||
"Win32_Graphics_Gdi",
|
||||
"Win32_UI_Input_KeyboardAndMouse",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
] }
|
||||
|
||||
[build-dependencies]
|
||||
glib-build-tools = { version = "0.19.0", optional = true }
|
||||
|
||||
[features]
|
||||
default = [ "wayland", "x11", "xdg_desktop_portal", "libei", "gtk" ]
|
||||
wayland = [ "input-capture/wayland", "input-emulation/wayland" ]
|
||||
x11 = [ "input-capture/x11", "input-emulation/x11" ]
|
||||
xdg_desktop_portal = [ "input-emulation/xdg_desktop_portal" ]
|
||||
libei = [ "input-capture/libei", "input-emulation/libei" ]
|
||||
default = ["wayland", "x11", "xdg_desktop_portal", "libei", "gtk"]
|
||||
wayland = ["dep:wayland-client", "dep:wayland-protocols", "dep:wayland-protocols-wlr", "dep:wayland-protocols-misc" ]
|
||||
x11 = ["dep:x11"]
|
||||
xdg_desktop_portal = ["dep:ashpd"]
|
||||
libei = ["dep:reis", "dep:ashpd"]
|
||||
gtk = ["dep:gtk", "dep:adw", "dep:async-channel", "dep:glib-build-tools"]
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
[package]
|
||||
name = "input-capture"
|
||||
description = "cross-platform input-capture library used by lan-mouse"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
repository = "https://github.com/ferdinandschober/lan-mouse"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
futures = "0.3.28"
|
||||
futures-core = "0.3.30"
|
||||
log = "0.4.22"
|
||||
input-event = { path = "../input-event" }
|
||||
memmap = "0.7"
|
||||
tempfile = "3.8"
|
||||
thiserror = "1.0.61"
|
||||
tokio = { version = "1.32.0", features = ["io-util", "io-std", "macros", "net", "process", "rt", "sync", "signal"] }
|
||||
once_cell = "1.19.0"
|
||||
|
||||
|
||||
[target.'cfg(all(unix, not(target_os="macos")))'.dependencies]
|
||||
wayland-client = { version="0.31.1", optional = true }
|
||||
wayland-protocols = { version="0.31.0", features=["client", "staging", "unstable"], optional = true }
|
||||
wayland-protocols-wlr = { version="0.2.0", features=["client"], optional = true }
|
||||
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
||||
ashpd = { version = "0.8", default-features = false, features = ["tokio"], optional = true }
|
||||
reis = { version = "0.2", features = [ "tokio" ], optional = true }
|
||||
|
||||
[target.'cfg(target_os="macos")'.dependencies]
|
||||
core-graphics = { version = "0.23", features = ["highsierra"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.54.0", features = [
|
||||
"Win32_System_LibraryLoader",
|
||||
"Win32_System_Threading",
|
||||
"Win32_Foundation",
|
||||
"Win32_Graphics",
|
||||
"Win32_Graphics_Gdi",
|
||||
"Win32_UI_Input_KeyboardAndMouse",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
] }
|
||||
|
||||
[features]
|
||||
default = ["wayland", "x11", "libei"]
|
||||
wayland = ["dep:wayland-client", "dep:wayland-protocols", "dep:wayland-protocols-wlr" ]
|
||||
x11 = ["dep:x11"]
|
||||
libei = ["dep:reis", "dep:ashpd"]
|
||||
@@ -1,159 +0,0 @@
|
||||
use std::{fmt::Display, io};
|
||||
|
||||
use futures_core::Stream;
|
||||
|
||||
use input_event::Event;
|
||||
|
||||
use self::error::CaptureCreationError;
|
||||
|
||||
pub mod error;
|
||||
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
pub mod libei;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod macos;
|
||||
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
pub mod wayland;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod windows;
|
||||
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
pub mod x11;
|
||||
|
||||
/// fallback input capture (does not produce events)
|
||||
pub mod dummy;
|
||||
|
||||
pub type CaptureHandle = u64;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
|
||||
pub enum Position {
|
||||
Left,
|
||||
Right,
|
||||
Top,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn opposite(&self) -> Self {
|
||||
match self {
|
||||
Position::Left => Self::Right,
|
||||
Position::Right => Self::Left,
|
||||
Position::Top => Self::Bottom,
|
||||
Position::Bottom => Self::Top,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let pos = match self {
|
||||
Position::Left => "left",
|
||||
Position::Right => "right",
|
||||
Position::Top => "top",
|
||||
Position::Bottom => "bottom",
|
||||
};
|
||||
write!(f, "{}", pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Backend {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
InputCapturePortal,
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
LayerShell,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
X11,
|
||||
#[cfg(windows)]
|
||||
Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
MacOs,
|
||||
Dummy,
|
||||
}
|
||||
|
||||
impl Display for Backend {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Backend::InputCapturePortal => write!(f, "input-capture-portal"),
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Backend::LayerShell => write!(f, "layer-shell"),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
Backend::X11 => write!(f, "X11"),
|
||||
#[cfg(windows)]
|
||||
Backend::Windows => write!(f, "windows"),
|
||||
#[cfg(target_os = "macos")]
|
||||
Backend::MacOs => write!(f, "MacOS"),
|
||||
Backend::Dummy => write!(f, "dummy"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait InputCapture: Stream<Item = io::Result<(CaptureHandle, Event)>> + Unpin {
|
||||
/// create a new client with the given id
|
||||
fn create(&mut self, id: CaptureHandle, pos: Position) -> io::Result<()>;
|
||||
|
||||
/// destroy the client with the given id, if it exists
|
||||
fn destroy(&mut self, id: CaptureHandle) -> io::Result<()>;
|
||||
|
||||
/// release mouse
|
||||
fn release(&mut self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
pub async fn create_backend(
|
||||
backend: Backend,
|
||||
) -> Result<Box<dyn InputCapture<Item = io::Result<(CaptureHandle, Event)>>>, CaptureCreationError>
|
||||
{
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Backend::InputCapturePortal => Ok(Box::new(libei::LibeiInputCapture::new().await?)),
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Backend::LayerShell => Ok(Box::new(wayland::WaylandInputCapture::new()?)),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
Backend::X11 => Ok(Box::new(x11::X11InputCapture::new()?)),
|
||||
#[cfg(windows)]
|
||||
Backend::Windows => Ok(Box::new(windows::WindowsInputCapture::new())),
|
||||
#[cfg(target_os = "macos")]
|
||||
Backend::MacOs => Ok(Box::new(macos::MacOSInputCapture::new()?)),
|
||||
Backend::Dummy => Ok(Box::new(dummy::DummyInputCapture::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
backend: Option<Backend>,
|
||||
) -> Result<Box<dyn InputCapture<Item = io::Result<(CaptureHandle, Event)>>>, CaptureCreationError>
|
||||
{
|
||||
if let Some(backend) = backend {
|
||||
let b = create_backend(backend).await;
|
||||
if b.is_ok() {
|
||||
log::info!("using capture backend: {backend}");
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
for backend in [
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Backend::InputCapturePortal,
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Backend::LayerShell,
|
||||
#[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 capture backend: {backend}");
|
||||
return Ok(b);
|
||||
}
|
||||
Err(e) => log::warn!("{backend} input capture backend unavailable: {e}"),
|
||||
}
|
||||
}
|
||||
Err(CaptureCreationError::NoAvailableBackend)
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
[package]
|
||||
name = "input-emulation"
|
||||
description = "cross-platform input emulation library used by lan-mouse"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
repository = "https://github.com/ferdinandschober/lan-mouse"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
async-trait = "0.1.80"
|
||||
futures = "0.3.28"
|
||||
log = "0.4.22"
|
||||
input-event = { path = "../input-event" }
|
||||
thiserror = "1.0.61"
|
||||
tokio = { version = "1.32.0", features = ["io-util", "io-std", "macros", "net", "process", "rt", "sync", "signal"] }
|
||||
|
||||
[target.'cfg(all(unix, not(target_os="macos")))'.dependencies]
|
||||
wayland-client = { version="0.31.1", optional = true }
|
||||
wayland-protocols = { version="0.31.0", features=["client", "staging", "unstable"], optional = true }
|
||||
wayland-protocols-wlr = { version="0.2.0", features=["client"], optional = true }
|
||||
wayland-protocols-misc = { version="0.2.0", features=["client"], optional = true }
|
||||
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
||||
ashpd = { version = "0.8", default-features = false, features = ["tokio"], optional = true }
|
||||
reis = { version = "0.2", features = [ "tokio" ], optional = true }
|
||||
|
||||
[target.'cfg(target_os="macos")'.dependencies]
|
||||
core-graphics = { version = "0.23", features = ["highsierra"] }
|
||||
keycode = "0.4.0"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.54.0", features = [
|
||||
"Win32_System_LibraryLoader",
|
||||
"Win32_System_Threading",
|
||||
"Win32_Foundation",
|
||||
"Win32_Graphics",
|
||||
"Win32_Graphics_Gdi",
|
||||
"Win32_UI_Input_KeyboardAndMouse",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
] }
|
||||
|
||||
[features]
|
||||
default = ["wayland", "x11", "xdg_desktop_portal", "libei"]
|
||||
wayland = ["dep:wayland-client", "dep:wayland-protocols", "dep:wayland-protocols-wlr", "dep:wayland-protocols-misc" ]
|
||||
x11 = ["dep:x11"]
|
||||
xdg_desktop_portal = ["dep:ashpd"]
|
||||
libei = ["dep:reis", "dep:ashpd"]
|
||||
@@ -1,22 +0,0 @@
|
||||
use async_trait::async_trait;
|
||||
use input_event::Event;
|
||||
|
||||
use super::{EmulationHandle, InputEmulation};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DummyEmulation;
|
||||
|
||||
impl DummyEmulation {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for DummyEmulation {
|
||||
async fn consume(&mut self, event: Event, client_handle: EmulationHandle) {
|
||||
log::info!("received event: ({client_handle}) {event}");
|
||||
}
|
||||
async fn create(&mut self, _: EmulationHandle) {}
|
||||
async fn destroy(&mut self, _: EmulationHandle) {}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
use async_trait::async_trait;
|
||||
use std::{fmt::Display, future};
|
||||
|
||||
use input_event::Event;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use self::error::EmulationCreationError;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod windows;
|
||||
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
pub mod x11;
|
||||
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
pub mod wlroots;
|
||||
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
pub mod xdg_desktop_portal;
|
||||
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
pub mod libei;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod macos;
|
||||
|
||||
/// fallback input emulation (logs events)
|
||||
pub mod dummy;
|
||||
pub mod error;
|
||||
|
||||
pub type EmulationHandle = u64;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Backend {
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Wlroots,
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Libei,
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
Xdp,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
X11,
|
||||
#[cfg(windows)]
|
||||
Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
MacOs,
|
||||
Dummy,
|
||||
}
|
||||
|
||||
impl Display for Backend {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Backend::Wlroots => write!(f, "wlroots"),
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Backend::Libei => write!(f, "libei"),
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
Backend::Xdp => write!(f, "xdg-desktop-portal"),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
Backend::X11 => write!(f, "X11"),
|
||||
#[cfg(windows)]
|
||||
Backend::Windows => write!(f, "windows"),
|
||||
#[cfg(target_os = "macos")]
|
||||
Backend::MacOs => write!(f, "macos"),
|
||||
Backend::Dummy => write!(f, "dummy"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait InputEmulation: Send {
|
||||
async fn consume(&mut self, event: Event, handle: EmulationHandle);
|
||||
async fn create(&mut self, handle: EmulationHandle);
|
||||
async fn destroy(&mut self, handle: EmulationHandle);
|
||||
/// this function is waited on continuously and can be used to handle events
|
||||
async fn dispatch(&mut self) -> Result<()> {
|
||||
let _: () = future::pending().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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) => log::warn!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
Err(EmulationCreationError::NoAvailableBackend)
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
[package]
|
||||
name = "input-event"
|
||||
description = "cross-platform input-event types for input-capture / input-emulation"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
repository = "https://github.com/ferdinandschober/lan-mouse"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
futures-core = "0.3.30"
|
||||
log = "0.4.22"
|
||||
num_enum = "0.7.2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
92
src/capture.rs
Normal file
92
src/capture.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use std::io;
|
||||
|
||||
use futures_core::Stream;
|
||||
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
config::CaptureBackend,
|
||||
event::Event,
|
||||
};
|
||||
|
||||
use self::error::CaptureCreationError;
|
||||
|
||||
pub mod error;
|
||||
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
pub mod libei;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod macos;
|
||||
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
pub mod wayland;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod windows;
|
||||
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
pub mod x11;
|
||||
|
||||
/// fallback input capture (does not produce events)
|
||||
pub mod dummy;
|
||||
|
||||
pub async fn create_backend(
|
||||
backend: CaptureBackend,
|
||||
) -> Result<Box<dyn InputCapture<Item = io::Result<(ClientHandle, Event)>>>, CaptureCreationError> {
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
CaptureBackend::InputCapturePortal => Ok(Box::new(libei::LibeiInputCapture::new().await?)),
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
CaptureBackend::LayerShell => Ok(Box::new(wayland::WaylandInputCapture::new()?)),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
CaptureBackend::X11 => Ok(Box::new(x11::X11InputCapture::new()?)),
|
||||
#[cfg(windows)]
|
||||
CaptureBackend::Windows => Ok(Box::new(windows::WindowsInputCapture::new())),
|
||||
#[cfg(target_os = "macos")]
|
||||
CaptureBackend::MacOs => Ok(Box::new(macos::MacOSInputCapture::new()?)),
|
||||
CaptureBackend::Dummy => Ok(Box::new(dummy::DummyInputCapture::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
backend: Option<CaptureBackend>,
|
||||
) -> Result<Box<dyn InputCapture<Item = io::Result<(ClientHandle, Event)>>>, CaptureCreationError> {
|
||||
if let Some(backend) = backend {
|
||||
let b = create_backend(backend).await;
|
||||
if b.is_ok() {
|
||||
log::info!("using capture backend: {backend}");
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
for backend in [
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
CaptureBackend::InputCapturePortal,
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
CaptureBackend::LayerShell,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
CaptureBackend::X11,
|
||||
#[cfg(windows)]
|
||||
CaptureBackend::Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
CaptureBackend::MacOs,
|
||||
CaptureBackend::Dummy,
|
||||
] {
|
||||
match create_backend(backend).await {
|
||||
Ok(b) => {
|
||||
log::info!("using capture backend: {backend}");
|
||||
return Ok(b);
|
||||
}
|
||||
Err(e) => log::warn!("{backend} input capture backend unavailable: {e}"),
|
||||
}
|
||||
}
|
||||
Err(CaptureCreationError::NoAvailableBackend)
|
||||
}
|
||||
|
||||
pub trait InputCapture: Stream<Item = io::Result<(ClientHandle, Event)>> + Unpin {
|
||||
/// notify input capture of configuration changes
|
||||
fn notify(&mut self, event: ClientEvent) -> io::Result<()>;
|
||||
|
||||
/// release mouse
|
||||
fn release(&mut self) -> io::Result<()>;
|
||||
}
|
||||
@@ -4,9 +4,10 @@ use std::task::{Context, Poll};
|
||||
|
||||
use futures_core::Stream;
|
||||
|
||||
use input_event::Event;
|
||||
use crate::capture::InputCapture;
|
||||
use crate::event::Event;
|
||||
|
||||
use super::{CaptureHandle, InputCapture, Position};
|
||||
use crate::client::{ClientEvent, ClientHandle};
|
||||
|
||||
pub struct DummyInputCapture {}
|
||||
|
||||
@@ -23,11 +24,7 @@ impl Default for DummyInputCapture {
|
||||
}
|
||||
|
||||
impl InputCapture for DummyInputCapture {
|
||||
fn create(&mut self, _handle: CaptureHandle, _pos: Position) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&mut self, _handle: CaptureHandle) -> io::Result<()> {
|
||||
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -37,7 +34,7 @@ impl InputCapture for DummyInputCapture {
|
||||
}
|
||||
|
||||
impl Stream for DummyInputCapture {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
Poll::Pending
|
||||
@@ -30,24 +30,25 @@ use tokio::{
|
||||
use futures_core::Stream;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||
|
||||
use super::{
|
||||
error::LibeiCaptureCreationError, CaptureHandle, InputCapture as LanMouseInputCapture, Position,
|
||||
use crate::{
|
||||
capture::InputCapture as LanMouseInputCapture,
|
||||
client::{ClientEvent, ClientHandle, Position},
|
||||
event::{Event, KeyboardEvent, PointerEvent},
|
||||
};
|
||||
|
||||
use super::error::LibeiCaptureCreationError;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ProducerEvent {
|
||||
Release,
|
||||
Create(CaptureHandle, Position),
|
||||
Destroy(CaptureHandle),
|
||||
ClientEvent(ClientEvent),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct LibeiInputCapture<'a> {
|
||||
input_capture: Pin<Box<InputCapture<'a>>>,
|
||||
libei_task: JoinHandle<Result<()>>,
|
||||
event_rx: tokio::sync::mpsc::Receiver<(CaptureHandle, Event)>,
|
||||
event_rx: tokio::sync::mpsc::Receiver<(ClientHandle, Event)>,
|
||||
notify_tx: tokio::sync::mpsc::Sender<ProducerEvent>,
|
||||
}
|
||||
|
||||
@@ -80,9 +81,9 @@ fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) {
|
||||
|
||||
fn select_barriers(
|
||||
zones: &Zones,
|
||||
clients: &Vec<(CaptureHandle, Position)>,
|
||||
clients: &Vec<(ClientHandle, Position)>,
|
||||
next_barrier_id: &mut u32,
|
||||
) -> (Vec<Barrier>, HashMap<BarrierID, CaptureHandle>) {
|
||||
) -> (Vec<Barrier>, HashMap<BarrierID, ClientHandle>) {
|
||||
let mut client_for_barrier = HashMap::new();
|
||||
let mut barriers: Vec<Barrier> = vec![];
|
||||
|
||||
@@ -106,9 +107,9 @@ fn select_barriers(
|
||||
async fn update_barriers(
|
||||
input_capture: &InputCapture<'_>,
|
||||
session: &Session<'_>,
|
||||
active_clients: &Vec<(CaptureHandle, Position)>,
|
||||
active_clients: &Vec<(ClientHandle, Position)>,
|
||||
next_barrier_id: &mut u32,
|
||||
) -> Result<HashMap<BarrierID, CaptureHandle>> {
|
||||
) -> Result<HashMap<BarrierID, ClientHandle>> {
|
||||
let zones = input_capture.zones(session).await?.response()?;
|
||||
log::debug!("zones: {zones:?}");
|
||||
|
||||
@@ -184,8 +185,8 @@ async fn connect_to_eis(
|
||||
async fn libei_event_handler(
|
||||
mut ei_event_stream: EiConvertEventStream,
|
||||
context: ei::Context,
|
||||
event_tx: Sender<(CaptureHandle, Event)>,
|
||||
current_client: Rc<Cell<Option<CaptureHandle>>>,
|
||||
event_tx: Sender<(ClientHandle, Event)>,
|
||||
current_client: Rc<Cell<Option<ClientHandle>>>,
|
||||
) -> Result<()> {
|
||||
loop {
|
||||
let ei_event = match ei_event_stream.next().await {
|
||||
@@ -201,12 +202,12 @@ async fn libei_event_handler(
|
||||
|
||||
async fn wait_for_active_client(
|
||||
notify_rx: &mut Receiver<ProducerEvent>,
|
||||
active_clients: &mut Vec<(CaptureHandle, Position)>,
|
||||
active_clients: &mut Vec<(ClientHandle, Position)>,
|
||||
) -> Result<()> {
|
||||
// wait for a client update
|
||||
while let Some(producer_event) = notify_rx.recv().await {
|
||||
if let ProducerEvent::Create(c, p) = producer_event {
|
||||
handle_producer_event(ProducerEvent::Create(c, p), active_clients)?;
|
||||
if let ProducerEvent::ClientEvent(c) = producer_event {
|
||||
handle_producer_event(ProducerEvent::ClientEvent(c), active_clients)?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -225,7 +226,7 @@ impl<'a> LibeiInputCapture<'a> {
|
||||
/* safety: libei_task does not outlive Self */
|
||||
let input_capture = unsafe { &*input_capture_ptr };
|
||||
|
||||
let mut active_clients: Vec<(CaptureHandle, Position)> = vec![];
|
||||
let mut active_clients: Vec<(ClientHandle, Position)> = vec![];
|
||||
let mut next_barrier_id = 1u32;
|
||||
|
||||
/* there is a bug in xdg-remote-desktop-portal-gnome / mutter that
|
||||
@@ -348,8 +349,8 @@ async fn release_capture(
|
||||
input_capture: &InputCapture<'_>,
|
||||
session: &Session<'_>,
|
||||
activated: Activated,
|
||||
current_client: CaptureHandle,
|
||||
active_clients: &[(CaptureHandle, Position)],
|
||||
current_client: ClientHandle,
|
||||
active_clients: &[(ClientHandle, Position)],
|
||||
) -> Result<()> {
|
||||
log::debug!("releasing input capture {}", activated.activation_id());
|
||||
let (x, y) = activated.cursor_position();
|
||||
@@ -376,16 +377,16 @@ async fn release_capture(
|
||||
|
||||
fn handle_producer_event(
|
||||
producer_event: ProducerEvent,
|
||||
active_clients: &mut Vec<(CaptureHandle, Position)>,
|
||||
active_clients: &mut Vec<(ClientHandle, Position)>,
|
||||
) -> Result<bool> {
|
||||
log::debug!("handling event: {producer_event:?}");
|
||||
let updated = match producer_event {
|
||||
ProducerEvent::Release => false,
|
||||
ProducerEvent::Create(c, p) => {
|
||||
ProducerEvent::ClientEvent(ClientEvent::Create(c, p)) => {
|
||||
active_clients.push((c, p));
|
||||
true
|
||||
}
|
||||
ProducerEvent::Destroy(c) => {
|
||||
ProducerEvent::ClientEvent(ClientEvent::Destroy(c)) => {
|
||||
active_clients.retain(|(h, _)| *h != c);
|
||||
true
|
||||
}
|
||||
@@ -395,9 +396,9 @@ fn handle_producer_event(
|
||||
|
||||
async fn handle_ei_event(
|
||||
ei_event: EiEvent,
|
||||
current_client: Option<CaptureHandle>,
|
||||
current_client: Option<ClientHandle>,
|
||||
context: &ei::Context,
|
||||
event_tx: &Sender<(CaptureHandle, Event)>,
|
||||
event_tx: &Sender<(ClientHandle, Event)>,
|
||||
) {
|
||||
match ei_event {
|
||||
EiEvent::SeatAdded(s) => {
|
||||
@@ -546,18 +547,12 @@ async fn handle_ei_event(
|
||||
}
|
||||
|
||||
impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
|
||||
fn create(&mut self, handle: super::CaptureHandle, pos: super::Position) -> io::Result<()> {
|
||||
fn notify(&mut self, event: ClientEvent) -> io::Result<()> {
|
||||
let notify_tx = self.notify_tx.clone();
|
||||
tokio::task::spawn_local(async move {
|
||||
let _ = notify_tx.send(ProducerEvent::Create(handle, pos)).await;
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&mut self, handle: super::CaptureHandle) -> io::Result<()> {
|
||||
let notify_tx = self.notify_tx.clone();
|
||||
tokio::task::spawn_local(async move {
|
||||
let _ = notify_tx.send(ProducerEvent::Destroy(handle)).await;
|
||||
log::debug!("notifying {event:?}");
|
||||
let _ = notify_tx.send(ProducerEvent::ClientEvent(event)).await;
|
||||
log::debug!("done !");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -565,6 +560,7 @@ impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
|
||||
fn release(&mut self) -> io::Result<()> {
|
||||
let notify_tx = self.notify_tx.clone();
|
||||
tokio::task::spawn_local(async move {
|
||||
log::debug!("notifying Release");
|
||||
let _ = notify_tx.send(ProducerEvent::Release).await;
|
||||
});
|
||||
Ok(())
|
||||
@@ -572,7 +568,7 @@ impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Stream for LibeiInputCapture<'a> {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
match ready!(self.event_rx.poll_recv(cx)) {
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::{error::MacOSInputCaptureCreationError, CaptureHandle, InputCapture, Position};
|
||||
use crate::capture::error::MacOSInputCaptureCreationError;
|
||||
use crate::capture::InputCapture;
|
||||
use crate::client::{ClientEvent, ClientHandle};
|
||||
use crate::event::Event;
|
||||
use futures_core::Stream;
|
||||
use input_event::Event;
|
||||
use std::task::{Context, Poll};
|
||||
use std::{io, pin::Pin};
|
||||
|
||||
@@ -13,7 +15,7 @@ impl MacOSInputCapture {
|
||||
}
|
||||
|
||||
impl Stream for MacOSInputCapture {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
Poll::Pending
|
||||
@@ -21,11 +23,7 @@ impl Stream for MacOSInputCapture {
|
||||
}
|
||||
|
||||
impl InputCapture for MacOSInputCapture {
|
||||
fn create(&mut self, _id: CaptureHandle, _pos: Position) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&mut self, _id: CaptureHandle) -> io::Result<()> {
|
||||
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
use crate::{
|
||||
capture::{error::WaylandBindError, InputCapture},
|
||||
client::{ClientEvent, ClientHandle, Position},
|
||||
};
|
||||
|
||||
use futures_core::Stream;
|
||||
use memmap::MmapOptions;
|
||||
use std::{
|
||||
@@ -60,12 +65,9 @@ use wayland_client::{
|
||||
|
||||
use tempfile;
|
||||
|
||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||
use crate::event::{Event, KeyboardEvent, PointerEvent};
|
||||
|
||||
use super::{
|
||||
error::{LayerShellCaptureCreationError, WaylandBindError},
|
||||
CaptureHandle, InputCapture, Position,
|
||||
};
|
||||
use super::error::LayerShellCaptureCreationError;
|
||||
|
||||
struct Globals {
|
||||
compositor: wl_compositor::WlCompositor,
|
||||
@@ -102,13 +104,13 @@ struct State {
|
||||
pointer_lock: Option<ZwpLockedPointerV1>,
|
||||
rel_pointer: Option<ZwpRelativePointerV1>,
|
||||
shortcut_inhibitor: Option<ZwpKeyboardShortcutsInhibitorV1>,
|
||||
client_for_window: Vec<(Rc<Window>, CaptureHandle)>,
|
||||
focused: Option<(Rc<Window>, CaptureHandle)>,
|
||||
client_for_window: Vec<(Rc<Window>, ClientHandle)>,
|
||||
focused: Option<(Rc<Window>, ClientHandle)>,
|
||||
g: Globals,
|
||||
wayland_fd: OwnedFd,
|
||||
read_guard: Option<ReadEventsGuard>,
|
||||
qh: QueueHandle<Self>,
|
||||
pending_events: VecDeque<(CaptureHandle, Event)>,
|
||||
pending_events: VecDeque<(ClientHandle, Event)>,
|
||||
output_info: Vec<(WlOutput, OutputInfo)>,
|
||||
scroll_discrete_pending: bool,
|
||||
}
|
||||
@@ -367,11 +369,11 @@ impl WaylandInputCapture {
|
||||
Ok(WaylandInputCapture(inner))
|
||||
}
|
||||
|
||||
fn add_client(&mut self, handle: CaptureHandle, pos: Position) {
|
||||
fn add_client(&mut self, handle: ClientHandle, pos: Position) {
|
||||
self.0.get_mut().state.add_client(handle, pos);
|
||||
}
|
||||
|
||||
fn delete_client(&mut self, handle: CaptureHandle) {
|
||||
fn delete_client(&mut self, handle: ClientHandle) {
|
||||
let inner = self.0.get_mut();
|
||||
// remove all windows corresponding to this client
|
||||
while let Some(i) = inner
|
||||
@@ -469,7 +471,7 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
fn add_client(&mut self, client: CaptureHandle, pos: Position) {
|
||||
fn add_client(&mut self, client: ClientHandle, pos: Position) {
|
||||
let outputs = get_output_configuration(self, pos);
|
||||
|
||||
log::debug!("outputs: {outputs:?}");
|
||||
@@ -562,13 +564,15 @@ impl Inner {
|
||||
}
|
||||
|
||||
impl InputCapture for WaylandInputCapture {
|
||||
fn create(&mut self, handle: CaptureHandle, pos: Position) -> io::Result<()> {
|
||||
self.add_client(handle, pos);
|
||||
let inner = self.0.get_mut();
|
||||
inner.flush_events()
|
||||
}
|
||||
fn destroy(&mut self, handle: CaptureHandle) -> io::Result<()> {
|
||||
self.delete_client(handle);
|
||||
fn notify(&mut self, client_event: ClientEvent) -> io::Result<()> {
|
||||
match client_event {
|
||||
ClientEvent::Create(handle, pos) => {
|
||||
self.add_client(handle, pos);
|
||||
}
|
||||
ClientEvent::Destroy(handle) => {
|
||||
self.delete_client(handle);
|
||||
}
|
||||
}
|
||||
let inner = self.0.get_mut();
|
||||
inner.flush_events()
|
||||
}
|
||||
@@ -582,7 +586,7 @@ impl InputCapture for WaylandInputCapture {
|
||||
}
|
||||
|
||||
impl Stream for WaylandInputCapture {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
if let Some(event) = self.0.get_mut().state.pending_events.pop_front() {
|
||||
@@ -9,7 +9,7 @@ use std::ptr::{addr_of, addr_of_mut};
|
||||
use futures::executor::block_on;
|
||||
use std::default::Default;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::{mpsc, Mutex};
|
||||
use std::sync::mpsc;
|
||||
use std::task::ready;
|
||||
use std::{io, pin::Pin, thread};
|
||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||
@@ -32,25 +32,25 @@ use windows::Win32::UI::WindowsAndMessaging::{
|
||||
WNDPROC,
|
||||
};
|
||||
|
||||
use input_event::{
|
||||
scancode::{self, Linux},
|
||||
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
||||
use crate::client::Position;
|
||||
use crate::event::{
|
||||
KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
||||
};
|
||||
use crate::scancode::Linux;
|
||||
use crate::{
|
||||
capture::InputCapture,
|
||||
client::{ClientEvent, ClientHandle},
|
||||
event::Event,
|
||||
scancode,
|
||||
};
|
||||
|
||||
use super::{CaptureHandle, InputCapture, Position};
|
||||
|
||||
enum Request {
|
||||
Create(CaptureHandle, Position),
|
||||
Destroy(CaptureHandle),
|
||||
}
|
||||
|
||||
pub struct WindowsInputCapture {
|
||||
event_rx: Receiver<(CaptureHandle, Event)>,
|
||||
event_rx: Receiver<(ClientHandle, Event)>,
|
||||
msg_thread: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
enum EventType {
|
||||
Request = 0,
|
||||
ClientEvent = 0,
|
||||
Release = 1,
|
||||
Exit = 2,
|
||||
}
|
||||
@@ -59,28 +59,15 @@ unsafe fn signal_message_thread(event_type: EventType) {
|
||||
if let Some(event_tid) = get_event_tid() {
|
||||
PostThreadMessageW(event_tid, WM_USER, WPARAM(event_type as usize), LPARAM(0)).unwrap();
|
||||
} else {
|
||||
panic!();
|
||||
log::warn!("lost event");
|
||||
}
|
||||
}
|
||||
|
||||
impl InputCapture for WindowsInputCapture {
|
||||
fn create(&mut self, handle: CaptureHandle, pos: Position) -> io::Result<()> {
|
||||
fn notify(&mut self, event: ClientEvent) -> io::Result<()> {
|
||||
unsafe {
|
||||
{
|
||||
let mut requests = REQUEST_BUFFER.lock().unwrap();
|
||||
requests.push(Request::Create(handle, pos));
|
||||
}
|
||||
signal_message_thread(EventType::Request);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn destroy(&mut self, handle: CaptureHandle) -> io::Result<()> {
|
||||
unsafe {
|
||||
{
|
||||
let mut requests = REQUEST_BUFFER.lock().unwrap();
|
||||
requests.push(Request::Destroy(handle));
|
||||
}
|
||||
signal_message_thread(EventType::Request);
|
||||
EVENT_BUFFER.push(event);
|
||||
signal_message_thread(EventType::ClientEvent);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -91,10 +78,10 @@ impl InputCapture for WindowsInputCapture {
|
||||
}
|
||||
}
|
||||
|
||||
static mut REQUEST_BUFFER: Mutex<Vec<Request>> = Mutex::new(Vec::new());
|
||||
static mut ACTIVE_CLIENT: Option<CaptureHandle> = None;
|
||||
static mut CLIENT_FOR_POS: Lazy<HashMap<Position, CaptureHandle>> = Lazy::new(HashMap::new);
|
||||
static mut EVENT_TX: Option<Sender<(CaptureHandle, Event)>> = None;
|
||||
static mut EVENT_BUFFER: Vec<ClientEvent> = Vec::new();
|
||||
static mut ACTIVE_CLIENT: Option<ClientHandle> = None;
|
||||
static mut CLIENT_FOR_POS: Lazy<HashMap<Position, ClientHandle>> = Lazy::new(HashMap::new);
|
||||
static mut EVENT_TX: Option<Sender<(ClientHandle, Event)>> = None;
|
||||
static mut EVENT_THREAD_ID: AtomicU32 = AtomicU32::new(0);
|
||||
unsafe fn set_event_tid(tid: u32) {
|
||||
EVENT_THREAD_ID.store(tid, Ordering::SeqCst);
|
||||
@@ -369,6 +356,7 @@ fn enumerate_displays() -> Vec<RECT> {
|
||||
break;
|
||||
}
|
||||
if device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP != 0 {
|
||||
log::info!("{:?}", device.DeviceName);
|
||||
devices.push(device.DeviceName);
|
||||
}
|
||||
}
|
||||
@@ -547,18 +535,9 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
|
||||
x if x == EventType::Release as usize => {
|
||||
ACTIVE_CLIENT.take();
|
||||
}
|
||||
x if x == EventType::Request as usize => {
|
||||
let requests = {
|
||||
let mut res = vec![];
|
||||
let mut requests = REQUEST_BUFFER.lock().unwrap();
|
||||
for request in requests.drain(..) {
|
||||
res.push(request);
|
||||
}
|
||||
res
|
||||
};
|
||||
|
||||
for request in requests {
|
||||
update_clients(request)
|
||||
x if x == EventType::ClientEvent as usize => {
|
||||
while let Some(event) = EVENT_BUFFER.pop() {
|
||||
update_clients(event)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -572,12 +551,12 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn update_clients(request: Request) {
|
||||
match request {
|
||||
Request::Create(handle, pos) => {
|
||||
fn update_clients(client_event: ClientEvent) {
|
||||
match client_event {
|
||||
ClientEvent::Create(handle, pos) => {
|
||||
unsafe { CLIENT_FOR_POS.insert(pos, handle) };
|
||||
}
|
||||
Request::Destroy(handle) => unsafe {
|
||||
ClientEvent::Destroy(handle) => unsafe {
|
||||
for pos in [
|
||||
Position::Left,
|
||||
Position::Right,
|
||||
@@ -613,7 +592,7 @@ impl WindowsInputCapture {
|
||||
}
|
||||
|
||||
impl Stream for WindowsInputCapture {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
match ready!(self.event_rx.poll_recv(cx)) {
|
||||
None => Poll::Ready(None),
|
||||
@@ -3,11 +3,12 @@ use std::task::Poll;
|
||||
|
||||
use futures_core::Stream;
|
||||
|
||||
use super::InputCapture;
|
||||
use input_event::Event;
|
||||
use crate::capture::InputCapture;
|
||||
use crate::event::Event;
|
||||
|
||||
use crate::client::{ClientEvent, ClientHandle};
|
||||
|
||||
use super::error::X11InputCaptureCreationError;
|
||||
use super::{CaptureHandle, Position};
|
||||
|
||||
pub struct X11InputCapture {}
|
||||
|
||||
@@ -18,11 +19,7 @@ impl X11InputCapture {
|
||||
}
|
||||
|
||||
impl InputCapture for X11InputCapture {
|
||||
fn create(&mut self, _id: CaptureHandle, _pos: Position) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&mut self, _id: CaptureHandle) -> io::Result<()> {
|
||||
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -32,7 +29,7 @@ impl InputCapture for X11InputCapture {
|
||||
}
|
||||
|
||||
impl Stream for X11InputCapture {
|
||||
type Item = io::Result<(CaptureHandle, Event)>;
|
||||
type Item = io::Result<(ClientHandle, Event)>;
|
||||
|
||||
fn poll_next(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::capture;
|
||||
use crate::client::{ClientEvent, Position};
|
||||
use crate::config::Config;
|
||||
use crate::event::{Event, KeyboardEvent};
|
||||
use anyhow::{anyhow, Result};
|
||||
use futures::StreamExt;
|
||||
use input_capture::{self, Position};
|
||||
use input_event::{Event, KeyboardEvent};
|
||||
use tokio::task::LocalSet;
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
@@ -19,13 +20,12 @@ pub fn run() -> Result<()> {
|
||||
|
||||
async fn input_capture_test(config: Config) -> Result<()> {
|
||||
log::info!("creating input capture");
|
||||
let backend = config.capture_backend.map(|b| b.into());
|
||||
let mut input_capture = input_capture::create(backend).await?;
|
||||
let mut input_capture = capture::create(config.capture_backend).await?;
|
||||
log::info!("creating clients");
|
||||
input_capture.create(0, Position::Left)?;
|
||||
input_capture.create(1, Position::Right)?;
|
||||
input_capture.create(2, Position::Top)?;
|
||||
input_capture.create(3, Position::Bottom)?;
|
||||
input_capture.notify(ClientEvent::Create(0, Position::Left))?;
|
||||
input_capture.notify(ClientEvent::Create(1, Position::Right))?;
|
||||
input_capture.notify(ClientEvent::Create(2, Position::Top))?;
|
||||
input_capture.notify(ClientEvent::Create(3, Position::Bottom))?;
|
||||
loop {
|
||||
let (client, event) = input_capture
|
||||
.next()
|
||||
|
||||
@@ -10,7 +10,6 @@ use serde::{Deserialize, Serialize};
|
||||
use slab::Slab;
|
||||
|
||||
use crate::config::DEFAULT_PORT;
|
||||
use input_capture;
|
||||
|
||||
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum Position {
|
||||
@@ -26,17 +25,6 @@ impl Default for Position {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Position> for input_capture::Position {
|
||||
fn from(position: Position) -> input_capture::Position {
|
||||
match position {
|
||||
Position::Left => input_capture::Position::Left,
|
||||
Position::Right => input_capture::Position::Right,
|
||||
Position::Top => input_capture::Position::Top,
|
||||
Position::Bottom => input_capture::Position::Bottom,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PositionParseError {
|
||||
string: String,
|
||||
@@ -64,6 +52,17 @@ impl FromStr for Position {
|
||||
}
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn opposite(&self) -> Self {
|
||||
match self {
|
||||
Position::Left => Self::Right,
|
||||
Position::Right => Self::Left,
|
||||
Position::Top => Self::Bottom,
|
||||
Position::Bottom => Self::Top,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
@@ -119,6 +118,12 @@ impl Default for ClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ClientEvent {
|
||||
Create(ClientHandle, Position),
|
||||
Destroy(ClientHandle),
|
||||
}
|
||||
|
||||
pub type ClientHandle = u64;
|
||||
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
|
||||
@@ -9,11 +9,8 @@ use std::{error::Error, fs};
|
||||
use toml;
|
||||
|
||||
use crate::client::Position;
|
||||
|
||||
use input_event::scancode::{
|
||||
self,
|
||||
Linux::{KeyLeftAlt, KeyLeftCtrl, KeyLeftMeta, KeyLeftShift},
|
||||
};
|
||||
use crate::scancode;
|
||||
use crate::scancode::Linux::{KeyLeftAlt, KeyLeftCtrl, KeyLeftMeta, KeyLeftShift};
|
||||
|
||||
pub const DEFAULT_PORT: u16 = 4242;
|
||||
|
||||
@@ -118,24 +115,6 @@ impl Display for CaptureBackend {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CaptureBackend> for input_capture::Backend {
|
||||
fn from(backend: CaptureBackend) -> Self {
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
CaptureBackend::InputCapturePortal => Self::InputCapturePortal,
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
CaptureBackend::LayerShell => Self::LayerShell,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
CaptureBackend::X11 => Self::X11,
|
||||
#[cfg(windows)]
|
||||
CaptureBackend::Windows => Self::Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
CaptureBackend::MacOs => Self::MacOs,
|
||||
CaptureBackend::Dummy => Self::Dummy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)]
|
||||
pub enum EmulationBackend {
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
@@ -153,26 +132,6 @@ pub enum EmulationBackend {
|
||||
Dummy,
|
||||
}
|
||||
|
||||
impl From<EmulationBackend> for input_emulation::Backend {
|
||||
fn from(backend: EmulationBackend) -> Self {
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
EmulationBackend::Wlroots => Self::Wlroots,
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
EmulationBackend::Libei => Self::Libei,
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
EmulationBackend::Xdp => Self::Xdp,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
EmulationBackend::X11 => Self::X11,
|
||||
#[cfg(windows)]
|
||||
EmulationBackend::Windows => Self::Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
EmulationBackend::MacOs => Self::MacOs,
|
||||
EmulationBackend::Dummy => Self::Dummy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for EmulationBackend {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
|
||||
104
src/emulate.rs
Normal file
104
src/emulate.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use async_trait::async_trait;
|
||||
use std::future;
|
||||
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
config::EmulationBackend,
|
||||
event::Event,
|
||||
};
|
||||
use anyhow::Result;
|
||||
|
||||
use self::error::EmulationCreationError;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod windows;
|
||||
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
pub mod x11;
|
||||
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
pub mod wlroots;
|
||||
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
pub mod xdg_desktop_portal;
|
||||
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
pub mod libei;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod macos;
|
||||
|
||||
/// fallback input emulation (logs events)
|
||||
pub mod dummy;
|
||||
pub mod error;
|
||||
|
||||
#[async_trait]
|
||||
pub trait InputEmulation: Send {
|
||||
async fn consume(&mut self, event: Event, client_handle: ClientHandle);
|
||||
async fn notify(&mut self, client_event: ClientEvent);
|
||||
/// this function is waited on continuously and can be used to handle events
|
||||
async fn dispatch(&mut self) -> Result<()> {
|
||||
let _: () = future::pending().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn destroy(&mut self);
|
||||
}
|
||||
|
||||
pub async fn create_backend(
|
||||
backend: EmulationBackend,
|
||||
) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
EmulationBackend::Wlroots => Ok(Box::new(wlroots::WlrootsEmulation::new()?)),
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
EmulationBackend::Libei => Ok(Box::new(libei::LibeiEmulation::new().await?)),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
EmulationBackend::X11 => Ok(Box::new(x11::X11Emulation::new()?)),
|
||||
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
|
||||
EmulationBackend::Xdp => Ok(Box::new(
|
||||
xdg_desktop_portal::DesktopPortalEmulation::new().await?,
|
||||
)),
|
||||
#[cfg(windows)]
|
||||
EmulationBackend::Windows => Ok(Box::new(windows::WindowsEmulation::new()?)),
|
||||
#[cfg(target_os = "macos")]
|
||||
EmulationBackend::MacOs => Ok(Box::new(macos::MacOSEmulation::new()?)),
|
||||
EmulationBackend::Dummy => Ok(Box::new(dummy::DummyEmulation::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
backend: Option<EmulationBackend>,
|
||||
) -> 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")))]
|
||||
EmulationBackend::Wlroots,
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
EmulationBackend::Libei,
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
EmulationBackend::X11,
|
||||
#[cfg(windows)]
|
||||
EmulationBackend::Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
EmulationBackend::MacOs,
|
||||
EmulationBackend::Dummy,
|
||||
] {
|
||||
match create_backend(backend).await {
|
||||
Ok(b) => {
|
||||
log::info!("using emulation backend: {backend}");
|
||||
return Ok(b);
|
||||
}
|
||||
Err(e) => log::warn!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
Err(EmulationCreationError::NoAvailableBackend)
|
||||
}
|
||||
26
src/emulate/dummy.rs
Normal file
26
src/emulate/dummy.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
emulate::InputEmulation,
|
||||
event::Event,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DummyEmulation;
|
||||
|
||||
impl DummyEmulation {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for DummyEmulation {
|
||||
async fn consume(&mut self, event: Event, client_handle: ClientHandle) {
|
||||
log::info!("received event: ({client_handle}) {event}");
|
||||
}
|
||||
async fn notify(&mut self, client_event: ClientEvent) {
|
||||
log::info!("{client_event:?}");
|
||||
}
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
@@ -22,9 +22,13 @@ use reis::{
|
||||
PendingRequestResult,
|
||||
};
|
||||
|
||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
emulate::InputEmulation,
|
||||
event::Event,
|
||||
};
|
||||
|
||||
use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation};
|
||||
use super::error::LibeiEmulationCreationError;
|
||||
|
||||
pub struct LibeiEmulation {
|
||||
handshake: bool,
|
||||
@@ -107,14 +111,14 @@ impl LibeiEmulation {
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for LibeiEmulation {
|
||||
async fn consume(&mut self, event: Event, _client_handle: EmulationHandle) {
|
||||
async fn consume(&mut self, event: Event, _client_handle: ClientHandle) {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_micros() as u64;
|
||||
match event {
|
||||
Event::Pointer(p) => match p {
|
||||
PointerEvent::Motion {
|
||||
crate::event::PointerEvent::Motion {
|
||||
time: _,
|
||||
relative_x,
|
||||
relative_y,
|
||||
@@ -127,7 +131,7 @@ impl InputEmulation for LibeiEmulation {
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
}
|
||||
PointerEvent::Button {
|
||||
crate::event::PointerEvent::Button {
|
||||
time: _,
|
||||
button,
|
||||
state,
|
||||
@@ -146,7 +150,7 @@ impl InputEmulation for LibeiEmulation {
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
}
|
||||
PointerEvent::Axis {
|
||||
crate::event::PointerEvent::Axis {
|
||||
time: _,
|
||||
axis,
|
||||
value,
|
||||
@@ -162,7 +166,7 @@ impl InputEmulation for LibeiEmulation {
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
}
|
||||
PointerEvent::AxisDiscrete120 { axis, value } => {
|
||||
crate::event::PointerEvent::AxisDiscrete120 { axis, value } => {
|
||||
if !self.has_scroll {
|
||||
return;
|
||||
}
|
||||
@@ -174,10 +178,10 @@ impl InputEmulation for LibeiEmulation {
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
}
|
||||
PointerEvent::Frame {} => {}
|
||||
crate::event::PointerEvent::Frame {} => {}
|
||||
},
|
||||
Event::Keyboard(k) => match k {
|
||||
KeyboardEvent::Key {
|
||||
crate::event::KeyboardEvent::Key {
|
||||
time: _,
|
||||
key,
|
||||
state,
|
||||
@@ -196,7 +200,7 @@ impl InputEmulation for LibeiEmulation {
|
||||
d.frame(self.serial, now);
|
||||
}
|
||||
}
|
||||
KeyboardEvent::Modifiers { .. } => {}
|
||||
crate::event::KeyboardEvent::Modifiers { .. } => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
@@ -392,6 +396,7 @@ impl InputEmulation for LibeiEmulation {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create(&mut self, _: EmulationHandle) {}
|
||||
async fn destroy(&mut self, _: EmulationHandle) {}
|
||||
async fn notify(&mut self, _client_event: ClientEvent) {}
|
||||
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
use super::{EmulationHandle, InputEmulation};
|
||||
use crate::client::{ClientEvent, ClientHandle};
|
||||
use crate::emulate::InputEmulation;
|
||||
use crate::event::{Event, KeyboardEvent, PointerEvent};
|
||||
use async_trait::async_trait;
|
||||
use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
|
||||
use core_graphics::event::{
|
||||
CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, ScrollEventUnit,
|
||||
};
|
||||
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
|
||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||
use keycode::{KeyMap, KeyMapping};
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::time::Duration;
|
||||
@@ -107,7 +108,7 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8) {
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for MacOSEmulation {
|
||||
async fn consume(&mut self, event: Event, _handle: EmulationHandle) {
|
||||
async fn consume(&mut self, event: Event, _client_handle: ClientHandle) {
|
||||
match event {
|
||||
Event::Pointer(pointer_event) => match pointer_event {
|
||||
PointerEvent::Motion {
|
||||
@@ -172,22 +173,22 @@ impl InputEmulation for MacOSEmulation {
|
||||
state,
|
||||
} => {
|
||||
let (event_type, mouse_button) = match (button, state) {
|
||||
(b, 1) if b == input_event::BTN_LEFT => {
|
||||
(b, 1) if b == crate::event::BTN_LEFT => {
|
||||
(CGEventType::LeftMouseDown, CGMouseButton::Left)
|
||||
}
|
||||
(b, 0) if b == input_event::BTN_LEFT => {
|
||||
(b, 0) if b == crate::event::BTN_LEFT => {
|
||||
(CGEventType::LeftMouseUp, CGMouseButton::Left)
|
||||
}
|
||||
(b, 1) if b == input_event::BTN_RIGHT => {
|
||||
(b, 1) if b == crate::event::BTN_RIGHT => {
|
||||
(CGEventType::RightMouseDown, CGMouseButton::Right)
|
||||
}
|
||||
(b, 0) if b == input_event::BTN_RIGHT => {
|
||||
(b, 0) if b == crate::event::BTN_RIGHT => {
|
||||
(CGEventType::RightMouseUp, CGMouseButton::Right)
|
||||
}
|
||||
(b, 1) if b == input_event::BTN_MIDDLE => {
|
||||
(b, 1) if b == crate::event::BTN_MIDDLE => {
|
||||
(CGEventType::OtherMouseDown, CGMouseButton::Center)
|
||||
}
|
||||
(b, 0) if b == input_event::BTN_MIDDLE => {
|
||||
(b, 0) if b == crate::event::BTN_MIDDLE => {
|
||||
(CGEventType::OtherMouseUp, CGMouseButton::Center)
|
||||
}
|
||||
_ => {
|
||||
@@ -296,7 +297,7 @@ impl InputEmulation for MacOSEmulation {
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&mut self, _handle: EmulationHandle) {}
|
||||
async fn notify(&mut self, _client_event: ClientEvent) {}
|
||||
|
||||
async fn destroy(&mut self, _handle: EmulationHandle) {}
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
use super::error::WindowsEmulationCreationError;
|
||||
use input_event::{
|
||||
scancode, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE,
|
||||
BTN_RIGHT,
|
||||
use crate::{
|
||||
emulate::InputEmulation,
|
||||
event::{KeyboardEvent, PointerEvent},
|
||||
scancode,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use std::ops::BitOrAssign;
|
||||
use std::time::Duration;
|
||||
@@ -19,7 +19,11 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{
|
||||
};
|
||||
use windows::Win32::UI::WindowsAndMessaging::{XBUTTON1, XBUTTON2};
|
||||
|
||||
use super::{EmulationHandle, InputEmulation};
|
||||
use crate::event::{BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT};
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
event::Event,
|
||||
};
|
||||
|
||||
const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500);
|
||||
const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
||||
@@ -36,7 +40,7 @@ impl WindowsEmulation {
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for WindowsEmulation {
|
||||
async fn consume(&mut self, event: Event, _: EmulationHandle) {
|
||||
async fn consume(&mut self, event: Event, _: ClientHandle) {
|
||||
match event {
|
||||
Event::Pointer(pointer_event) => match pointer_event {
|
||||
PointerEvent::Motion {
|
||||
@@ -79,9 +83,11 @@ impl InputEmulation for WindowsEmulation {
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&mut self, _handle: EmulationHandle) {}
|
||||
async fn notify(&mut self, _: ClientEvent) {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, _handle: EmulationHandle) {}
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
|
||||
impl WindowsEmulation {
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::{error::WlrootsEmulationCreationError, InputEmulation};
|
||||
use crate::client::{ClientEvent, ClientHandle};
|
||||
use crate::emulate::{error::WlrootsEmulationCreationError, InputEmulation};
|
||||
use async_trait::async_trait;
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
@@ -26,14 +27,13 @@ use wayland_client::{
|
||||
Connection, Dispatch, EventQueue, QueueHandle,
|
||||
};
|
||||
|
||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
||||
use crate::event::{Event, KeyboardEvent, PointerEvent};
|
||||
|
||||
use super::error::WaylandBindError;
|
||||
use super::EmulationHandle;
|
||||
|
||||
struct State {
|
||||
keymap: Option<(u32, OwnedFd, u32)>,
|
||||
input_for_client: HashMap<EmulationHandle, VirtualInput>,
|
||||
input_for_client: HashMap<ClientHandle, VirtualInput>,
|
||||
seat: wl_seat::WlSeat,
|
||||
qh: QueueHandle<Self>,
|
||||
vpm: VpManager,
|
||||
@@ -64,7 +64,7 @@ impl WlrootsEmulation {
|
||||
.bind(&qh, 1..=1, ())
|
||||
.map_err(|e| WaylandBindError::new(e, "virtual-keyboard-unstable-v1"))?;
|
||||
|
||||
let input_for_client: HashMap<EmulationHandle, VirtualInput> = HashMap::new();
|
||||
let input_for_client: HashMap<ClientHandle, VirtualInput> = HashMap::new();
|
||||
|
||||
let mut emulate = WlrootsEmulation {
|
||||
last_flush_failed: false,
|
||||
@@ -89,7 +89,7 @@ impl WlrootsEmulation {
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn add_client(&mut self, client: EmulationHandle) {
|
||||
fn add_client(&mut self, client: ClientHandle) {
|
||||
let pointer: Vp = self.vpm.create_virtual_pointer(None, &self.qh, ());
|
||||
let keyboard: Vk = self.vkm.create_virtual_keyboard(&self.seat, &self.qh, ());
|
||||
|
||||
@@ -104,19 +104,12 @@ impl State {
|
||||
|
||||
self.input_for_client.insert(client, vinput);
|
||||
}
|
||||
|
||||
fn destroy_client(&mut self, handle: EmulationHandle) {
|
||||
if let Some(input) = self.input_for_client.remove(&handle) {
|
||||
input.pointer.destroy();
|
||||
input.keyboard.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for WlrootsEmulation {
|
||||
async fn consume(&mut self, event: Event, handle: EmulationHandle) {
|
||||
if let Some(virtual_input) = self.state.input_for_client.get(&handle) {
|
||||
async fn consume(&mut self, event: Event, client_handle: ClientHandle) {
|
||||
if let Some(virtual_input) = self.state.input_for_client.get(&client_handle) {
|
||||
if self.last_flush_failed {
|
||||
if let Err(WaylandError::Io(e)) = self.queue.flush() {
|
||||
if e.kind() == io::ErrorKind::WouldBlock {
|
||||
@@ -125,7 +118,9 @@ impl InputEmulation for WlrootsEmulation {
|
||||
* will overwhelm the output buffer and leave the
|
||||
* wayland connection in a broken state
|
||||
*/
|
||||
log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
|
||||
log::warn!(
|
||||
"can't keep up, discarding event: ({client_handle}) - {event:?}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -149,18 +144,16 @@ impl InputEmulation for WlrootsEmulation {
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&mut self, handle: EmulationHandle) {
|
||||
self.state.add_client(handle);
|
||||
if let Err(e) = self.queue.flush() {
|
||||
log::error!("{}", e);
|
||||
}
|
||||
}
|
||||
async fn destroy(&mut self, handle: EmulationHandle) {
|
||||
self.state.destroy_client(handle);
|
||||
if let Err(e) = self.queue.flush() {
|
||||
log::error!("{}", e);
|
||||
async fn notify(&mut self, client_event: ClientEvent) {
|
||||
if let ClientEvent::Create(client, _) = client_event {
|
||||
self.state.add_client(client);
|
||||
if let Err(e) = self.queue.flush() {
|
||||
log::error!("{}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
|
||||
struct VirtualInput {
|
||||
@@ -5,11 +5,15 @@ use x11::{
|
||||
xtest,
|
||||
};
|
||||
|
||||
use input_event::{
|
||||
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
||||
use crate::{
|
||||
client::ClientHandle,
|
||||
emulate::InputEmulation,
|
||||
event::{
|
||||
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation};
|
||||
use super::error::X11EmulationCreationError;
|
||||
|
||||
pub struct X11Emulation {
|
||||
display: *mut xlib::Display,
|
||||
@@ -98,7 +102,7 @@ impl Drop for X11Emulation {
|
||||
|
||||
#[async_trait]
|
||||
impl InputEmulation for X11Emulation {
|
||||
async fn consume(&mut self, event: Event, _: EmulationHandle) {
|
||||
async fn consume(&mut self, event: Event, _: ClientHandle) {
|
||||
match event {
|
||||
Event::Pointer(pointer_event) => match pointer_event {
|
||||
PointerEvent::Motion {
|
||||
@@ -141,11 +145,9 @@ impl InputEmulation for X11Emulation {
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&mut self, _: EmulationHandle) {
|
||||
async fn notify(&mut self, _: crate::client::ClientEvent) {
|
||||
// for our purposes it does not matter what client sent the event
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, _: EmulationHandle) {
|
||||
// for our purposes it does not matter what client sent the event
|
||||
}
|
||||
async fn destroy(&mut self) {}
|
||||
}
|
||||
@@ -8,16 +8,20 @@ use ashpd::{
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
use input_event::{
|
||||
Event::{Keyboard, Pointer},
|
||||
KeyboardEvent, PointerEvent,
|
||||
use crate::{
|
||||
client::ClientEvent,
|
||||
emulate::InputEmulation,
|
||||
event::{
|
||||
Event::{Keyboard, Pointer},
|
||||
KeyboardEvent, PointerEvent,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{error::XdpEmulationCreationError, EmulationHandle, InputEmulation};
|
||||
use super::error::XdpEmulationCreationError;
|
||||
|
||||
pub struct DesktopPortalEmulation<'a> {
|
||||
proxy: RemoteDesktop<'a>,
|
||||
session: Option<Session<'a>>,
|
||||
session: Session<'a>,
|
||||
}
|
||||
|
||||
impl<'a> DesktopPortalEmulation<'a> {
|
||||
@@ -51,7 +55,6 @@ impl<'a> DesktopPortalEmulation<'a> {
|
||||
};
|
||||
|
||||
log::debug!("started session");
|
||||
let session = Some(session);
|
||||
|
||||
Ok(Self { proxy, session })
|
||||
}
|
||||
@@ -59,7 +62,7 @@ impl<'a> DesktopPortalEmulation<'a> {
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
async fn consume(&mut self, event: input_event::Event, _client: EmulationHandle) {
|
||||
async fn consume(&mut self, event: crate::event::Event, _client: crate::client::ClientHandle) {
|
||||
match event {
|
||||
Pointer(p) => match p {
|
||||
PointerEvent::Motion {
|
||||
@@ -69,11 +72,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
} => {
|
||||
if let Err(e) = self
|
||||
.proxy
|
||||
.notify_pointer_motion(
|
||||
self.session.as_ref().expect("no session"),
|
||||
relative_x,
|
||||
relative_y,
|
||||
)
|
||||
.notify_pointer_motion(&self.session, relative_x, relative_y)
|
||||
.await
|
||||
{
|
||||
log::warn!("{e}");
|
||||
@@ -90,11 +89,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
};
|
||||
if let Err(e) = self
|
||||
.proxy
|
||||
.notify_pointer_button(
|
||||
self.session.as_ref().expect("no session"),
|
||||
button as i32,
|
||||
state,
|
||||
)
|
||||
.notify_pointer_button(&self.session, button as i32, state)
|
||||
.await
|
||||
{
|
||||
log::warn!("{e}");
|
||||
@@ -107,11 +102,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
};
|
||||
if let Err(e) = self
|
||||
.proxy
|
||||
.notify_pointer_axis_discrete(
|
||||
self.session.as_ref().expect("no session"),
|
||||
axis,
|
||||
value,
|
||||
)
|
||||
.notify_pointer_axis_discrete(&self.session, axis, value)
|
||||
.await
|
||||
{
|
||||
log::warn!("{e}");
|
||||
@@ -132,12 +123,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
};
|
||||
if let Err(e) = self
|
||||
.proxy
|
||||
.notify_pointer_axis(
|
||||
self.session.as_ref().expect("no session"),
|
||||
dx,
|
||||
dy,
|
||||
true,
|
||||
)
|
||||
.notify_pointer_axis(&self.session, dx, dy, true)
|
||||
.await
|
||||
{
|
||||
log::warn!("{e}");
|
||||
@@ -158,11 +144,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
};
|
||||
if let Err(e) = self
|
||||
.proxy
|
||||
.notify_keyboard_keycode(
|
||||
self.session.as_ref().expect("no session"),
|
||||
key as i32,
|
||||
state,
|
||||
)
|
||||
.notify_keyboard_keycode(&self.session, key as i32, state)
|
||||
.await
|
||||
{
|
||||
log::warn!("{e}");
|
||||
@@ -177,20 +159,12 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&mut self, _client: EmulationHandle) {}
|
||||
async fn destroy(&mut self, _client: EmulationHandle) {}
|
||||
}
|
||||
async fn notify(&mut self, _client: ClientEvent) {}
|
||||
|
||||
impl<'a> Drop for DesktopPortalEmulation<'a> {
|
||||
fn drop(&mut self) {
|
||||
let session = self.session.take().expect("no session");
|
||||
tokio::runtime::Handle::try_current()
|
||||
.expect("no runtime")
|
||||
.block_on(async move {
|
||||
log::debug!("closing remote desktop session");
|
||||
if let Err(e) = session.close().await {
|
||||
log::error!("failed to close remote desktop session: {e}");
|
||||
}
|
||||
});
|
||||
async fn destroy(&mut self) {
|
||||
log::debug!("closing remote desktop session");
|
||||
if let Err(e) = self.session.close().await {
|
||||
log::error!("failed to close remote desktop session: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::client::{ClientEvent, Position};
|
||||
use crate::config::Config;
|
||||
use crate::emulate;
|
||||
use crate::event::{Event, PointerEvent};
|
||||
use anyhow::Result;
|
||||
use input_event::{Event, PointerEvent};
|
||||
use std::f64::consts::PI;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::task::LocalSet;
|
||||
@@ -21,9 +23,10 @@ const FREQUENCY_HZ: f64 = 1.0;
|
||||
const RADIUS: f64 = 100.0;
|
||||
|
||||
async fn input_emulation_test(config: Config) -> Result<()> {
|
||||
let backend = config.emulation_backend.map(|b| b.into());
|
||||
let mut emulation = input_emulation::create(backend).await?;
|
||||
emulation.create(0).await;
|
||||
let mut emulation = emulate::create(config.emulation_backend).await?;
|
||||
emulation
|
||||
.notify(ClientEvent::Create(0, Position::Left))
|
||||
.await;
|
||||
let start = Instant::now();
|
||||
let mut offset = (0, 0);
|
||||
loop {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use crate::scancode;
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::{
|
||||
error::Error,
|
||||
fmt::{self, Display},
|
||||
};
|
||||
|
||||
pub mod scancode;
|
||||
|
||||
// FIXME
|
||||
pub const BTN_LEFT: u32 = 0x110;
|
||||
pub const BTN_RIGHT: u32 = 0x111;
|
||||
@@ -1,8 +1,13 @@
|
||||
pub mod client;
|
||||
pub mod config;
|
||||
pub mod dns;
|
||||
pub mod event;
|
||||
pub mod server;
|
||||
|
||||
pub mod capture;
|
||||
pub mod emulate;
|
||||
|
||||
pub mod capture_test;
|
||||
pub mod emulation_test;
|
||||
pub mod frontend;
|
||||
pub mod scancode;
|
||||
|
||||
@@ -40,7 +40,7 @@ pub struct Server {
|
||||
client_manager: Rc<RefCell<ClientManager>>,
|
||||
port: Rc<Cell<u16>>,
|
||||
state: Rc<Cell<State>>,
|
||||
release_bind: Vec<input_event::scancode::Linux>,
|
||||
release_bind: Vec<crate::scancode::Linux>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
|
||||
@@ -4,11 +4,14 @@ use std::{collections::HashSet, net::SocketAddr};
|
||||
|
||||
use tokio::{process::Command, sync::mpsc::Sender, task::JoinHandle};
|
||||
|
||||
use input_capture::{self, error::CaptureCreationError, CaptureHandle, InputCapture, Position};
|
||||
|
||||
use input_event::{scancode, Event, KeyboardEvent};
|
||||
|
||||
use crate::{client::ClientHandle, config::CaptureBackend, server::State};
|
||||
use crate::{
|
||||
capture::{self, error::CaptureCreationError, InputCapture},
|
||||
client::{ClientEvent, ClientHandle},
|
||||
config::CaptureBackend,
|
||||
event::{Event, KeyboardEvent},
|
||||
scancode,
|
||||
server::State,
|
||||
};
|
||||
|
||||
use super::Server;
|
||||
|
||||
@@ -16,10 +19,8 @@ use super::Server;
|
||||
pub enum CaptureEvent {
|
||||
/// capture must release the mouse
|
||||
Release,
|
||||
/// add a capture client
|
||||
Create(CaptureHandle, Position),
|
||||
/// destory a capture client
|
||||
Destroy(CaptureHandle),
|
||||
/// capture is notified of a change in client states
|
||||
ClientEvent(ClientEvent),
|
||||
/// termination signal
|
||||
Terminate,
|
||||
}
|
||||
@@ -32,9 +33,8 @@ pub fn new(
|
||||
release_bind: Vec<scancode::Linux>,
|
||||
) -> Result<(JoinHandle<Result<()>>, Sender<CaptureEvent>), CaptureCreationError> {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
|
||||
let backend = backend.map(|b| b.into());
|
||||
let task = tokio::task::spawn_local(async move {
|
||||
let mut capture = input_capture::create(backend).await?;
|
||||
let mut capture = capture::create(backend).await?;
|
||||
let mut pressed_keys = HashSet::new();
|
||||
loop {
|
||||
tokio::select! {
|
||||
@@ -52,9 +52,9 @@ pub fn new(
|
||||
CaptureEvent::Release => {
|
||||
capture.release()?;
|
||||
server.state.replace(State::Receiving);
|
||||
|
||||
}
|
||||
CaptureEvent::Create(h, p) => capture.create(h, p)?,
|
||||
CaptureEvent::Destroy(h) => capture.destroy(h)?,
|
||||
CaptureEvent::ClientEvent(e) => capture.notify(e)?,
|
||||
CaptureEvent::Terminate => break,
|
||||
},
|
||||
None => break,
|
||||
@@ -82,7 +82,7 @@ async fn handle_capture_event(
|
||||
capture: &mut Box<dyn InputCapture>,
|
||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||
timer_tx: &Sender<()>,
|
||||
event: (CaptureHandle, Event),
|
||||
event: (ClientHandle, Event),
|
||||
pressed_keys: &mut HashSet<scancode::Linux>,
|
||||
release_bind: &[scancode::Linux],
|
||||
) -> Result<()> {
|
||||
|
||||
@@ -6,18 +6,21 @@ use tokio::{
|
||||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use crate::{client::ClientHandle, config::EmulationBackend, server::State};
|
||||
use input_emulation::{self, error::EmulationCreationError, EmulationHandle, InputEmulation};
|
||||
use input_event::{Event, KeyboardEvent};
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientHandle},
|
||||
config::EmulationBackend,
|
||||
emulate::{self, error::EmulationCreationError, InputEmulation},
|
||||
event::{Event, KeyboardEvent},
|
||||
scancode,
|
||||
server::State,
|
||||
};
|
||||
|
||||
use super::{CaptureEvent, Server};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum EmulationEvent {
|
||||
/// create a new client
|
||||
Create(EmulationHandle),
|
||||
/// destroy a client
|
||||
Destroy(EmulationHandle),
|
||||
/// input emulation is notified of a change in client states
|
||||
ClientEvent(ClientEvent),
|
||||
/// input emulation must release keys for client
|
||||
ReleaseKeys(ClientHandle),
|
||||
/// termination signal
|
||||
@@ -34,8 +37,7 @@ pub fn new(
|
||||
) -> Result<(JoinHandle<Result<()>>, Sender<EmulationEvent>), EmulationCreationError> {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
|
||||
let emulate_task = tokio::task::spawn_local(async move {
|
||||
let backend = backend.map(|b| b.into());
|
||||
let mut emulate = input_emulation::create(backend).await?;
|
||||
let mut emulate = emulate::create(backend).await?;
|
||||
let mut last_ignored = None;
|
||||
|
||||
loop {
|
||||
@@ -47,8 +49,7 @@ pub fn new(
|
||||
emulate_event = rx.recv() => {
|
||||
match emulate_event {
|
||||
Some(e) => match e {
|
||||
EmulationEvent::Create(h) => emulate.create(h).await,
|
||||
EmulationEvent::Destroy(h) => emulate.destroy(h).await,
|
||||
EmulationEvent::ClientEvent(e) => emulate.notify(e).await,
|
||||
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulate, c).await,
|
||||
EmulationEvent::Terminate => break,
|
||||
},
|
||||
@@ -72,6 +73,8 @@ pub fn new(
|
||||
release_keys(&server, &mut emulate, client).await;
|
||||
}
|
||||
|
||||
// destroy emulator
|
||||
emulate.destroy().await;
|
||||
anyhow::Ok(())
|
||||
});
|
||||
Ok((emulate_task, tx))
|
||||
@@ -221,7 +224,7 @@ async fn release_keys(
|
||||
state: 0,
|
||||
});
|
||||
emulate.consume(event, client).await;
|
||||
if let Ok(key) = input_event::scancode::Linux::try_from(key) {
|
||||
if let Ok(key) = scancode::Linux::try_from(key) {
|
||||
log::warn!("releasing stuck key: {key:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ use tokio::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
client::{ClientHandle, Position},
|
||||
client::{ClientEvent, ClientHandle, Position},
|
||||
frontend::{self, FrontendEvent, FrontendListener, FrontendRequest},
|
||||
};
|
||||
|
||||
@@ -205,8 +205,9 @@ pub async fn deactivate_client(
|
||||
None => return,
|
||||
};
|
||||
|
||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
||||
let event = ClientEvent::Destroy(handle);
|
||||
let _ = capture.send(CaptureEvent::ClientEvent(event)).await;
|
||||
let _ = emulate.send(EmulationEvent::ClientEvent(event)).await;
|
||||
}
|
||||
|
||||
pub async fn activate_client(
|
||||
@@ -236,8 +237,9 @@ pub async fn activate_client(
|
||||
};
|
||||
|
||||
/* notify emulation, capture and frontends */
|
||||
let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
|
||||
let _ = emulate.send(EmulationEvent::Create(handle)).await;
|
||||
let event = ClientEvent::Create(handle, pos);
|
||||
let _ = capture.send(CaptureEvent::ClientEvent(event)).await;
|
||||
let _ = emulate.send(EmulationEvent::ClientEvent(event)).await;
|
||||
}
|
||||
|
||||
pub async fn remove_client(
|
||||
@@ -256,8 +258,9 @@ pub async fn remove_client(
|
||||
};
|
||||
|
||||
if active {
|
||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
||||
let destroy = ClientEvent::Destroy(handle);
|
||||
let _ = capture.send(CaptureEvent::ClientEvent(destroy)).await;
|
||||
let _ = emulate.send(EmulationEvent::ClientEvent(destroy)).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -332,11 +335,13 @@ async fn update_pos(
|
||||
// update state in event input emulator & input capture
|
||||
if changed {
|
||||
if active {
|
||||
let _ = capture.send(CaptureEvent::Destroy(handle)).await;
|
||||
let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
|
||||
let destroy = ClientEvent::Destroy(handle);
|
||||
let _ = capture.send(CaptureEvent::ClientEvent(destroy)).await;
|
||||
let _ = emulate.send(EmulationEvent::ClientEvent(destroy)).await;
|
||||
}
|
||||
let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
|
||||
let _ = emulate.send(EmulationEvent::Create(handle)).await;
|
||||
let create = ClientEvent::Create(handle, pos);
|
||||
let _ = capture.send(CaptureEvent::ClientEvent(create)).await;
|
||||
let _ = emulate.send(EmulationEvent::ClientEvent(create)).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ use tokio::{
|
||||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use crate::frontend::FrontendEvent;
|
||||
use input_event::Event;
|
||||
use crate::{event::Event, frontend::FrontendEvent};
|
||||
|
||||
use super::Server;
|
||||
|
||||
|
||||
@@ -5,9 +5,7 @@ use tokio::{
|
||||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use input_event::Event;
|
||||
|
||||
use crate::client::ClientHandle;
|
||||
use crate::{client::ClientHandle, event::Event};
|
||||
|
||||
use super::{capture_task::CaptureEvent, emulation_task::EmulationEvent, Server, State};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user