Compare commits

...

21 Commits

Author SHA1 Message Date
Ferdinand Schober
09f08f1798 fix drop impl for desktop-portal 2024-07-05 00:31:16 +02:00
Ferdinand Schober
f97621e987 adjust error handling 2024-07-04 23:44:32 +02:00
Ferdinand Schober
b3aa3f4281 remove dispatch workaround 2024-07-04 22:40:05 +02:00
Ferdinand Schober
9abec63313 update dependencies 2024-07-03 11:11:50 +02:00
Ferdinand Schober
35e626976e add explicit version 2024-07-02 22:39:20 +02:00
Ferdinand Schober
684735b499 fix dependencies in input-event 2024-07-02 22:34:43 +02:00
Ferdinand Schober
abfc744e4c specify dependency versions explicitly 2024-07-02 22:17:26 +02:00
Ferdinand Schober
fb2c39e8ae fix all features enabled by default 2024-07-02 22:07:37 +02:00
Ferdinand Schober
82ab5ecbbd fix xdg-desktop-portal backend not available 2024-07-02 22:07:37 +02:00
Ferdinand Schober
3fd2b31562 update deps 2024-07-02 22:07:37 +02:00
Ferdinand Schober
90e83cee87 purge dependencies 2024-07-02 22:07:37 +02:00
Ferdinand Schober
4db2d37f32 split into input-{event,capture,emulation} 2024-07-02 22:07:37 +02:00
Ferdinand Schober
7b511bb97d fix iteration order 2024-07-02 16:23:07 +02:00
Ferdinand Schober
70a23b9fa7 reduce coupling of emulation and capture backends 2024-07-02 16:23:07 +02:00
Ferdinand Schober
b6b16063a8 Configurable emulation backend (#151) 2024-07-01 20:09:16 +02:00
Ferdinand Schober
9cbe1ed8d8 fix release bind message 2024-06-30 14:41:16 +02:00
Ferdinand Schober
3528ef4fae Configurable capture backend (#150)
capture backend can now be configured via the `capture_backend` cli argument / config entry
2024-06-29 00:10:36 +02:00
Ferdinand Schober
232c048c19 fix transmuting to pointer types UB (#147)
closes #134
2024-06-25 14:13:05 +02:00
Orhun Parmaksız
1c37579ae5 Update Arch Linux instructions (#145) 2024-06-19 20:28:30 +02:00
Ferdinand Schober
460bacade5 fix sizeof usize assumed to be 8 (#143)
closes #141
2024-06-09 00:49:00 +02:00
虢豳
5fd3b719d6 Extract package name and version from Cargo.toml (#136)
* chore: nix flake update

* feat: Extract package name and version from Cargo.toml
2024-05-22 08:06:44 +02:00
48 changed files with 2281 additions and 1623 deletions

877
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,10 @@
[workspace]
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"
@@ -6,16 +13,16 @@ edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/ferdinandschober/lan-mouse" 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] [profile.release]
strip = true strip = true
lto = "fat" lto = "fat"
[dependencies] [dependencies]
tempfile = "3.8" 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 }
hickory-resolver = "0.24.1" hickory-resolver = "0.24.1"
memmap = "0.7"
toml = "0.8" toml = "0.8"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0.71" anyhow = "1.0.71"
@@ -23,52 +30,26 @@ log = "0.4.20"
env_logger = "0.11.3" env_logger = "0.11.3"
serde_json = "1.0.107" serde_json = "1.0.107"
tokio = {version = "1.32.0", features = ["io-util", "io-std", "macros", "net", "process", "rt", "sync", "signal"] } 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" futures = "0.3.28"
clap = { version="4.4.11", features = ["derive"] } clap = { version="4.4.11", features = ["derive"] }
gtk = { package = "gtk4", version = "0.8.1", features = ["v4_2"], optional = true } gtk = { package = "gtk4", version = "0.8.1", features = ["v4_2"], optional = true }
adw = { package = "libadwaita", version = "0.6.0", features = ["v1_1"], optional = true } adw = { package = "libadwaita", version = "0.6.0", features = ["v1_1"], optional = true }
async-channel = { version = "2.1.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" hostname = "0.4.0"
slab = "0.4.9" slab = "0.4.9"
endi = "1.1.0"
thiserror = "1.0.61"
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
libc = "0.2.148" 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] [build-dependencies]
glib-build-tools = { version = "0.19.0", optional = true } glib-build-tools = { version = "0.19.0", optional = true }
[features] [features]
default = ["wayland", "x11", "xdg_desktop_portal", "libei", "gtk"] default = [ "wayland", "x11", "xdg_desktop_portal", "libei", "gtk" ]
wayland = ["dep:wayland-client", "dep:wayland-protocols", "dep:wayland-protocols-wlr", "dep:wayland-protocols-misc" ] wayland = [ "input-capture/wayland", "input-emulation/wayland" ]
x11 = ["dep:x11"] x11 = [ "input-capture/x11", "input-emulation/x11" ]
xdg_desktop_portal = ["dep:ashpd"] xdg_desktop_portal = [ "input-emulation/xdg_desktop_portal" ]
libei = ["dep:reis", "dep:ashpd"] libei = [ "input-capture/libei", "input-emulation/libei" ]
gtk = ["dep:gtk", "dep:adw", "dep:async-channel", "dep:glib-build-tools"] gtk = ["dep:gtk", "dep:adw", "dep:async-channel", "dep:glib-build-tools"]

View File

@@ -69,7 +69,15 @@ Precompiled release binaries for Windows, MacOS and Linux are available in the [
For Windows, the depenedencies are included in the .zip file, for other operating systems see [Installing Dependencies](#installing-dependencies). For Windows, the depenedencies are included in the .zip file, for other operating systems see [Installing Dependencies](#installing-dependencies).
### Arch Linux ### Arch Linux
Lan Mouse is available on the AUR:
Lan Mouse can be installed from the [official repositories](https://archlinux.org/packages/extra/x86_64/lan-mouse/):
```sh
pacman -S lan-mouse
```
It is also available on the AUR:
```sh ```sh
# git version (includes latest changes) # git version (includes latest changes)
paru -S lan-mouse-git paru -S lan-mouse-git

View File

@@ -1,5 +1,7 @@
# example configuration # example configuration
# capture_backend = "LayerShell"
# release bind # release bind
release_bind = [ "KeyA", "KeyS", "KeyD", "KeyF" ] release_bind = [ "KeyA", "KeyS", "KeyD", "KeyF" ]

12
flake.lock generated
View File

@@ -20,11 +20,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1710806803, "lastModified": 1716293225,
"narHash": "sha256-qrxvLS888pNJFwJdK+hf1wpRCSQcqA6W5+Ox202NDa0=", "narHash": "sha256-pU9ViBVE3XYb70xZx+jK6SEVphvt7xMTbm6yDIF4xPs=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "b06025f1533a1e07b6db3e75151caa155d1c7eb3", "rev": "3eaeaeb6b1e08a016380c279f8846e0bd8808916",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -48,11 +48,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1710987136, "lastModified": 1716257780,
"narHash": "sha256-Q8GRdlAIKZ8tJUXrbcRO1pA33AdoPfTUirsSnmGQnOU=", "narHash": "sha256-R+NjvJzKEkTVCmdrKRfPE4liX/KMGVqGUwwS5H8ET8A=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "97596b54ac34ad8184ca1eef44b1ec2e5c2b5f9e", "rev": "4e5e3d2c5c9b2721bd266f9e43c14e96811b89d2",
"type": "github" "type": "github"
}, },
"original": { "original": {

48
input-capture/Cargo.toml Normal file
View File

@@ -0,0 +1,48 @@
[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", version = "0.1.0" }
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.32.1", features=["client", "staging", "unstable"], optional = true }
wayland-protocols-wlr = { version="0.3.1", 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.57.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"]

View File

@@ -4,10 +4,9 @@ use std::task::{Context, Poll};
use futures_core::Stream; use futures_core::Stream;
use crate::capture::InputCapture; use input_event::Event;
use crate::event::Event;
use crate::client::{ClientEvent, ClientHandle}; use super::{CaptureHandle, InputCapture, Position};
pub struct DummyInputCapture {} pub struct DummyInputCapture {}
@@ -24,7 +23,11 @@ impl Default for DummyInputCapture {
} }
impl InputCapture for DummyInputCapture { impl InputCapture for DummyInputCapture {
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> { fn create(&mut self, _handle: CaptureHandle, _pos: Position) -> io::Result<()> {
Ok(())
}
fn destroy(&mut self, _handle: CaptureHandle) -> io::Result<()> {
Ok(()) Ok(())
} }
@@ -34,7 +37,7 @@ impl InputCapture for DummyInputCapture {
} }
impl Stream for DummyInputCapture { impl Stream for DummyInputCapture {
type Item = io::Result<(ClientHandle, Event)>; type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Pending Poll::Pending

142
input-capture/src/error.rs Normal file
View File

@@ -0,0 +1,142 @@
use std::fmt::Display;
use thiserror::Error;
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use std::io;
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use wayland_client::{
backend::WaylandError,
globals::{BindError, GlobalError},
ConnectError, DispatchError,
};
#[derive(Debug, Error)]
pub enum CaptureCreationError {
NoAvailableBackend,
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Libei(#[from] LibeiCaptureCreationError),
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
LayerShell(#[from] LayerShellCaptureCreationError),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
X11(#[from] X11InputCaptureCreationError),
#[cfg(target_os = "macos")]
Macos(#[from] MacOSInputCaptureCreationError),
#[cfg(windows)]
Windows,
}
impl Display for CaptureCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reason = match self {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
CaptureCreationError::Libei(reason) => {
format!("error creating portal backend: {reason}")
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
CaptureCreationError::LayerShell(reason) => {
format!("error creating layer-shell backend: {reason}")
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
CaptureCreationError::X11(e) => format!("{e}"),
#[cfg(target_os = "macos")]
CaptureCreationError::Macos(e) => format!("{e}"),
#[cfg(windows)]
CaptureCreationError::Windows => String::new(),
CaptureCreationError::NoAvailableBackend => "no available backend".to_string(),
};
write!(f, "could not create input capture: {reason}")
}
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum LibeiCaptureCreationError {
Ashpd(#[from] ashpd::Error),
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
impl Display for LibeiCaptureCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LibeiCaptureCreationError::Ashpd(portal_error) => write!(f, "{portal_error}"),
}
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub struct WaylandBindError {
inner: BindError,
protocol: &'static str,
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl WaylandBindError {
pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
Self { inner, protocol }
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for WaylandBindError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} protocol not supported: {}",
self.protocol, self.inner
)
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum LayerShellCaptureCreationError {
Connect(#[from] ConnectError),
Global(#[from] GlobalError),
Wayland(#[from] WaylandError),
Bind(#[from] WaylandBindError),
Dispatch(#[from] DispatchError),
Io(#[from] io::Error),
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for LayerShellCaptureCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LayerShellCaptureCreationError::Bind(e) => write!(f, "{e}"),
LayerShellCaptureCreationError::Connect(e) => {
write!(f, "could not connect to wayland compositor: {e}")
}
LayerShellCaptureCreationError::Global(e) => write!(f, "wayland error: {e}"),
LayerShellCaptureCreationError::Wayland(e) => write!(f, "wayland error: {e}"),
LayerShellCaptureCreationError::Dispatch(e) => {
write!(f, "error dispatching wayland events: {e}")
}
LayerShellCaptureCreationError::Io(e) => write!(f, "io error: {e}"),
}
}
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum X11InputCaptureCreationError {
NotImplemented,
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
impl Display for X11InputCaptureCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "X11 input capture is not yet implemented :(")
}
}
#[cfg(target_os = "macos")]
#[derive(Debug, Error)]
pub enum MacOSInputCaptureCreationError {
NotImplemented,
}
#[cfg(target_os = "macos")]
impl Display for MacOSInputCaptureCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "macos input capture is not yet implemented :(")
}
}

159
input-capture/src/lib.rs Normal file
View File

@@ -0,0 +1,159 @@
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)
}

View File

@@ -30,23 +30,24 @@ use tokio::{
use futures_core::Stream; use futures_core::Stream;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::{ use input_event::{Event, KeyboardEvent, PointerEvent};
capture::InputCapture as LanMouseInputCapture,
client::{ClientEvent, ClientHandle, Position}, use super::{
event::{Event, KeyboardEvent, PointerEvent}, error::LibeiCaptureCreationError, CaptureHandle, InputCapture as LanMouseInputCapture, Position,
}; };
#[derive(Debug)] #[derive(Debug)]
enum ProducerEvent { enum ProducerEvent {
Release, Release,
ClientEvent(ClientEvent), Create(CaptureHandle, Position),
Destroy(CaptureHandle),
} }
#[allow(dead_code)] #[allow(dead_code)]
pub struct LibeiInputCapture<'a> { pub struct LibeiInputCapture<'a> {
input_capture: Pin<Box<InputCapture<'a>>>, input_capture: Pin<Box<InputCapture<'a>>>,
libei_task: JoinHandle<Result<()>>, libei_task: JoinHandle<Result<()>>,
event_rx: tokio::sync::mpsc::Receiver<(ClientHandle, Event)>, event_rx: tokio::sync::mpsc::Receiver<(CaptureHandle, Event)>,
notify_tx: tokio::sync::mpsc::Sender<ProducerEvent>, notify_tx: tokio::sync::mpsc::Sender<ProducerEvent>,
} }
@@ -79,9 +80,9 @@ fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) {
fn select_barriers( fn select_barriers(
zones: &Zones, zones: &Zones,
clients: &Vec<(ClientHandle, Position)>, clients: &Vec<(CaptureHandle, Position)>,
next_barrier_id: &mut u32, next_barrier_id: &mut u32,
) -> (Vec<Barrier>, HashMap<BarrierID, ClientHandle>) { ) -> (Vec<Barrier>, HashMap<BarrierID, CaptureHandle>) {
let mut client_for_barrier = HashMap::new(); let mut client_for_barrier = HashMap::new();
let mut barriers: Vec<Barrier> = vec![]; let mut barriers: Vec<Barrier> = vec![];
@@ -105,9 +106,9 @@ fn select_barriers(
async fn update_barriers( async fn update_barriers(
input_capture: &InputCapture<'_>, input_capture: &InputCapture<'_>,
session: &Session<'_>, session: &Session<'_>,
active_clients: &Vec<(ClientHandle, Position)>, active_clients: &Vec<(CaptureHandle, Position)>,
next_barrier_id: &mut u32, next_barrier_id: &mut u32,
) -> Result<HashMap<BarrierID, ClientHandle>> { ) -> Result<HashMap<BarrierID, CaptureHandle>> {
let zones = input_capture.zones(session).await?.response()?; let zones = input_capture.zones(session).await?.response()?;
log::debug!("zones: {zones:?}"); log::debug!("zones: {zones:?}");
@@ -131,7 +132,7 @@ impl<'a> Drop for LibeiInputCapture<'a> {
async fn create_session<'a>( async fn create_session<'a>(
input_capture: &'a InputCapture<'a>, input_capture: &'a InputCapture<'a>,
) -> Result<(Session<'a>, BitFlags<Capabilities>)> { ) -> std::result::Result<(Session<'a>, BitFlags<Capabilities>), ashpd::Error> {
log::debug!("creating input capture session"); log::debug!("creating input capture session");
let (session, capabilities) = loop { let (session, capabilities) = loop {
match input_capture match input_capture
@@ -183,8 +184,8 @@ async fn connect_to_eis(
async fn libei_event_handler( async fn libei_event_handler(
mut ei_event_stream: EiConvertEventStream, mut ei_event_stream: EiConvertEventStream,
context: ei::Context, context: ei::Context,
event_tx: Sender<(ClientHandle, Event)>, event_tx: Sender<(CaptureHandle, Event)>,
current_client: Rc<Cell<Option<ClientHandle>>>, current_client: Rc<Cell<Option<CaptureHandle>>>,
) -> Result<()> { ) -> Result<()> {
loop { loop {
let ei_event = match ei_event_stream.next().await { let ei_event = match ei_event_stream.next().await {
@@ -200,12 +201,12 @@ async fn libei_event_handler(
async fn wait_for_active_client( async fn wait_for_active_client(
notify_rx: &mut Receiver<ProducerEvent>, notify_rx: &mut Receiver<ProducerEvent>,
active_clients: &mut Vec<(ClientHandle, Position)>, active_clients: &mut Vec<(CaptureHandle, Position)>,
) -> Result<()> { ) -> Result<()> {
// wait for a client update // wait for a client update
while let Some(producer_event) = notify_rx.recv().await { while let Some(producer_event) = notify_rx.recv().await {
if let ProducerEvent::ClientEvent(c) = producer_event { if let ProducerEvent::Create(c, p) = producer_event {
handle_producer_event(ProducerEvent::ClientEvent(c), active_clients)?; handle_producer_event(ProducerEvent::Create(c, p), active_clients)?;
break; break;
} }
} }
@@ -213,7 +214,7 @@ async fn wait_for_active_client(
} }
impl<'a> LibeiInputCapture<'a> { impl<'a> LibeiInputCapture<'a> {
pub async fn new() -> Result<Self> { pub async fn new() -> std::result::Result<Self, LibeiCaptureCreationError> {
let input_capture = Box::pin(InputCapture::new().await?); let input_capture = Box::pin(InputCapture::new().await?);
let input_capture_ptr = input_capture.as_ref().get_ref() as *const InputCapture<'static>; let input_capture_ptr = input_capture.as_ref().get_ref() as *const InputCapture<'static>;
let mut first_session = Some(create_session(unsafe { &*input_capture_ptr }).await?); let mut first_session = Some(create_session(unsafe { &*input_capture_ptr }).await?);
@@ -224,7 +225,7 @@ impl<'a> LibeiInputCapture<'a> {
/* safety: libei_task does not outlive Self */ /* safety: libei_task does not outlive Self */
let input_capture = unsafe { &*input_capture_ptr }; let input_capture = unsafe { &*input_capture_ptr };
let mut active_clients: Vec<(ClientHandle, Position)> = vec![]; let mut active_clients: Vec<(CaptureHandle, Position)> = vec![];
let mut next_barrier_id = 1u32; let mut next_barrier_id = 1u32;
/* there is a bug in xdg-remote-desktop-portal-gnome / mutter that /* there is a bug in xdg-remote-desktop-portal-gnome / mutter that
@@ -347,8 +348,8 @@ async fn release_capture(
input_capture: &InputCapture<'_>, input_capture: &InputCapture<'_>,
session: &Session<'_>, session: &Session<'_>,
activated: Activated, activated: Activated,
current_client: ClientHandle, current_client: CaptureHandle,
active_clients: &[(ClientHandle, Position)], active_clients: &[(CaptureHandle, Position)],
) -> Result<()> { ) -> Result<()> {
log::debug!("releasing input capture {}", activated.activation_id()); log::debug!("releasing input capture {}", activated.activation_id());
let (x, y) = activated.cursor_position(); let (x, y) = activated.cursor_position();
@@ -375,16 +376,16 @@ async fn release_capture(
fn handle_producer_event( fn handle_producer_event(
producer_event: ProducerEvent, producer_event: ProducerEvent,
active_clients: &mut Vec<(ClientHandle, Position)>, active_clients: &mut Vec<(CaptureHandle, Position)>,
) -> Result<bool> { ) -> Result<bool> {
log::debug!("handling event: {producer_event:?}"); log::debug!("handling event: {producer_event:?}");
let updated = match producer_event { let updated = match producer_event {
ProducerEvent::Release => false, ProducerEvent::Release => false,
ProducerEvent::ClientEvent(ClientEvent::Create(c, p)) => { ProducerEvent::Create(c, p) => {
active_clients.push((c, p)); active_clients.push((c, p));
true true
} }
ProducerEvent::ClientEvent(ClientEvent::Destroy(c)) => { ProducerEvent::Destroy(c) => {
active_clients.retain(|(h, _)| *h != c); active_clients.retain(|(h, _)| *h != c);
true true
} }
@@ -394,9 +395,9 @@ fn handle_producer_event(
async fn handle_ei_event( async fn handle_ei_event(
ei_event: EiEvent, ei_event: EiEvent,
current_client: Option<ClientHandle>, current_client: Option<CaptureHandle>,
context: &ei::Context, context: &ei::Context,
event_tx: &Sender<(ClientHandle, Event)>, event_tx: &Sender<(CaptureHandle, Event)>,
) { ) {
match ei_event { match ei_event {
EiEvent::SeatAdded(s) => { EiEvent::SeatAdded(s) => {
@@ -545,12 +546,18 @@ async fn handle_ei_event(
} }
impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> { impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
fn notify(&mut self, event: ClientEvent) -> io::Result<()> { fn create(&mut self, handle: super::CaptureHandle, pos: super::Position) -> io::Result<()> {
let notify_tx = self.notify_tx.clone(); let notify_tx = self.notify_tx.clone();
tokio::task::spawn_local(async move { tokio::task::spawn_local(async move {
log::debug!("notifying {event:?}"); let _ = notify_tx.send(ProducerEvent::Create(handle, pos)).await;
let _ = notify_tx.send(ProducerEvent::ClientEvent(event)).await; });
log::debug!("done !"); 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;
}); });
Ok(()) Ok(())
} }
@@ -558,7 +565,6 @@ impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
fn release(&mut self) -> io::Result<()> { fn release(&mut self) -> io::Result<()> {
let notify_tx = self.notify_tx.clone(); let notify_tx = self.notify_tx.clone();
tokio::task::spawn_local(async move { tokio::task::spawn_local(async move {
log::debug!("notifying Release");
let _ = notify_tx.send(ProducerEvent::Release).await; let _ = notify_tx.send(ProducerEvent::Release).await;
}); });
Ok(()) Ok(())
@@ -566,7 +572,7 @@ impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
} }
impl<'a> Stream for LibeiInputCapture<'a> { impl<'a> Stream for LibeiInputCapture<'a> {
type Item = io::Result<(ClientHandle, Event)>; type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match ready!(self.event_rx.poll_recv(cx)) { match ready!(self.event_rx.poll_recv(cx)) {

View File

@@ -0,0 +1,35 @@
use crate::{error::MacOSInputCaptureCreationError, CaptureHandle, InputCapture, Position};
use futures_core::Stream;
use input_event::Event;
use std::task::{Context, Poll};
use std::{io, pin::Pin};
pub struct MacOSInputCapture;
impl MacOSInputCapture {
pub fn new() -> std::result::Result<Self, MacOSInputCaptureCreationError> {
Err(MacOSInputCaptureCreationError::NotImplemented)
}
}
impl Stream for MacOSInputCapture {
type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Pending
}
}
impl InputCapture for MacOSInputCapture {
fn create(&mut self, _id: CaptureHandle, _pos: Position) -> io::Result<()> {
Ok(())
}
fn destroy(&mut self, _id: CaptureHandle) -> io::Result<()> {
Ok(())
}
fn release(&mut self) -> io::Result<()> {
Ok(())
}
}

View File

@@ -1,9 +1,3 @@
use crate::{
capture::InputCapture,
client::{ClientEvent, ClientHandle, Position},
};
use anyhow::{anyhow, Result};
use futures_core::Stream; use futures_core::Stream;
use memmap::MmapOptions; use memmap::MmapOptions;
use std::{ use std::{
@@ -66,7 +60,12 @@ use wayland_client::{
use tempfile; use tempfile;
use crate::event::{Event, KeyboardEvent, PointerEvent}; use input_event::{Event, KeyboardEvent, PointerEvent};
use super::{
error::{LayerShellCaptureCreationError, WaylandBindError},
CaptureHandle, InputCapture, Position,
};
struct Globals { struct Globals {
compositor: wl_compositor::WlCompositor, compositor: wl_compositor::WlCompositor,
@@ -103,13 +102,13 @@ struct State {
pointer_lock: Option<ZwpLockedPointerV1>, pointer_lock: Option<ZwpLockedPointerV1>,
rel_pointer: Option<ZwpRelativePointerV1>, rel_pointer: Option<ZwpRelativePointerV1>,
shortcut_inhibitor: Option<ZwpKeyboardShortcutsInhibitorV1>, shortcut_inhibitor: Option<ZwpKeyboardShortcutsInhibitorV1>,
client_for_window: Vec<(Rc<Window>, ClientHandle)>, client_for_window: Vec<(Rc<Window>, CaptureHandle)>,
focused: Option<(Rc<Window>, ClientHandle)>, focused: Option<(Rc<Window>, CaptureHandle)>,
g: Globals, g: Globals,
wayland_fd: OwnedFd, wayland_fd: OwnedFd,
read_guard: Option<ReadEventsGuard>, read_guard: Option<ReadEventsGuard>,
qh: QueueHandle<Self>, qh: QueueHandle<Self>,
pending_events: VecDeque<(ClientHandle, Event)>, pending_events: VecDeque<(CaptureHandle, Event)>,
output_info: Vec<(WlOutput, OutputInfo)>, output_info: Vec<(WlOutput, OutputInfo)>,
scroll_discrete_pending: bool, scroll_discrete_pending: bool,
} }
@@ -258,64 +257,37 @@ fn draw(f: &mut File, (width, height): (u32, u32)) {
} }
impl WaylandInputCapture { impl WaylandInputCapture {
pub fn new() -> Result<Self> { pub fn new() -> std::result::Result<Self, LayerShellCaptureCreationError> {
let conn = match Connection::connect_to_env() { let conn = Connection::connect_to_env()?;
Ok(c) => c, let (g, mut queue) = registry_queue_init::<State>(&conn)?;
Err(e) => return Err(anyhow!("could not connect to wayland compositor: {e:?}")),
};
let (g, mut queue) = match registry_queue_init::<State>(&conn) {
Ok(q) => q,
Err(e) => return Err(anyhow!("failed to initialize wl_registry: {e:?}")),
};
let qh = queue.handle(); let qh = queue.handle();
let compositor: wl_compositor::WlCompositor = match g.bind(&qh, 4..=5, ()) { let compositor: wl_compositor::WlCompositor = g
Ok(compositor) => compositor, .bind(&qh, 4..=5, ())
Err(_) => return Err(anyhow!("wl_compositor >= v4 not supported")), .map_err(|e| WaylandBindError::new(e, "wl_compositor 4..=5"))?;
}; let xdg_output_manager: ZxdgOutputManagerV1 = g
.bind(&qh, 1..=3, ())
let xdg_output_manager: ZxdgOutputManagerV1 = match g.bind(&qh, 1..=3, ()) { .map_err(|e| WaylandBindError::new(e, "xdg_output_manager 1..=3"))?;
Ok(xdg_output_manager) => xdg_output_manager, let shm: wl_shm::WlShm = g
Err(_) => return Err(anyhow!("xdg_output not supported!")), .bind(&qh, 1..=1, ())
}; .map_err(|e| WaylandBindError::new(e, "wl_shm"))?;
let layer_shell: ZwlrLayerShellV1 = g
let shm: wl_shm::WlShm = match g.bind(&qh, 1..=1, ()) { .bind(&qh, 3..=4, ())
Ok(wl_shm) => wl_shm, .map_err(|e| WaylandBindError::new(e, "wlr_layer_shell 3..=4"))?;
Err(_) => return Err(anyhow!("wl_shm v1 not supported")), let seat: wl_seat::WlSeat = g
}; .bind(&qh, 7..=8, ())
.map_err(|e| WaylandBindError::new(e, "wl_seat 7..=8"))?;
let layer_shell: ZwlrLayerShellV1 = match g.bind(&qh, 3..=4, ()) {
Ok(layer_shell) => layer_shell,
Err(_) => return Err(anyhow!("zwlr_layer_shell_v1 >= v3 not supported - required to display a surface at the edge of the screen")),
};
let seat: wl_seat::WlSeat = match g.bind(&qh, 7..=8, ()) {
Ok(wl_seat) => wl_seat,
Err(_) => return Err(anyhow!("wl_seat >= v7 not supported")),
};
let pointer_constraints: ZwpPointerConstraintsV1 = match g.bind(&qh, 1..=1, ()) {
Ok(pointer_constraints) => pointer_constraints,
Err(_) => return Err(anyhow!("zwp_pointer_constraints_v1 not supported")),
};
let relative_pointer_manager: ZwpRelativePointerManagerV1 = match g.bind(&qh, 1..=1, ()) {
Ok(relative_pointer_manager) => relative_pointer_manager,
Err(_) => return Err(anyhow!("zwp_relative_pointer_manager_v1 not supported")),
};
let shortcut_inhibit_manager: ZwpKeyboardShortcutsInhibitManagerV1 =
match g.bind(&qh, 1..=1, ()) {
Ok(shortcut_inhibit_manager) => shortcut_inhibit_manager,
Err(_) => {
return Err(anyhow!(
"zwp_keyboard_shortcuts_inhibit_manager_v1 not supported"
))
}
};
let pointer_constraints: ZwpPointerConstraintsV1 = g
.bind(&qh, 1..=1, ())
.map_err(|e| WaylandBindError::new(e, "zwp_pointer_constraints_v1"))?;
let relative_pointer_manager: ZwpRelativePointerManagerV1 = g
.bind(&qh, 1..=1, ())
.map_err(|e| WaylandBindError::new(e, "zwp_relative_pointer_manager_v1"))?;
let shortcut_inhibit_manager: ZwpKeyboardShortcutsInhibitManagerV1 = g
.bind(&qh, 1..=1, ())
.map_err(|e| WaylandBindError::new(e, "zwp_keyboard_shortcuts_inhibit_manager_v1"))?;
let outputs = vec![]; let outputs = vec![];
let g = Globals { let g = Globals {
@@ -395,11 +367,11 @@ impl WaylandInputCapture {
Ok(WaylandInputCapture(inner)) Ok(WaylandInputCapture(inner))
} }
fn add_client(&mut self, handle: ClientHandle, pos: Position) { fn add_client(&mut self, handle: CaptureHandle, pos: Position) {
self.0.get_mut().state.add_client(handle, pos); self.0.get_mut().state.add_client(handle, pos);
} }
fn delete_client(&mut self, handle: ClientHandle) { fn delete_client(&mut self, handle: CaptureHandle) {
let inner = self.0.get_mut(); let inner = self.0.get_mut();
// remove all windows corresponding to this client // remove all windows corresponding to this client
while let Some(i) = inner while let Some(i) = inner
@@ -497,7 +469,7 @@ impl State {
} }
} }
fn add_client(&mut self, client: ClientHandle, pos: Position) { fn add_client(&mut self, client: CaptureHandle, pos: Position) {
let outputs = get_output_configuration(self, pos); let outputs = get_output_configuration(self, pos);
log::debug!("outputs: {outputs:?}"); log::debug!("outputs: {outputs:?}");
@@ -590,15 +562,13 @@ impl Inner {
} }
impl InputCapture for WaylandInputCapture { impl InputCapture for WaylandInputCapture {
fn notify(&mut self, client_event: ClientEvent) -> io::Result<()> { fn create(&mut self, handle: CaptureHandle, pos: Position) -> io::Result<()> {
match client_event { self.add_client(handle, pos);
ClientEvent::Create(handle, pos) => { let inner = self.0.get_mut();
self.add_client(handle, pos); inner.flush_events()
} }
ClientEvent::Destroy(handle) => { fn destroy(&mut self, handle: CaptureHandle) -> io::Result<()> {
self.delete_client(handle); self.delete_client(handle);
}
}
let inner = self.0.get_mut(); let inner = self.0.get_mut();
inner.flush_events() inner.flush_events()
} }
@@ -612,7 +582,7 @@ impl InputCapture for WaylandInputCapture {
} }
impl Stream for WaylandInputCapture { impl Stream for WaylandInputCapture {
type Item = io::Result<(ClientHandle, Event)>; type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { 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() { if let Some(event) = self.0.get_mut().state.pending_events.pop_front() {

View File

@@ -9,7 +9,7 @@ use std::ptr::{addr_of, addr_of_mut};
use futures::executor::block_on; use futures::executor::block_on;
use std::default::Default; use std::default::Default;
use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::mpsc; use std::sync::{mpsc, Mutex};
use std::task::ready; use std::task::ready;
use std::{io, pin::Pin, thread}; use std::{io, pin::Pin, thread};
use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio::sync::mpsc::{channel, Receiver, Sender};
@@ -32,25 +32,25 @@ use windows::Win32::UI::WindowsAndMessaging::{
WNDPROC, WNDPROC,
}; };
use crate::client::Position; use input_event::{
use crate::event::{ scancode::{self, Linux},
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 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 { pub struct WindowsInputCapture {
event_rx: Receiver<(ClientHandle, Event)>, event_rx: Receiver<(CaptureHandle, Event)>,
msg_thread: Option<std::thread::JoinHandle<()>>, msg_thread: Option<std::thread::JoinHandle<()>>,
} }
enum EventType { enum EventType {
ClientEvent = 0, Request = 0,
Release = 1, Release = 1,
Exit = 2, Exit = 2,
} }
@@ -59,15 +59,28 @@ unsafe fn signal_message_thread(event_type: EventType) {
if let Some(event_tid) = get_event_tid() { if let Some(event_tid) = get_event_tid() {
PostThreadMessageW(event_tid, WM_USER, WPARAM(event_type as usize), LPARAM(0)).unwrap(); PostThreadMessageW(event_tid, WM_USER, WPARAM(event_type as usize), LPARAM(0)).unwrap();
} else { } else {
log::warn!("lost event"); panic!();
} }
} }
impl InputCapture for WindowsInputCapture { impl InputCapture for WindowsInputCapture {
fn notify(&mut self, event: ClientEvent) -> io::Result<()> { fn create(&mut self, handle: CaptureHandle, pos: Position) -> io::Result<()> {
unsafe { unsafe {
EVENT_BUFFER.push(event); {
signal_message_thread(EventType::ClientEvent); 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);
} }
Ok(()) Ok(())
} }
@@ -78,10 +91,10 @@ impl InputCapture for WindowsInputCapture {
} }
} }
static mut EVENT_BUFFER: Vec<ClientEvent> = Vec::new(); static mut REQUEST_BUFFER: Mutex<Vec<Request>> = Mutex::new(Vec::new());
static mut ACTIVE_CLIENT: Option<ClientHandle> = None; static mut ACTIVE_CLIENT: Option<CaptureHandle> = None;
static mut CLIENT_FOR_POS: Lazy<HashMap<Position, ClientHandle>> = Lazy::new(HashMap::new); static mut CLIENT_FOR_POS: Lazy<HashMap<Position, CaptureHandle>> = Lazy::new(HashMap::new);
static mut EVENT_TX: Option<Sender<(ClientHandle, Event)>> = None; static mut EVENT_TX: Option<Sender<(CaptureHandle, Event)>> = None;
static mut EVENT_THREAD_ID: AtomicU32 = AtomicU32::new(0); static mut EVENT_THREAD_ID: AtomicU32 = AtomicU32::new(0);
unsafe fn set_event_tid(tid: u32) { unsafe fn set_event_tid(tid: u32) {
EVENT_THREAD_ID.store(tid, Ordering::SeqCst); EVENT_THREAD_ID.store(tid, Ordering::SeqCst);
@@ -96,8 +109,7 @@ unsafe fn get_event_tid() -> Option<u32> {
static mut ENTRY_POINT: (i32, i32) = (0, 0); static mut ENTRY_POINT: (i32, i32) = (0, 0);
fn to_mouse_event(wparam: WPARAM, lparam: LPARAM) -> Option<PointerEvent> { fn to_mouse_event(wparam: WPARAM, lparam: LPARAM) -> Option<PointerEvent> {
let mouse_low_level: MSLLHOOKSTRUCT = let mouse_low_level: MSLLHOOKSTRUCT = unsafe { *(lparam.0 as *const MSLLHOOKSTRUCT) };
unsafe { *std::mem::transmute::<LPARAM, *const MSLLHOOKSTRUCT>(lparam) };
match wparam { match wparam {
WPARAM(p) if p == WM_LBUTTONDOWN as usize => Some(PointerEvent::Button { WPARAM(p) if p == WM_LBUTTONDOWN as usize => Some(PointerEvent::Button {
time: 0, time: 0,
@@ -167,8 +179,7 @@ fn to_mouse_event(wparam: WPARAM, lparam: LPARAM) -> Option<PointerEvent> {
} }
unsafe fn to_key_event(wparam: WPARAM, lparam: LPARAM) -> Option<KeyboardEvent> { unsafe fn to_key_event(wparam: WPARAM, lparam: LPARAM) -> Option<KeyboardEvent> {
let kybrdllhookstruct: KBDLLHOOKSTRUCT = let kybrdllhookstruct: KBDLLHOOKSTRUCT = *(lparam.0 as *const KBDLLHOOKSTRUCT);
*std::mem::transmute::<LPARAM, *const KBDLLHOOKSTRUCT>(lparam);
let mut scan_code = kybrdllhookstruct.scanCode; let mut scan_code = kybrdllhookstruct.scanCode;
log::trace!("scan_code: {scan_code}"); log::trace!("scan_code: {scan_code}");
if kybrdllhookstruct.flags.contains(LLKHF_EXTENDED) { if kybrdllhookstruct.flags.contains(LLKHF_EXTENDED) {
@@ -247,8 +258,7 @@ unsafe fn check_client_activation(wparam: WPARAM, lparam: LPARAM) -> bool {
if wparam.0 != WM_MOUSEMOVE as usize { if wparam.0 != WM_MOUSEMOVE as usize {
return ACTIVE_CLIENT.is_some(); return ACTIVE_CLIENT.is_some();
} }
let mouse_low_level: MSLLHOOKSTRUCT = let mouse_low_level: MSLLHOOKSTRUCT = *(lparam.0 as *const MSLLHOOKSTRUCT);
unsafe { *std::mem::transmute::<LPARAM, *const MSLLHOOKSTRUCT>(lparam) };
static mut PREV_POS: Option<(i32, i32)> = None; static mut PREV_POS: Option<(i32, i32)> = None;
let curr_pos = (mouse_low_level.pt.x, mouse_low_level.pt.y); let curr_pos = (mouse_low_level.pt.x, mouse_low_level.pt.y);
let prev_pos = PREV_POS.unwrap_or(curr_pos); let prev_pos = PREV_POS.unwrap_or(curr_pos);
@@ -359,7 +369,6 @@ fn enumerate_displays() -> Vec<RECT> {
break; break;
} }
if device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP != 0 { if device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP != 0 {
log::info!("{:?}", device.DeviceName);
devices.push(device.DeviceName); devices.push(device.DeviceName);
} }
} }
@@ -538,28 +547,37 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
x if x == EventType::Release as usize => { x if x == EventType::Release as usize => {
ACTIVE_CLIENT.take(); ACTIVE_CLIENT.take();
} }
x if x == EventType::ClientEvent as usize => { x if x == EventType::Request as usize => {
while let Some(event) = EVENT_BUFFER.pop() { let requests = {
update_clients(event) 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)
} }
} }
_ => {} _ => {}
} }
} else { } else {
/* other messages for window_procs */ /* other messages for window_procs */
TranslateMessage(&msg); let _ = TranslateMessage(&msg);
DispatchMessageW(&msg); DispatchMessageW(&msg);
} }
} }
} }
} }
fn update_clients(client_event: ClientEvent) { fn update_clients(request: Request) {
match client_event { match request {
ClientEvent::Create(handle, pos) => { Request::Create(handle, pos) => {
unsafe { CLIENT_FOR_POS.insert(pos, handle) }; unsafe { CLIENT_FOR_POS.insert(pos, handle) };
} }
ClientEvent::Destroy(handle) => unsafe { Request::Destroy(handle) => unsafe {
for pos in [ for pos in [
Position::Left, Position::Left,
Position::Right, Position::Right,
@@ -578,7 +596,7 @@ fn update_clients(client_event: ClientEvent) {
} }
impl WindowsInputCapture { impl WindowsInputCapture {
pub(crate) fn new() -> Result<Self> { pub(crate) fn new() -> Self {
unsafe { unsafe {
let (tx, rx) = channel(10); let (tx, rx) = channel(10);
EVENT_TX.replace(tx); EVENT_TX.replace(tx);
@@ -586,16 +604,16 @@ impl WindowsInputCapture {
let msg_thread = Some(thread::spawn(|| message_thread(ready_tx))); let msg_thread = Some(thread::spawn(|| message_thread(ready_tx)));
/* wait for thread to set its id */ /* wait for thread to set its id */
ready_rx.recv().expect("channel closed"); ready_rx.recv().expect("channel closed");
Ok(Self { Self {
msg_thread, msg_thread,
event_rx: rx, event_rx: rx,
}) }
} }
} }
} }
impl Stream for WindowsInputCapture { impl Stream for WindowsInputCapture {
type Item = io::Result<(ClientHandle, Event)>; type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match ready!(self.event_rx.poll_recv(cx)) { match ready!(self.event_rx.poll_recv(cx)) {
None => Poll::Ready(None), None => Poll::Ready(None),

43
input-capture/src/x11.rs Normal file
View File

@@ -0,0 +1,43 @@
use std::io;
use std::task::Poll;
use futures_core::Stream;
use super::InputCapture;
use input_event::Event;
use super::error::X11InputCaptureCreationError;
use super::{CaptureHandle, Position};
pub struct X11InputCapture {}
impl X11InputCapture {
pub fn new() -> std::result::Result<Self, X11InputCaptureCreationError> {
Err(X11InputCaptureCreationError::NotImplemented)
}
}
impl InputCapture for X11InputCapture {
fn create(&mut self, _id: CaptureHandle, _pos: Position) -> io::Result<()> {
Ok(())
}
fn destroy(&mut self, _id: CaptureHandle) -> io::Result<()> {
Ok(())
}
fn release(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Stream for X11InputCapture {
type Item = io::Result<(CaptureHandle, Event)>;
fn poll_next(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
Poll::Pending
}
}

View File

@@ -0,0 +1,48 @@
[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", version = "0.1.0" }
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.32.1", features=["client", "staging", "unstable"], optional = true }
wayland-protocols-wlr = { version="0.3.1", features=["client"], optional = true }
wayland-protocols-misc = { version="0.3.1", 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.57.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"]

View File

@@ -0,0 +1,29 @@
use async_trait::async_trait;
use input_event::Event;
use crate::error::EmulationError;
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,
) -> Result<(), EmulationError> {
log::info!("received event: ({client_handle}) {event}");
Ok(())
}
async fn create(&mut self, _: EmulationHandle) {}
async fn destroy(&mut self, _: EmulationHandle) {}
}

View File

@@ -0,0 +1,192 @@
use std::{fmt::Display, io};
use thiserror::Error;
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use wayland_client::{
backend::WaylandError,
globals::{BindError, GlobalError},
ConnectError, DispatchError,
};
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::HandshakeError;
#[derive(Debug, Error)]
pub enum EmulationError {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei error flushing events: `{0}`")]
Libei(#[from] reis::event::Error),
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[error("wayland error: `{0}`")]
Wayland(#[from] wayland_client::backend::WaylandError),
#[cfg(all(
unix,
any(feature = "xdg_desktop_portal", feature = "libei"),
not(target_os = "macos")
))]
#[error("xdg-desktop-portal: `{0}`")]
Ashpd(#[from] ashpd::Error),
#[error("io error: `{0}`")]
Io(#[from] io::Error),
}
#[derive(Debug, Error)]
pub enum EmulationCreationError {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Wlroots(#[from] WlrootsEmulationCreationError),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Libei(#[from] LibeiEmulationCreationError),
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Xdp(#[from] XdpEmulationCreationError),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
X11(#[from] X11EmulationCreationError),
#[cfg(target_os = "macos")]
MacOs(#[from] MacOSEmulationCreationError),
#[cfg(windows)]
Windows(#[from] WindowsEmulationCreationError),
NoAvailableBackend,
}
impl Display for EmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reason = match self {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
EmulationCreationError::Wlroots(e) => format!("wlroots backend: {e}"),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
EmulationCreationError::Libei(e) => format!("libei backend: {e}"),
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
EmulationCreationError::Xdp(e) => format!("desktop portal backend: {e}"),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
EmulationCreationError::X11(e) => format!("x11 backend: {e}"),
#[cfg(target_os = "macos")]
EmulationCreationError::MacOs(e) => format!("macos backend: {e}"),
#[cfg(windows)]
EmulationCreationError::Windows(e) => format!("windows backend: {e}"),
EmulationCreationError::NoAvailableBackend => "no backend available".to_string(),
};
write!(f, "could not create input emulation backend: {reason}")
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum WlrootsEmulationCreationError {
Connect(#[from] ConnectError),
Global(#[from] GlobalError),
Wayland(#[from] WaylandError),
Bind(#[from] WaylandBindError),
Dispatch(#[from] DispatchError),
Io(#[from] std::io::Error),
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub struct WaylandBindError {
inner: BindError,
protocol: &'static str,
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl WaylandBindError {
pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
Self { inner, protocol }
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for WaylandBindError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} protocol not supported: {}",
self.protocol, self.inner
)
}
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for WlrootsEmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WlrootsEmulationCreationError::Bind(e) => write!(f, "{e}"),
WlrootsEmulationCreationError::Connect(e) => {
write!(f, "could not connect to wayland compositor: {e}")
}
WlrootsEmulationCreationError::Global(e) => write!(f, "wayland error: {e}"),
WlrootsEmulationCreationError::Wayland(e) => write!(f, "wayland error: {e}"),
WlrootsEmulationCreationError::Dispatch(e) => {
write!(f, "error dispatching wayland events: {e}")
}
WlrootsEmulationCreationError::Io(e) => write!(f, "io error: {e}"),
}
}
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum LibeiEmulationCreationError {
Ashpd(#[from] ashpd::Error),
Io(#[from] std::io::Error),
Handshake(#[from] HandshakeError),
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
impl Display for LibeiEmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LibeiEmulationCreationError::Ashpd(e) => write!(f, "xdg-desktop-portal: {e}"),
LibeiEmulationCreationError::Io(e) => write!(f, "io error: {e}"),
LibeiEmulationCreationError::Handshake(e) => write!(f, "error in libei handshake: {e}"),
}
}
}
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum XdpEmulationCreationError {
Ashpd(#[from] ashpd::Error),
}
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
impl Display for XdpEmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
XdpEmulationCreationError::Ashpd(e) => write!(f, "portal error: {e}"),
}
}
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum X11EmulationCreationError {
OpenDisplay,
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
impl Display for X11EmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
X11EmulationCreationError::OpenDisplay => write!(f, "could not open display!"),
}
}
}
#[cfg(target_os = "macos")]
#[derive(Debug, Error)]
pub enum MacOSEmulationCreationError {
EventSourceCreation,
}
#[cfg(target_os = "macos")]
impl Display for MacOSEmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MacOSEmulationCreationError::EventSourceCreation => {
write!(f, "could not create event source")
}
}
}
}
#[cfg(windows)]
#[derive(Debug, Error)]
pub enum WindowsEmulationCreationError {}

141
input-emulation/src/lib.rs Normal file
View File

@@ -0,0 +1,141 @@
use async_trait::async_trait;
use error::EmulationError;
use std::fmt::Display;
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,
) -> Result<(), EmulationError>;
async fn create(&mut self, handle: EmulationHandle);
async fn destroy(&mut self, handle: EmulationHandle);
}
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)
}

View File

@@ -0,0 +1,336 @@
use anyhow::{anyhow, Result};
use futures::StreamExt;
use once_cell::sync::Lazy;
use std::{
collections::HashMap,
io,
os::{fd::OwnedFd, unix::net::UnixStream},
sync::{
atomic::{AtomicU32, Ordering},
Arc, RwLock,
},
time::{SystemTime, UNIX_EPOCH},
};
use tokio::task::JoinHandle;
use ashpd::{
desktop::{
remote_desktop::{DeviceType, RemoteDesktop},
ResponseError,
},
WindowIdentifier,
};
use async_trait::async_trait;
use reis::{
ei::{
self, button::ButtonState, handshake::ContextType, keyboard::KeyState, Button, Keyboard,
Pointer, Scroll,
},
event::{DeviceCapability, DeviceEvent, EiEvent, SeatEvent},
tokio::{ei_handshake, EiConvertEventStream, EiEventStream},
};
use input_event::{Event, KeyboardEvent, PointerEvent};
use crate::error::EmulationError;
use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation};
static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
let mut m = HashMap::new();
m.insert("ei_connection", 1);
m.insert("ei_callback", 1);
m.insert("ei_pingpong", 1);
m.insert("ei_seat", 1);
m.insert("ei_device", 2);
m.insert("ei_pointer", 1);
m.insert("ei_pointer_absolute", 1);
m.insert("ei_scroll", 1);
m.insert("ei_button", 1);
m.insert("ei_keyboard", 1);
m.insert("ei_touchscreen", 1);
m
});
#[derive(Clone, Default)]
struct Devices {
pointer: Arc<RwLock<Option<(ei::Device, ei::Pointer)>>>,
scroll: Arc<RwLock<Option<(ei::Device, ei::Scroll)>>>,
button: Arc<RwLock<Option<(ei::Device, ei::Button)>>>,
keyboard: Arc<RwLock<Option<(ei::Device, ei::Keyboard)>>>,
}
pub struct LibeiEmulation {
context: ei::Context,
devices: Devices,
serial: AtomicU32,
ei_task: JoinHandle<Result<()>>,
}
async fn get_ei_fd() -> Result<OwnedFd, ashpd::Error> {
let proxy = RemoteDesktop::new().await?;
// retry when user presses the cancel button
let (session, _) = loop {
log::debug!("creating session ...");
let session = proxy.create_session().await?;
log::debug!("selecting devices ...");
proxy
.select_devices(&session, DeviceType::Keyboard | DeviceType::Pointer)
.await?;
log::info!("requesting permission for input emulation");
match proxy
.start(&session, &WindowIdentifier::default())
.await?
.response()
{
Ok(d) => break (session, d),
Err(ashpd::Error::Response(ResponseError::Cancelled)) => {
log::warn!("request cancelled!");
continue;
}
e => e?,
};
};
proxy.connect_to_eis(&session).await
}
impl LibeiEmulation {
pub async fn new() -> Result<Self, LibeiEmulationCreationError> {
let eifd = get_ei_fd().await?;
let stream = UnixStream::from(eifd);
stream.set_nonblocking(true)?;
let context = ei::Context::new(stream)?;
context.flush().map_err(|e| io::Error::new(e.kind(), e))?;
let mut events = EiEventStream::new(context.clone())?;
let handshake = ei_handshake(
&mut events,
"de.feschber.LanMouse",
ContextType::Sender,
&INTERFACES,
)
.await?;
let events = EiConvertEventStream::new(events, handshake.serial);
let devices = Devices::default();
let ei_task =
tokio::task::spawn_local(ei_event_handler(events, context.clone(), devices.clone()));
let serial = AtomicU32::new(handshake.serial);
Ok(Self {
serial,
context,
ei_task,
devices,
})
}
}
impl Drop for LibeiEmulation {
fn drop(&mut self) {
self.ei_task.abort();
}
}
#[async_trait]
impl InputEmulation for LibeiEmulation {
async fn consume(
&mut self,
event: Event,
_handle: EmulationHandle,
) -> Result<(), EmulationError> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros() as u64;
match event {
Event::Pointer(p) => match p {
PointerEvent::Motion {
time: _,
relative_x,
relative_y,
} => {
let pointer_device = self.devices.pointer.read().unwrap();
if let Some((d, p)) = pointer_device.as_ref() {
p.motion_relative(relative_x as f32, relative_y as f32);
d.frame(self.serial.load(Ordering::SeqCst), now);
}
}
PointerEvent::Button {
time: _,
button,
state,
} => {
let button_device = self.devices.button.read().unwrap();
if let Some((d, b)) = button_device.as_ref() {
b.button(
button,
match state {
0 => ButtonState::Released,
_ => ButtonState::Press,
},
);
d.frame(self.serial.load(Ordering::SeqCst), now);
}
}
PointerEvent::Axis {
time: _,
axis,
value,
} => {
let scroll_device = self.devices.scroll.read().unwrap();
if let Some((d, s)) = scroll_device.as_ref() {
match axis {
0 => s.scroll(0., value as f32),
_ => s.scroll(value as f32, 0.),
}
d.frame(self.serial.load(Ordering::SeqCst), now);
}
}
PointerEvent::AxisDiscrete120 { axis, value } => {
let scroll_device = self.devices.scroll.read().unwrap();
if let Some((d, s)) = scroll_device.as_ref() {
match axis {
0 => s.scroll_discrete(0, value),
_ => s.scroll_discrete(value, 0),
}
d.frame(self.serial.load(Ordering::SeqCst), now);
}
}
PointerEvent::Frame {} => {}
},
Event::Keyboard(k) => match k {
KeyboardEvent::Key {
time: _,
key,
state,
} => {
let keyboard_device = self.devices.keyboard.read().unwrap();
if let Some((d, k)) = keyboard_device.as_ref() {
k.key(
key,
match state {
0 => KeyState::Released,
_ => KeyState::Press,
},
);
d.frame(self.serial.load(Ordering::SeqCst), now);
}
}
KeyboardEvent::Modifiers { .. } => {}
},
_ => {}
}
self.context
.flush()
.map_err(|e| io::Error::new(e.kind(), e))?;
Ok(())
}
async fn create(&mut self, _: EmulationHandle) {}
async fn destroy(&mut self, _: EmulationHandle) {}
}
async fn ei_event_handler(
mut events: EiConvertEventStream,
context: ei::Context,
devices: Devices,
) -> Result<()> {
loop {
let event = events
.next()
.await
.ok_or(anyhow!("ei stream closed"))?
.map_err(|e| anyhow!("libei error: {e:?}"))?;
const CAPABILITIES: &[DeviceCapability] = &[
DeviceCapability::Pointer,
DeviceCapability::PointerAbsolute,
DeviceCapability::Keyboard,
DeviceCapability::Touch,
DeviceCapability::Scroll,
DeviceCapability::Button,
];
log::debug!("{event:?}");
match event {
EiEvent::Disconnected(e) => {
log::debug!("ei disconnected: {e:?}");
break;
}
EiEvent::SeatAdded(e) => {
e.seat().bind_capabilities(CAPABILITIES);
}
EiEvent::SeatRemoved(e) => {
log::debug!("seat removed: {:?}", e.seat());
}
EiEvent::DeviceAdded(e) => {
let device_type = e.device().device_type();
log::debug!("device added: {device_type:?}");
e.device().device();
let device = e.device();
if let Some(pointer) = e.device().interface::<Pointer>() {
devices
.pointer
.write()
.unwrap()
.replace((device.device().clone(), pointer));
}
if let Some(keyboard) = e.device().interface::<Keyboard>() {
devices
.keyboard
.write()
.unwrap()
.replace((device.device().clone(), keyboard));
}
if let Some(scroll) = e.device().interface::<Scroll>() {
devices
.scroll
.write()
.unwrap()
.replace((device.device().clone(), scroll));
}
if let Some(button) = e.device().interface::<Button>() {
devices
.button
.write()
.unwrap()
.replace((device.device().clone(), button));
}
}
EiEvent::DeviceRemoved(e) => {
log::debug!("device removed: {:?}", e.device().device_type());
}
EiEvent::DevicePaused(e) => {
log::debug!("device paused: {:?}", e.device().device_type());
}
EiEvent::DeviceResumed(e) => {
log::debug!("device resumed: {:?}", e.device().device_type());
e.device().device().start_emulating(0, 0);
}
EiEvent::KeyboardModifiers(e) => {
log::debug!("modifiers: {e:?}");
}
// only for receiver context
// EiEvent::Frame(_) => { },
// EiEvent::DeviceStartEmulating(_) => { },
// EiEvent::DeviceStopEmulating(_) => { },
// EiEvent::PointerMotion(_) => { },
// EiEvent::PointerMotionAbsolute(_) => { },
// EiEvent::Button(_) => { },
// EiEvent::ScrollDelta(_) => { },
// EiEvent::ScrollStop(_) => { },
// EiEvent::ScrollCancel(_) => { },
// EiEvent::ScrollDiscrete(_) => { },
// EiEvent::KeyboardKey(_) => { },
// EiEvent::TouchDown(_) => { },
// EiEvent::TouchUp(_) => { },
// EiEvent::TouchMotion(_) => { },
_ => unreachable!("unexpected ei event"),
}
context.flush()?;
}
Ok(())
}

View File

@@ -1,18 +1,18 @@
use crate::client::{ClientEvent, ClientHandle}; use super::{error::EmulationError, EmulationHandle, InputEmulation};
use crate::emulate::InputEmulation;
use crate::event::{Event, KeyboardEvent, PointerEvent};
use anyhow::{anyhow, Result};
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::{
CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, ScrollEventUnit, CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, ScrollEventUnit,
}; };
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID}; use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use input_event::{Event, KeyboardEvent, PointerEvent};
use keycode::{KeyMap, KeyMapping}; use keycode::{KeyMap, KeyMapping};
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::time::Duration; use std::time::Duration;
use tokio::task::AbortHandle; use tokio::task::AbortHandle;
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);
@@ -53,11 +53,9 @@ impl IndexMut<CGMouseButton> for ButtonState {
unsafe impl Send for MacOSEmulation {} unsafe impl Send for MacOSEmulation {}
impl MacOSEmulation { impl MacOSEmulation {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self, MacOSEmulationCreationError> {
let event_source = match CGEventSource::new(CGEventSourceStateID::CombinedSessionState) { let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
Ok(e) => e, .map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?;
Err(_) => return Err(anyhow!("event source creation failed!")),
};
let button_state = ButtonState { let button_state = ButtonState {
left: false, left: false,
right: false, right: false,
@@ -109,7 +107,11 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8) {
#[async_trait] #[async_trait]
impl InputEmulation for MacOSEmulation { impl InputEmulation for MacOSEmulation {
async fn consume(&mut self, event: Event, _client_handle: ClientHandle) { async fn consume(
&mut self,
event: Event,
_handle: EmulationHandle,
) -> Result<(), EmulationError> {
match event { match event {
Event::Pointer(pointer_event) => match pointer_event { Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion { PointerEvent::Motion {
@@ -131,7 +133,7 @@ impl InputEmulation for MacOSEmulation {
Some(l) => l, Some(l) => l,
None => { None => {
log::warn!("could not get mouse location!"); log::warn!("could not get mouse location!");
return; return Ok(());
} }
}; };
@@ -155,7 +157,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e, Ok(e) => e,
Err(_) => { Err(_) => {
log::warn!("mouse event creation failed!"); log::warn!("mouse event creation failed!");
return; return Ok(());
} }
}; };
event.set_integer_value_field( event.set_integer_value_field(
@@ -174,27 +176,27 @@ impl InputEmulation for MacOSEmulation {
state, state,
} => { } => {
let (event_type, mouse_button) = match (button, state) { let (event_type, mouse_button) = match (button, state) {
(b, 1) if b == crate::event::BTN_LEFT => { (b, 1) if b == input_event::BTN_LEFT => {
(CGEventType::LeftMouseDown, CGMouseButton::Left) (CGEventType::LeftMouseDown, CGMouseButton::Left)
} }
(b, 0) if b == crate::event::BTN_LEFT => { (b, 0) if b == input_event::BTN_LEFT => {
(CGEventType::LeftMouseUp, CGMouseButton::Left) (CGEventType::LeftMouseUp, CGMouseButton::Left)
} }
(b, 1) if b == crate::event::BTN_RIGHT => { (b, 1) if b == input_event::BTN_RIGHT => {
(CGEventType::RightMouseDown, CGMouseButton::Right) (CGEventType::RightMouseDown, CGMouseButton::Right)
} }
(b, 0) if b == crate::event::BTN_RIGHT => { (b, 0) if b == input_event::BTN_RIGHT => {
(CGEventType::RightMouseUp, CGMouseButton::Right) (CGEventType::RightMouseUp, CGMouseButton::Right)
} }
(b, 1) if b == crate::event::BTN_MIDDLE => { (b, 1) if b == input_event::BTN_MIDDLE => {
(CGEventType::OtherMouseDown, CGMouseButton::Center) (CGEventType::OtherMouseDown, CGMouseButton::Center)
} }
(b, 0) if b == crate::event::BTN_MIDDLE => { (b, 0) if b == input_event::BTN_MIDDLE => {
(CGEventType::OtherMouseUp, CGMouseButton::Center) (CGEventType::OtherMouseUp, CGMouseButton::Center)
} }
_ => { _ => {
log::warn!("invalid button event: {button},{state}"); log::warn!("invalid button event: {button},{state}");
return; return Ok(());
} }
}; };
// store button state // store button state
@@ -210,7 +212,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e, Ok(e) => e,
Err(()) => { Err(()) => {
log::warn!("mouse event creation failed!"); log::warn!("mouse event creation failed!");
return; return Ok(());
} }
}; };
event.post(CGEventTapLocation::HID); event.post(CGEventTapLocation::HID);
@@ -226,7 +228,7 @@ impl InputEmulation for MacOSEmulation {
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x) 1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
_ => { _ => {
log::warn!("invalid scroll event: {axis}, {value}"); log::warn!("invalid scroll event: {axis}, {value}");
return; return Ok(());
} }
}; };
let event = match CGEvent::new_scroll_event( let event = match CGEvent::new_scroll_event(
@@ -240,7 +242,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e, Ok(e) => e,
Err(()) => { Err(()) => {
log::warn!("scroll event creation failed!"); log::warn!("scroll event creation failed!");
return; return Ok(());
} }
}; };
event.post(CGEventTapLocation::HID); event.post(CGEventTapLocation::HID);
@@ -251,7 +253,7 @@ impl InputEmulation for MacOSEmulation {
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x) 1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
_ => { _ => {
log::warn!("invalid scroll event: {axis}, {value}"); log::warn!("invalid scroll event: {axis}, {value}");
return; return Ok(());
} }
}; };
let event = match CGEvent::new_scroll_event( let event = match CGEvent::new_scroll_event(
@@ -265,7 +267,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e, Ok(e) => e,
Err(()) => { Err(()) => {
log::warn!("scroll event creation failed!"); log::warn!("scroll event creation failed!");
return; return Ok(());
} }
}; };
event.post(CGEventTapLocation::HID); event.post(CGEventTapLocation::HID);
@@ -282,7 +284,7 @@ impl InputEmulation for MacOSEmulation {
Ok(k) => k.mac as CGKeyCode, Ok(k) => k.mac as CGKeyCode,
Err(_) => { Err(_) => {
log::warn!("unable to map key event"); log::warn!("unable to map key event");
return; return Ok(());
} }
}; };
match state { match state {
@@ -296,9 +298,11 @@ impl InputEmulation for MacOSEmulation {
}, },
_ => (), _ => (),
} }
// FIXME
Ok(())
} }
async fn notify(&mut self, _client_event: ClientEvent) {} async fn create(&mut self, _handle: EmulationHandle) {}
async fn destroy(&mut self) {} async fn destroy(&mut self, _handle: EmulationHandle) {}
} }

View File

@@ -1,9 +1,9 @@
use crate::{ use super::error::{EmulationError, WindowsEmulationCreationError};
emulate::InputEmulation, use input_event::{
event::{KeyboardEvent, PointerEvent}, scancode, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE,
scancode, BTN_RIGHT,
}; };
use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use std::ops::BitOrAssign; use std::ops::BitOrAssign;
use std::time::Duration; use std::time::Duration;
@@ -19,11 +19,7 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{
}; };
use windows::Win32::UI::WindowsAndMessaging::{XBUTTON1, XBUTTON2}; use windows::Win32::UI::WindowsAndMessaging::{XBUTTON1, XBUTTON2};
use crate::event::{BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT}; use super::{EmulationHandle, InputEmulation};
use crate::{
client::{ClientEvent, ClientHandle},
event::Event,
};
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);
@@ -33,14 +29,14 @@ pub struct WindowsEmulation {
} }
impl WindowsEmulation { impl WindowsEmulation {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self, WindowsEmulationCreationError> {
Ok(Self { repeat_task: None }) Ok(Self { repeat_task: None })
} }
} }
#[async_trait] #[async_trait]
impl InputEmulation for WindowsEmulation { impl InputEmulation for WindowsEmulation {
async fn consume(&mut self, event: Event, _: ClientHandle) { 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 {
PointerEvent::Motion { PointerEvent::Motion {
@@ -81,13 +77,13 @@ impl InputEmulation for WindowsEmulation {
}, },
_ => {} _ => {}
} }
// FIXME
Ok(())
} }
async fn notify(&mut self, _: ClientEvent) { async fn create(&mut self, _handle: EmulationHandle) {}
// nothing to do
}
async fn destroy(&mut self) {} async fn destroy(&mut self, _handle: EmulationHandle) {}
} }
impl WindowsEmulation { impl WindowsEmulation {

View File

@@ -1,5 +1,6 @@
use crate::client::{ClientEvent, ClientHandle}; use crate::error::EmulationError;
use crate::emulate::InputEmulation;
use super::{error::WlrootsEmulationCreationError, InputEmulation};
use async_trait::async_trait; use async_trait::async_trait;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
@@ -7,7 +8,6 @@ use std::os::fd::{AsFd, OwnedFd};
use wayland_client::backend::WaylandError; use wayland_client::backend::WaylandError;
use wayland_client::WEnum; use wayland_client::WEnum;
use anyhow::{anyhow, Result};
use wayland_client::protocol::wl_keyboard::{self, WlKeyboard}; use wayland_client::protocol::wl_keyboard::{self, WlKeyboard};
use wayland_client::protocol::wl_pointer::{Axis, ButtonState}; use wayland_client::protocol::wl_pointer::{Axis, ButtonState};
use wayland_client::protocol::wl_seat::WlSeat; use wayland_client::protocol::wl_seat::WlSeat;
@@ -28,11 +28,14 @@ use wayland_client::{
Connection, Dispatch, EventQueue, QueueHandle, Connection, Dispatch, EventQueue, QueueHandle,
}; };
use crate::event::{Event, KeyboardEvent, PointerEvent}; use input_event::{Event, KeyboardEvent, PointerEvent};
use super::error::WaylandBindError;
use super::EmulationHandle;
struct State { struct State {
keymap: Option<(u32, OwnedFd, u32)>, keymap: Option<(u32, OwnedFd, u32)>,
input_for_client: HashMap<ClientHandle, VirtualInput>, input_for_client: HashMap<EmulationHandle, VirtualInput>,
seat: wl_seat::WlSeat, seat: wl_seat::WlSeat,
qh: QueueHandle<Self>, qh: QueueHandle<Self>,
vpm: VpManager, vpm: VpManager,
@@ -47,20 +50,23 @@ pub(crate) struct WlrootsEmulation {
} }
impl WlrootsEmulation { impl WlrootsEmulation {
pub fn new() -> Result<Self> { pub 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();
let seat: wl_seat::WlSeat = match globals.bind(&qh, 7..=8, ()) { let seat: wl_seat::WlSeat = globals
Ok(wl_seat) => wl_seat, .bind(&qh, 7..=8, ())
Err(_) => return Err(anyhow!("wl_seat >= v7 not supported")), .map_err(|e| WaylandBindError::new(e, "wl_seat 7..=8"))?;
};
let vpm: VpManager = globals.bind(&qh, 1..=1, ())?; let vpm: VpManager = globals
let vkm: VkManager = globals.bind(&qh, 1..=1, ())?; .bind(&qh, 1..=1, ())
.map_err(|e| WaylandBindError::new(e, "wlr-virtual-pointer-unstable-v1"))?;
let vkm: VkManager = globals
.bind(&qh, 1..=1, ())
.map_err(|e| WaylandBindError::new(e, "virtual-keyboard-unstable-v1"))?;
let input_for_client: HashMap<ClientHandle, VirtualInput> = HashMap::new(); let input_for_client: HashMap<EmulationHandle, VirtualInput> = HashMap::new();
let mut emulate = WlrootsEmulation { let mut emulate = WlrootsEmulation {
last_flush_failed: false, last_flush_failed: false,
@@ -75,7 +81,7 @@ impl WlrootsEmulation {
queue, queue,
}; };
while emulate.state.keymap.is_none() { while emulate.state.keymap.is_none() {
emulate.queue.blocking_dispatch(&mut emulate.state).unwrap(); emulate.queue.blocking_dispatch(&mut emulate.state)?;
} }
// let fd = unsafe { &File::from_raw_fd(emulate.state.keymap.unwrap().1.as_raw_fd()) }; // let fd = unsafe { &File::from_raw_fd(emulate.state.keymap.unwrap().1.as_raw_fd()) };
// let mmap = unsafe { MmapOptions::new().map_copy(fd).unwrap() }; // let mmap = unsafe { MmapOptions::new().map_copy(fd).unwrap() };
@@ -85,7 +91,7 @@ impl WlrootsEmulation {
} }
impl State { impl State {
fn add_client(&mut self, client: ClientHandle) { fn add_client(&mut self, client: EmulationHandle) {
let pointer: Vp = self.vpm.create_virtual_pointer(None, &self.qh, ()); let pointer: Vp = self.vpm.create_virtual_pointer(None, &self.qh, ());
let keyboard: Vk = self.vkm.create_virtual_keyboard(&self.seat, &self.qh, ()); let keyboard: Vk = self.vkm.create_virtual_keyboard(&self.seat, &self.qh, ());
@@ -100,56 +106,65 @@ impl State {
self.input_for_client.insert(client, vinput); 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] #[async_trait]
impl InputEmulation for WlrootsEmulation { impl InputEmulation for WlrootsEmulation {
async fn consume(&mut self, event: Event, client_handle: ClientHandle) { async fn consume(
if let Some(virtual_input) = self.state.input_for_client.get(&client_handle) { &mut self,
event: Event,
handle: EmulationHandle,
) -> Result<(), EmulationError> {
if let Some(virtual_input) = self.state.input_for_client.get(&handle) {
if self.last_flush_failed { if self.last_flush_failed {
if let Err(WaylandError::Io(e)) = self.queue.flush() { match self.queue.flush() {
if e.kind() == io::ErrorKind::WouldBlock { Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
/* /*
* outgoing buffer is full - sending more events * outgoing buffer is full - sending more events
* will overwhelm the output buffer and leave the * will overwhelm the output buffer and leave the
* wayland connection in a broken state * wayland connection in a broken state
*/ */
log::warn!( log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
"can't keep up, discarding event: ({client_handle}) - {event:?}" return Ok(());
);
return;
} }
_ => {}
} }
} }
virtual_input.consume_event(event).unwrap(); virtual_input
.consume_event(event)
.unwrap_or_else(|_| panic!("failed to convert event: {event:?}"));
match self.queue.flush() { match self.queue.flush() {
Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => { Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
self.last_flush_failed = true; self.last_flush_failed = true;
log::warn!("can't keep up, retrying ..."); log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
}
Err(WaylandError::Io(e)) => {
log::error!("{e}")
}
Err(WaylandError::Protocol(e)) => {
panic!("wayland protocol violation: {e}")
}
Ok(()) => {
self.last_flush_failed = false;
} }
Err(WaylandError::Protocol(e)) => panic!("wayland protocol violation: {e}"),
Ok(()) => self.last_flush_failed = false,
Err(e) => Err(e)?,
} }
} }
Ok(())
} }
async fn notify(&mut self, client_event: ClientEvent) { async fn create(&mut self, handle: EmulationHandle) {
if let ClientEvent::Create(client, _) = client_event { self.state.add_client(handle);
self.state.add_client(client); if let Err(e) = self.queue.flush() {
if let Err(e) = self.queue.flush() { log::error!("{}", e);
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 destroy(&mut self) {}
} }
struct VirtualInput { struct VirtualInput {

View File

@@ -1,4 +1,3 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait; use async_trait::async_trait;
use std::ptr; use std::ptr;
use x11::{ use x11::{
@@ -6,14 +5,14 @@ use x11::{
xtest, xtest,
}; };
use crate::{ use input_event::{
client::ClientHandle, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
emulate::InputEmulation,
event::{
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
},
}; };
use crate::error::EmulationError;
use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation};
pub struct X11Emulation { pub struct X11Emulation {
display: *mut xlib::Display, display: *mut xlib::Display,
} }
@@ -21,11 +20,11 @@ pub struct X11Emulation {
unsafe impl Send for X11Emulation {} unsafe impl Send for X11Emulation {}
impl X11Emulation { impl X11Emulation {
pub fn new() -> Result<Self> { pub 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 => {
Err(anyhow!("could not open display")) Err(X11EmulationCreationError::OpenDisplay)
} }
display => Ok(display), display => Ok(display),
} }
@@ -101,7 +100,7 @@ impl Drop for X11Emulation {
#[async_trait] #[async_trait]
impl InputEmulation for X11Emulation { impl InputEmulation for X11Emulation {
async fn consume(&mut self, event: Event, _: ClientHandle) { 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 {
PointerEvent::Motion { PointerEvent::Motion {
@@ -142,11 +141,15 @@ impl InputEmulation for X11Emulation {
unsafe { unsafe {
xlib::XFlush(self.display); xlib::XFlush(self.display);
} }
// FIXME
Ok(())
} }
async fn notify(&mut self, _: crate::client::ClientEvent) { async fn create(&mut self, _: EmulationHandle) {
// for our purposes it does not matter what client sent the event // for our purposes it does not matter what client sent the event
} }
async fn destroy(&mut self) {} async fn destroy(&mut self, _: EmulationHandle) {
// for our purposes it does not matter what client sent the event
}
} }

View File

@@ -4,26 +4,28 @@ use ashpd::{
remote_desktop::{Axis, DeviceType, KeyState, RemoteDesktop}, remote_desktop::{Axis, DeviceType, KeyState, RemoteDesktop},
ResponseError, Session, ResponseError, Session,
}, },
zbus::AsyncDrop,
WindowIdentifier, WindowIdentifier,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use crate::{ use futures::FutureExt;
client::ClientEvent, use input_event::{
emulate::InputEmulation, Event::{Keyboard, Pointer},
event::{ KeyboardEvent, PointerEvent,
Event::{Keyboard, Pointer},
KeyboardEvent, PointerEvent,
},
}; };
use crate::error::EmulationError;
use super::{error::XdpEmulationCreationError, EmulationHandle, InputEmulation};
pub struct DesktopPortalEmulation<'a> { pub struct DesktopPortalEmulation<'a> {
proxy: RemoteDesktop<'a>, proxy: RemoteDesktop<'a>,
session: Session<'a>, session: Session<'a>,
} }
impl<'a> DesktopPortalEmulation<'a> { impl<'a> DesktopPortalEmulation<'a> {
pub async fn new() -> Result<DesktopPortalEmulation<'a>> { pub 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?;
@@ -53,6 +55,7 @@ impl<'a> DesktopPortalEmulation<'a> {
}; };
log::debug!("started session"); log::debug!("started session");
let session = session;
Ok(Self { proxy, session }) Ok(Self { proxy, session })
} }
@@ -60,7 +63,11 @@ impl<'a> DesktopPortalEmulation<'a> {
#[async_trait] #[async_trait]
impl<'a> InputEmulation for DesktopPortalEmulation<'a> { impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
async fn consume(&mut self, event: crate::event::Event, _client: crate::client::ClientHandle) { async fn consume(
&mut self,
event: input_event::Event,
_client: EmulationHandle,
) -> Result<(), EmulationError> {
match event { match event {
Pointer(p) => match p { Pointer(p) => match p {
PointerEvent::Motion { PointerEvent::Motion {
@@ -68,13 +75,9 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
relative_x, relative_x,
relative_y, relative_y,
} => { } => {
if let Err(e) = self self.proxy
.proxy
.notify_pointer_motion(&self.session, relative_x, relative_y) .notify_pointer_motion(&self.session, relative_x, relative_y)
.await .await?;
{
log::warn!("{e}");
}
} }
PointerEvent::Button { PointerEvent::Button {
time: _, time: _,
@@ -85,26 +88,18 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
0 => KeyState::Released, 0 => KeyState::Released,
_ => KeyState::Pressed, _ => KeyState::Pressed,
}; };
if let Err(e) = self self.proxy
.proxy
.notify_pointer_button(&self.session, button as i32, state) .notify_pointer_button(&self.session, button as i32, state)
.await .await?;
{
log::warn!("{e}");
}
} }
PointerEvent::AxisDiscrete120 { axis, value } => { PointerEvent::AxisDiscrete120 { axis, value } => {
let axis = match axis { let axis = match axis {
0 => Axis::Vertical, 0 => Axis::Vertical,
_ => Axis::Horizontal, _ => Axis::Horizontal,
}; };
if let Err(e) = self self.proxy
.proxy
.notify_pointer_axis_discrete(&self.session, axis, value) .notify_pointer_axis_discrete(&self.session, axis, value)
.await .await?;
{
log::warn!("{e}");
}
} }
PointerEvent::Axis { PointerEvent::Axis {
time: _, time: _,
@@ -119,13 +114,9 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
Axis::Vertical => (0., value), Axis::Vertical => (0., value),
Axis::Horizontal => (value, 0.), Axis::Horizontal => (value, 0.),
}; };
if let Err(e) = self self.proxy
.proxy
.notify_pointer_axis(&self.session, dx, dy, true) .notify_pointer_axis(&self.session, dx, dy, true)
.await .await?;
{
log::warn!("{e}");
}
} }
PointerEvent::Frame {} => {} PointerEvent::Frame {} => {}
}, },
@@ -140,13 +131,9 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
0 => KeyState::Released, 0 => KeyState::Released,
_ => KeyState::Pressed, _ => KeyState::Pressed,
}; };
if let Err(e) = self self.proxy
.proxy
.notify_keyboard_keycode(&self.session, key as i32, state) .notify_keyboard_keycode(&self.session, key as i32, state)
.await .await?;
{
log::warn!("{e}");
}
} }
KeyboardEvent::Modifiers { .. } => { KeyboardEvent::Modifiers { .. } => {
// ignore // ignore
@@ -155,14 +142,28 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
} }
_ => {} _ => {}
} }
Ok(())
} }
async fn notify(&mut self, _client: ClientEvent) {} async fn create(&mut self, _client: EmulationHandle) {}
async fn destroy(&mut self, _client: EmulationHandle) {}
}
async fn destroy(&mut self) { impl<'a> AsyncDrop for DesktopPortalEmulation<'a> {
log::debug!("closing remote desktop session"); #[doc = r" Perform the async cleanup."]
if let Err(e) = self.session.close().await { #[must_use]
log::error!("failed to close remote desktop session: {e}"); #[allow(clippy::type_complexity, clippy::type_repetition_in_bounds)]
fn async_drop<'async_trait>(
self,
) -> ::core::pin::Pin<
Box<dyn ::core::future::Future<Output = ()> + ::core::marker::Send + 'async_trait>,
>
where
Self: 'async_trait,
{
async move {
let _ = self.session.close().await;
} }
.boxed()
} }
} }

14
input-event/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[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"] }

View File

@@ -1,10 +1,11 @@
use crate::scancode;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::{ use std::{
error::Error, error::Error,
fmt::{self, Display}, fmt::{self, Display},
}; };
pub mod scancode;
// FIXME // FIXME
pub const BTN_LEFT: u32 = 0x110; pub const BTN_LEFT: u32 = 0x110;
pub const BTN_RIGHT: u32 = 0x111; pub const BTN_RIGHT: u32 = 0x111;

View File

@@ -2,10 +2,14 @@
rustPlatform, rustPlatform,
lib, lib,
pkgs, pkgs,
}: }: let
cargoToml = builtins.fromTOML (builtins.readFile ../Cargo.toml);
pname = cargoToml.package.name;
version = cargoToml.package.version;
in
rustPlatform.buildRustPackage { rustPlatform.buildRustPackage {
pname = "lan-mouse"; pname = pname;
version = "0.7.0"; version = version;
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
pkg-config pkg-config
@@ -23,7 +27,7 @@ rustPlatform.buildRustPackage {
]; ];
src = builtins.path { src = builtins.path {
name = "lan-mouse"; name = pname;
path = lib.cleanSource ../.; path = lib.cleanSource ../.;
}; };
@@ -38,7 +42,7 @@ rustPlatform.buildRustPackage {
Lan Mouse is a mouse and keyboard sharing software similar to universal-control on Apple devices. It allows for using multiple pcs with a single set of mouse and keyboard. This is also known as a Software KVM switch. Lan Mouse is a mouse and keyboard sharing software similar to universal-control on Apple devices. It allows for using multiple pcs with a single set of mouse and keyboard. This is also known as a Software KVM switch.
The primary target is Wayland on Linux but Windows and MacOS and Linux on Xorg have partial support as well (see below for more details). The primary target is Wayland on Linux but Windows and MacOS and Linux on Xorg have partial support as well (see below for more details).
''; '';
mainProgram = "lan-mouse"; mainProgram = pname;
platforms = platforms.all; platforms = platforms.all;
}; };
} }

View File

@@ -1,78 +0,0 @@
use std::io;
use futures_core::Stream;
use crate::{
client::{ClientEvent, ClientHandle},
event::Event,
};
#[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() -> Box<dyn InputCapture<Item = io::Result<(ClientHandle, Event)>>> {
#[cfg(target_os = "macos")]
match macos::MacOSInputCapture::new() {
Ok(p) => return Box::new(p),
Err(e) => log::info!("macos input capture not available: {e}"),
}
#[cfg(windows)]
match windows::WindowsInputCapture::new() {
Ok(p) => return Box::new(p),
Err(e) => log::info!("windows input capture not available: {e}"),
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
match libei::LibeiInputCapture::new().await {
Ok(p) => {
log::info!("using libei input capture");
return Box::new(p);
}
Err(e) => log::info!("libei input capture not available: {e}"),
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
match wayland::WaylandInputCapture::new() {
Ok(p) => {
log::info!("using layer-shell input capture");
return Box::new(p);
}
Err(e) => log::info!("layer_shell input capture not available: {e}"),
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
match x11::X11InputCapture::new() {
Ok(p) => {
log::info!("using x11 input capture");
return Box::new(p);
}
Err(e) => log::info!("x11 input capture not available: {e}"),
}
log::error!("falling back to dummy input capture");
Box::new(dummy::DummyInputCapture::new())
}
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<()>;
}

View File

@@ -1,33 +0,0 @@
use crate::capture::InputCapture;
use crate::client::{ClientEvent, ClientHandle};
use crate::event::Event;
use anyhow::{anyhow, Result};
use futures_core::Stream;
use std::task::{Context, Poll};
use std::{io, pin::Pin};
pub struct MacOSInputCapture;
impl MacOSInputCapture {
pub fn new() -> Result<Self> {
Err(anyhow!("not yet implemented"))
}
}
impl Stream for MacOSInputCapture {
type Item = io::Result<(ClientHandle, Event)>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Pending
}
}
impl InputCapture for MacOSInputCapture {
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> {
Ok(())
}
fn release(&mut self) -> io::Result<()> {
Ok(())
}
}

View File

@@ -1,39 +0,0 @@
use anyhow::{anyhow, Result};
use std::io;
use std::task::Poll;
use futures_core::Stream;
use crate::capture::InputCapture;
use crate::event::Event;
use crate::client::{ClientEvent, ClientHandle};
pub struct X11InputCapture {}
impl X11InputCapture {
pub fn new() -> Result<Self> {
Err(anyhow!("not implemented"))
}
}
impl InputCapture for X11InputCapture {
fn notify(&mut self, _event: ClientEvent) -> io::Result<()> {
Ok(())
}
fn release(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Stream for X11InputCapture {
type Item = io::Result<(ClientHandle, Event)>;
fn poll_next(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
Poll::Pending
}
}

View File

@@ -1,8 +1,8 @@
use crate::capture; use crate::config::Config;
use crate::client::{ClientEvent, Position};
use crate::event::{Event, KeyboardEvent};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use futures::StreamExt; use futures::StreamExt;
use input_capture::{self, Position};
use input_event::{Event, KeyboardEvent};
use tokio::task::LocalSet; use tokio::task::LocalSet;
pub fn run() -> Result<()> { pub fn run() -> Result<()> {
@@ -12,17 +12,20 @@ pub fn run() -> Result<()> {
.enable_time() .enable_time()
.build()?; .build()?;
runtime.block_on(LocalSet::new().run_until(input_capture_test())) let config = Config::new()?;
runtime.block_on(LocalSet::new().run_until(input_capture_test(config)))
} }
async fn input_capture_test() -> Result<()> { async fn input_capture_test(config: Config) -> Result<()> {
log::info!("creating input capture"); log::info!("creating input capture");
let mut input_capture = capture::create().await; let backend = config.capture_backend.map(|b| b.into());
let mut input_capture = input_capture::create(backend).await?;
log::info!("creating clients"); log::info!("creating clients");
input_capture.notify(ClientEvent::Create(0, Position::Left))?; input_capture.create(0, Position::Left)?;
input_capture.notify(ClientEvent::Create(1, Position::Right))?; input_capture.create(1, Position::Right)?;
input_capture.notify(ClientEvent::Create(2, Position::Top))?; input_capture.create(2, Position::Top)?;
input_capture.notify(ClientEvent::Create(3, Position::Bottom))?; input_capture.create(3, Position::Bottom)?;
loop { loop {
let (client, event) = input_capture let (client, event) = input_capture
.next() .next()

View File

@@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
use slab::Slab; use slab::Slab;
use crate::config::DEFAULT_PORT; use crate::config::DEFAULT_PORT;
use input_capture;
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy, Serialize, Deserialize)]
pub enum Position { pub enum Position {
@@ -25,6 +26,17 @@ 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)] #[derive(Debug)]
pub struct PositionParseError { pub struct PositionParseError {
string: String, string: String,
@@ -52,17 +64,6 @@ 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 { impl Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
@@ -118,12 +119,6 @@ impl Default for ClientConfig {
} }
} }
#[derive(Clone, Copy, Debug)]
pub enum ClientEvent {
Create(ClientHandle, Position),
Destroy(ClientHandle),
}
pub type ClientHandle = u64; pub type ClientHandle = u64;
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]

View File

@@ -1,22 +1,28 @@
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
use std::fmt::Display;
use std::net::IpAddr; use std::net::IpAddr;
use std::{error::Error, fs}; use std::{error::Error, fs};
use toml; use toml;
use crate::client::Position; use crate::client::Position;
use crate::scancode;
use crate::scancode::Linux::{KeyLeftAlt, KeyLeftCtrl, KeyLeftMeta, KeyLeftShift}; use input_event::scancode::{
self,
Linux::{KeyLeftAlt, KeyLeftCtrl, KeyLeftMeta, KeyLeftShift},
};
pub const DEFAULT_PORT: u16 = 4242; pub const DEFAULT_PORT: u16 = 4242;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ConfigToml { pub struct ConfigToml {
pub capture_backend: Option<CaptureBackend>,
pub emulation_backend: Option<EmulationBackend>,
pub port: Option<u16>, pub port: Option<u16>,
pub frontend: Option<String>, pub frontend: Option<Frontend>,
pub release_bind: Option<Vec<scancode::Linux>>, pub release_bind: Option<Vec<scancode::Linux>>,
pub left: Option<TomlClient>, pub left: Option<TomlClient>,
pub right: Option<TomlClient>, pub right: Option<TomlClient>,
@@ -26,6 +32,7 @@ pub struct ConfigToml {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct TomlClient { pub struct TomlClient {
pub capture_backend: Option<CaptureBackend>,
pub hostname: Option<String>, pub hostname: Option<String>,
pub host_name: Option<String>, pub host_name: Option<String>,
pub ips: Option<Vec<IpAddr>>, pub ips: Option<Vec<IpAddr>>,
@@ -51,7 +58,7 @@ struct CliArgs {
/// the frontend to use [cli | gtk] /// the frontend to use [cli | gtk]
#[arg(short, long)] #[arg(short, long)]
frontend: Option<String>, frontend: Option<Frontend>,
/// non-default config file location /// non-default config file location
#[arg(short, long)] #[arg(short, long)]
@@ -68,16 +75,144 @@ struct CliArgs {
/// test input emulation /// test input emulation
#[arg(long)] #[arg(long)]
test_emulation: bool, test_emulation: bool,
/// capture backend override
#[arg(long)]
capture_backend: Option<CaptureBackend>,
/// emulation backend override
#[arg(long)]
emulation_backend: Option<EmulationBackend>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)]
pub enum CaptureBackend {
#[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 CaptureBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
CaptureBackend::InputCapturePortal => write!(f, "input-capture-portal"),
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
CaptureBackend::LayerShell => write!(f, "layer-shell"),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
CaptureBackend::X11 => write!(f, "X11"),
#[cfg(windows)]
CaptureBackend::Windows => write!(f, "windows"),
#[cfg(target_os = "macos")]
CaptureBackend::MacOs => write!(f, "MacOS"),
CaptureBackend::Dummy => write!(f, "dummy"),
}
}
}
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")))]
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 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 {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
EmulationBackend::Wlroots => write!(f, "wlroots"),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
EmulationBackend::Libei => write!(f, "libei"),
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
EmulationBackend::Xdp => write!(f, "xdg-desktop-portal"),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
EmulationBackend::X11 => write!(f, "X11"),
#[cfg(windows)]
EmulationBackend::Windows => write!(f, "windows"),
#[cfg(target_os = "macos")]
EmulationBackend::MacOs => write!(f, "macos"),
EmulationBackend::Dummy => write!(f, "dummy"),
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, ValueEnum)]
pub enum Frontend { pub enum Frontend {
Gtk, Gtk,
Cli, Cli,
} }
impl Default for Frontend {
fn default() -> Self {
if cfg!(feature = "gtk") {
Self::Gtk
} else {
Self::Cli
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Config {
pub capture_backend: Option<CaptureBackend>,
pub emulation_backend: Option<EmulationBackend>,
pub frontend: Frontend, pub frontend: Frontend,
pub port: u16, pub port: u16,
pub clients: Vec<(TomlClient, Position)>, pub clients: Vec<(TomlClient, Position)>,
@@ -129,33 +264,14 @@ impl Config {
Ok(c) => Some(c), Ok(c) => Some(c),
}; };
let frontend = match args.frontend { let frontend_arg = args.frontend;
None => match &config_toml { let frontend_cfg = config_toml.as_ref().and_then(|c| c.frontend);
Some(c) => c.frontend.clone(), let frontend = frontend_arg.or(frontend_cfg).unwrap_or_default();
None => None,
},
frontend => frontend,
};
let frontend = match frontend { let port = args
#[cfg(feature = "gtk")] .port
None => Frontend::Gtk, .or(config_toml.as_ref().and_then(|c| c.port))
#[cfg(not(feature = "gtk"))] .unwrap_or(DEFAULT_PORT);
None => Frontend::Cli,
Some(s) => match s.as_str() {
"cli" => Frontend::Cli,
"gtk" => Frontend::Gtk,
_ => Frontend::Cli,
},
};
let port = match args.port {
Some(port) => port,
None => match &config_toml {
Some(c) => c.port.unwrap_or(DEFAULT_PORT),
None => DEFAULT_PORT,
},
};
log::debug!("{config_toml:?}"); log::debug!("{config_toml:?}");
let release_bind = config_toml let release_bind = config_toml
@@ -163,6 +279,14 @@ impl Config {
.and_then(|c| c.release_bind.clone()) .and_then(|c| c.release_bind.clone())
.unwrap_or(Vec::from_iter(DEFAULT_RELEASE_KEYS.iter().cloned())); .unwrap_or(Vec::from_iter(DEFAULT_RELEASE_KEYS.iter().cloned()));
let capture_backend = args
.capture_backend
.or(config_toml.as_ref().and_then(|c| c.capture_backend));
let emulation_backend = args
.emulation_backend
.or(config_toml.as_ref().and_then(|c| c.emulation_backend));
let mut clients: Vec<(TomlClient, Position)> = vec![]; let mut clients: Vec<(TomlClient, Position)> = vec![];
if let Some(config_toml) = config_toml { if let Some(config_toml) = config_toml {
@@ -185,6 +309,8 @@ impl Config {
let test_emulation = args.test_emulation; let test_emulation = args.test_emulation;
Ok(Config { Ok(Config {
capture_backend,
emulation_backend,
daemon, daemon,
frontend, frontend,
clients, clients,

View File

@@ -1,98 +0,0 @@
use async_trait::async_trait;
use std::future;
use crate::{
client::{ClientEvent, ClientHandle},
event::Event,
};
use anyhow::Result;
#[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;
#[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() -> Box<dyn InputEmulation> {
#[cfg(windows)]
match windows::WindowsEmulation::new() {
Ok(c) => return Box::new(c),
Err(e) => log::warn!("windows input emulation unavailable: {e}"),
}
#[cfg(target_os = "macos")]
match macos::MacOSEmulation::new() {
Ok(c) => {
log::info!("using macos input emulation");
return Box::new(c);
}
Err(e) => log::error!("macos input emulatino not available: {e}"),
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
match wlroots::WlrootsEmulation::new() {
Ok(c) => {
log::info!("using wlroots input emulation");
return Box::new(c);
}
Err(e) => log::info!("wayland backend not available: {e}"),
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
match libei::LibeiEmulation::new().await {
Ok(c) => {
log::info!("using libei input emulation");
return Box::new(c);
}
Err(e) => log::info!("libei not available: {e}"),
}
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
match xdg_desktop_portal::DesktopPortalEmulation::new().await {
Ok(c) => {
log::info!("using xdg-remote-desktop-portal input emulation");
return Box::new(c);
}
Err(e) => log::info!("remote desktop portal not available: {e}"),
}
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
match x11::X11Emulation::new() {
Ok(c) => {
log::info!("using x11 input emulation");
return Box::new(c);
}
Err(e) => log::info!("x11 input emulation not available: {e}"),
}
log::error!("falling back to dummy input emulation");
Box::new(dummy::DummyEmulation::new())
}

View File

@@ -1,26 +0,0 @@
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) {}
}

View File

@@ -1,399 +0,0 @@
use std::{
collections::HashMap,
os::{fd::OwnedFd, unix::net::UnixStream},
time::{SystemTime, UNIX_EPOCH},
};
use anyhow::{anyhow, Result};
use ashpd::{
desktop::{
remote_desktop::{DeviceType, RemoteDesktop},
ResponseError,
},
WindowIdentifier,
};
use async_trait::async_trait;
use futures::StreamExt;
use reis::{
ei::{self, button::ButtonState, handshake::ContextType, keyboard::KeyState},
tokio::EiEventStream,
PendingRequestResult,
};
use crate::{
client::{ClientEvent, ClientHandle},
emulate::InputEmulation,
event::Event,
};
pub struct LibeiEmulation {
handshake: bool,
context: ei::Context,
events: EiEventStream,
pointer: Option<(ei::Device, ei::Pointer)>,
has_pointer: bool,
scroll: Option<(ei::Device, ei::Scroll)>,
has_scroll: bool,
button: Option<(ei::Device, ei::Button)>,
has_button: bool,
keyboard: Option<(ei::Device, ei::Keyboard)>,
has_keyboard: bool,
capabilities: HashMap<String, u64>,
capability_mask: u64,
sequence: u32,
serial: u32,
}
async fn get_ei_fd() -> Result<OwnedFd, ashpd::Error> {
let proxy = RemoteDesktop::new().await?;
// retry when user presses the cancel button
let (session, _) = loop {
log::debug!("creating session ...");
let session = proxy.create_session().await?;
log::debug!("selecting devices ...");
proxy
.select_devices(&session, DeviceType::Keyboard | DeviceType::Pointer)
.await?;
log::info!("requesting permission for input emulation");
match proxy
.start(&session, &WindowIdentifier::default())
.await?
.response()
{
Ok(d) => break (session, d),
Err(ashpd::Error::Response(ResponseError::Cancelled)) => {
log::warn!("request cancelled!");
continue;
}
e => e?,
};
};
proxy.connect_to_eis(&session).await
}
impl LibeiEmulation {
pub async fn new() -> Result<Self> {
// fd is owned by the message, so we need to dup it
let eifd = get_ei_fd().await?;
let stream = UnixStream::from(eifd);
// let stream = UnixStream::connect("/run/user/1000/eis-0")?;
stream.set_nonblocking(true)?;
let context = ei::Context::new(stream)?;
context.flush()?;
let events = EiEventStream::new(context.clone())?;
Ok(Self {
handshake: false,
context,
events,
pointer: None,
button: None,
scroll: None,
keyboard: None,
has_pointer: false,
has_button: false,
has_scroll: false,
has_keyboard: false,
capabilities: HashMap::new(),
capability_mask: 0,
sequence: 0,
serial: 0,
})
}
}
#[async_trait]
impl InputEmulation for LibeiEmulation {
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 {
crate::event::PointerEvent::Motion {
time: _,
relative_x,
relative_y,
} => {
if !self.has_pointer {
return;
}
if let Some((d, p)) = self.pointer.as_mut() {
p.motion_relative(relative_x as f32, relative_y as f32);
d.frame(self.serial, now);
}
}
crate::event::PointerEvent::Button {
time: _,
button,
state,
} => {
if !self.has_button {
return;
}
if let Some((d, b)) = self.button.as_mut() {
b.button(
button,
match state {
0 => ButtonState::Released,
_ => ButtonState::Press,
},
);
d.frame(self.serial, now);
}
}
crate::event::PointerEvent::Axis {
time: _,
axis,
value,
} => {
if !self.has_scroll {
return;
}
if let Some((d, s)) = self.scroll.as_mut() {
match axis {
0 => s.scroll(0., value as f32),
_ => s.scroll(value as f32, 0.),
}
d.frame(self.serial, now);
}
}
crate::event::PointerEvent::AxisDiscrete120 { axis, value } => {
if !self.has_scroll {
return;
}
if let Some((d, s)) = self.scroll.as_mut() {
match axis {
0 => s.scroll_discrete(0, value),
_ => s.scroll_discrete(value, 0),
}
d.frame(self.serial, now);
}
}
crate::event::PointerEvent::Frame {} => {}
},
Event::Keyboard(k) => match k {
crate::event::KeyboardEvent::Key {
time: _,
key,
state,
} => {
if !self.has_keyboard {
return;
}
if let Some((d, k)) = &mut self.keyboard {
k.key(
key,
match state {
0 => KeyState::Released,
_ => KeyState::Press,
},
);
d.frame(self.serial, now);
}
}
crate::event::KeyboardEvent::Modifiers { .. } => {}
},
_ => {}
}
self.context.flush().unwrap();
}
async fn dispatch(&mut self) -> Result<()> {
let event = match self.events.next().await {
Some(e) => e?,
None => return Err(anyhow!("libei connection lost")),
};
let event = match event {
PendingRequestResult::Request(result) => result,
PendingRequestResult::ParseError(e) => {
return Err(anyhow!("libei protocol violation: {e}"))
}
PendingRequestResult::InvalidObject(e) => return Err(anyhow!("invalid object {e}")),
};
match event {
ei::Event::Handshake(handshake, request) => match request {
ei::handshake::Event::HandshakeVersion { version } => {
if self.handshake {
return Ok(());
}
log::info!("libei version {}", version);
// sender means we are sending events _to_ the eis server
handshake.handshake_version(version); // FIXME
handshake.context_type(ContextType::Sender);
handshake.name("ei-demo-client");
handshake.interface_version("ei_connection", 1);
handshake.interface_version("ei_callback", 1);
handshake.interface_version("ei_pingpong", 1);
handshake.interface_version("ei_seat", 1);
handshake.interface_version("ei_device", 2);
handshake.interface_version("ei_pointer", 1);
handshake.interface_version("ei_pointer_absolute", 1);
handshake.interface_version("ei_scroll", 1);
handshake.interface_version("ei_button", 1);
handshake.interface_version("ei_keyboard", 1);
handshake.interface_version("ei_touchscreen", 1);
handshake.finish();
self.handshake = true;
}
ei::handshake::Event::InterfaceVersion { name, version } => {
log::debug!("handshake: Interface {name} @ {version}");
}
ei::handshake::Event::Connection { serial, connection } => {
connection.sync(1);
self.serial = serial;
}
_ => unreachable!(),
},
ei::Event::Connection(_connection, request) => match request {
ei::connection::Event::Seat { seat } => {
log::debug!("connected to seat: {seat:?}");
}
ei::connection::Event::Ping { ping } => {
ping.done(0);
}
ei::connection::Event::Disconnected {
last_serial: _,
reason,
explanation,
} => {
log::debug!("ei - disconnected: reason: {reason:?}: {explanation}")
}
ei::connection::Event::InvalidObject {
last_serial,
invalid_id,
} => {
return Err(anyhow!(
"invalid object: id: {invalid_id}, serial: {last_serial}"
));
}
_ => unreachable!(),
},
ei::Event::Device(device, request) => match request {
ei::device::Event::Destroyed { serial } => {
log::debug!("device destroyed: {device:?} - serial: {serial}")
}
ei::device::Event::Name { name } => {
log::debug!("device name: {name}")
}
ei::device::Event::DeviceType { device_type } => {
log::debug!("device type: {device_type:?}")
}
ei::device::Event::Dimensions { width, height } => {
log::debug!("device dimensions: {width}x{height}")
}
ei::device::Event::Region {
offset_x,
offset_y,
width,
hight,
scale,
} => log::debug!(
"device region: {width}x{hight} @ ({offset_x},{offset_y}), scale: {scale}"
),
ei::device::Event::Interface { object } => {
log::debug!("device interface: {object:?}");
if object.interface().eq("ei_pointer") {
log::debug!("GOT POINTER DEVICE");
self.pointer.replace((device, object.downcast().unwrap()));
} else if object.interface().eq("ei_button") {
log::debug!("GOT BUTTON DEVICE");
self.button.replace((device, object.downcast().unwrap()));
} else if object.interface().eq("ei_scroll") {
log::debug!("GOT SCROLL DEVICE");
self.scroll.replace((device, object.downcast().unwrap()));
} else if object.interface().eq("ei_keyboard") {
log::debug!("GOT KEYBOARD DEVICE");
self.keyboard.replace((device, object.downcast().unwrap()));
}
}
ei::device::Event::Done => {
log::debug!("device: done {device:?}");
}
ei::device::Event::Resumed { serial } => {
self.serial = serial;
device.start_emulating(serial, self.sequence);
self.sequence += 1;
log::debug!("resumed: {device:?}");
if let Some((d, _)) = &mut self.pointer {
if d == &device {
log::debug!("pointer resumed {serial}");
self.has_pointer = true;
}
}
if let Some((d, _)) = &mut self.button {
if d == &device {
log::debug!("button resumed {serial}");
self.has_button = true;
}
}
if let Some((d, _)) = &mut self.scroll {
if d == &device {
log::debug!("scroll resumed {serial}");
self.has_scroll = true;
}
}
if let Some((d, _)) = &mut self.keyboard {
if d == &device {
log::debug!("keyboard resumed {serial}");
self.has_keyboard = true;
}
}
}
ei::device::Event::Paused { serial } => {
self.has_pointer = false;
self.has_button = false;
self.serial = serial;
}
ei::device::Event::StartEmulating { serial, sequence } => {
log::debug!("start emulating {serial}, {sequence}")
}
ei::device::Event::StopEmulating { serial } => {
log::debug!("stop emulating {serial}")
}
ei::device::Event::Frame { serial, timestamp } => {
log::debug!("frame: {serial}, {timestamp}");
}
ei::device::Event::RegionMappingId { mapping_id } => {
log::debug!("RegionMappingId {mapping_id}")
}
e => log::debug!("invalid event: {e:?}"),
},
ei::Event::Seat(seat, request) => match request {
ei::seat::Event::Destroyed { serial } => {
self.serial = serial;
log::debug!("seat destroyed: {seat:?}");
}
ei::seat::Event::Name { name } => {
log::debug!("seat name: {name}");
}
ei::seat::Event::Capability { mask, interface } => {
log::debug!("seat capabilities: {mask}, interface: {interface:?}");
self.capabilities.insert(interface, mask);
self.capability_mask |= mask;
}
ei::seat::Event::Done => {
log::debug!("seat done");
log::debug!("binding capabilities: {}", self.capability_mask);
seat.bind(self.capability_mask);
}
ei::seat::Event::Device { device } => {
log::debug!("seat: new device - {device:?}");
}
_ => todo!(),
},
e => log::debug!("unhandled event: {e:?}"),
}
self.context.flush()?;
Ok(())
}
async fn notify(&mut self, _client_event: ClientEvent) {}
async fn destroy(&mut self) {}
}

View File

@@ -1,7 +1,6 @@
use crate::client::{ClientEvent, Position}; use crate::config::Config;
use crate::emulate;
use crate::event::{Event, PointerEvent};
use anyhow::Result; use anyhow::Result;
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};
use tokio::task::LocalSet; use tokio::task::LocalSet;
@@ -13,36 +12,38 @@ pub fn run() -> Result<()> {
.enable_time() .enable_time()
.build()?; .build()?;
runtime.block_on(LocalSet::new().run_until(input_emulation_test())) let config = Config::new()?;
runtime.block_on(LocalSet::new().run_until(input_emulation_test(config)))
} }
const FREQUENCY_HZ: f64 = 1.0; const FREQUENCY_HZ: f64 = 1.0;
const RADIUS: f64 = 100.0; const RADIUS: f64 = 100.0;
async fn input_emulation_test() -> Result<()> { async fn input_emulation_test(config: Config) -> Result<()> {
let mut emulation = emulate::create().await; let backend = config.emulation_backend.map(|b| b.into());
emulation let mut emulation = input_emulation::create(backend).await?;
.notify(ClientEvent::Create(0, Position::Left)) emulation.create(0).await;
.await;
let start = Instant::now(); let start = Instant::now();
let mut offset = (0, 0); let mut offset = (0, 0);
loop { loop {
tokio::select! { tokio::time::sleep(Duration::from_millis(1)).await;
_ = emulation.dispatch() => {} let elapsed = start.elapsed();
_ = tokio::time::sleep(Duration::from_millis(1)) => { let elapsed_sec_f64 = elapsed.as_secs_f64();
let elapsed = start.elapsed(); let second_fraction = elapsed_sec_f64 - elapsed_sec_f64 as u64 as f64;
let elapsed_sec_f64 = elapsed.as_secs_f64(); let radians = second_fraction * 2. * PI * FREQUENCY_HZ;
let second_fraction = elapsed_sec_f64 - elapsed_sec_f64 as u64 as f64; let new_offset_f = (radians.cos() * RADIUS * 2., (radians * 2.).sin() * RADIUS);
let radians = second_fraction * 2. * PI * FREQUENCY_HZ; let new_offset = (new_offset_f.0 as i32, new_offset_f.1 as i32);
let new_offset_f = (radians.cos() * RADIUS * 2., (radians * 2.).sin() * RADIUS); if new_offset != offset {
let new_offset = (new_offset_f.0 as i32, new_offset_f.1 as i32); let relative_motion = (new_offset.0 - offset.0, new_offset.1 - offset.1);
if new_offset != offset { offset = new_offset;
let relative_motion = (new_offset.0 - offset.0, new_offset.1 - offset.1); let (relative_x, relative_y) = (relative_motion.0 as f64, relative_motion.1 as f64);
offset = new_offset; let event = Event::Pointer(PointerEvent::Motion {
let (relative_x, relative_y) = (relative_motion.0 as f64, relative_motion.1 as f64); time: 0,
emulation.consume(Event::Pointer(PointerEvent::Motion {time: 0, relative_x, relative_y }), 0).await; relative_x,
} relative_y,
} });
emulation.consume(event, 0).await?;
} }
} }
} }

View File

@@ -11,6 +11,7 @@ use std::{
use crate::frontend::{gtk::window::Window, FrontendRequest}; use crate::frontend::{gtk::window::Window, FrontendRequest};
use adw::Application; use adw::Application;
use endi::{Endian, ReadBytes};
use gtk::{ use gtk::{
gdk::Display, glib::clone, prelude::*, subclass::prelude::ObjectSubclassIsExt, IconTheme, gdk::Display, glib::clone, prelude::*, subclass::prelude::ObjectSubclassIsExt, IconTheme,
}; };
@@ -85,16 +86,14 @@ fn build_ui(app: &Application) {
gio::spawn_blocking(move || { gio::spawn_blocking(move || {
match loop { match loop {
// read length // read length
let mut len = [0u8; 8]; let len = match rx.read_u64(Endian::Big) {
match rx.read_exact(&mut len) { Ok(l) => l,
Ok(_) => (),
Err(e) if e.kind() == ErrorKind::UnexpectedEof => break Ok(()), Err(e) if e.kind() == ErrorKind::UnexpectedEof => break Ok(()),
Err(e) => break Err(e), Err(e) => break Err(e),
}; };
let len = usize::from_be_bytes(len);
// read payload // read payload
let mut buf = vec![0u8; len]; let mut buf = vec![0u8; len as usize];
match rx.read_exact(&mut buf) { match rx.read_exact(&mut buf) {
Ok(_) => (), Ok(_) => (),
Err(e) if e.kind() == ErrorKind::UnexpectedEof => break Ok(()), Err(e) if e.kind() == ErrorKind::UnexpectedEof => break Ok(()),

View File

@@ -10,6 +10,7 @@ use std::net::TcpStream;
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use endi::{Endian, WriteBytes};
use glib::{clone, Object}; use glib::{clone, Object};
use gtk::{ use gtk::{
gio, gio,
@@ -265,8 +266,7 @@ impl Window {
let mut stream = self.imp().stream.borrow_mut(); let mut stream = self.imp().stream.borrow_mut();
let stream = stream.as_mut().unwrap(); let stream = stream.as_mut().unwrap();
let bytes = json.as_bytes(); let bytes = json.as_bytes();
let len = bytes.len().to_be_bytes(); if let Err(e) = stream.write_u64(Endian::Big, bytes.len() as u64) {
if let Err(e) = stream.write(&len) {
log::error!("error sending message: {e}"); log::error!("error sending message: {e}");
}; };
if let Err(e) = stream.write(bytes) { if let Err(e) = stream.write(bytes) {

View File

@@ -1,13 +1,8 @@
pub mod client; pub mod client;
pub mod config; pub mod config;
pub mod dns; pub mod dns;
pub mod event;
pub mod server; pub mod server;
pub mod capture;
pub mod emulate;
pub mod capture_test; pub mod capture_test;
pub mod emulation_test; pub mod emulation_test;
pub mod frontend; pub mod frontend;
pub mod scancode;

View File

@@ -68,10 +68,12 @@ fn run_service(config: &Config) -> Result<()> {
// run async event loop // run async event loop
runtime.block_on(LocalSet::new().run_until(async { runtime.block_on(LocalSet::new().run_until(async {
// run main loop // run main loop
log::info!("Press Ctrl+Alt+Shift+Super to release the mouse"); log::info!("Press {:?} to release the mouse", config.release_bind);
let server = Server::new(config); let server = Server::new(config);
server.run().await?; server
.run(config.capture_backend, config.emulation_backend)
.await?;
log::debug!("service exiting"); log::debug!("service exiting");
anyhow::Ok(()) anyhow::Ok(())

View File

@@ -8,7 +8,7 @@ use tokio::signal;
use crate::{ use crate::{
client::{ClientConfig, ClientHandle, ClientManager, ClientState}, client::{ClientConfig, ClientHandle, ClientManager, ClientState},
config::Config, config::{CaptureBackend, Config, EmulationBackend},
dns, dns,
frontend::{FrontendListener, FrontendRequest}, frontend::{FrontendListener, FrontendRequest},
server::capture_task::CaptureEvent, server::capture_task::CaptureEvent,
@@ -40,7 +40,7 @@ pub struct Server {
client_manager: Rc<RefCell<ClientManager>>, client_manager: Rc<RefCell<ClientManager>>,
port: Rc<Cell<u16>>, port: Rc<Cell<u16>>,
state: Rc<Cell<State>>, state: Rc<Cell<State>>,
release_bind: Vec<crate::scancode::Linux>, release_bind: Vec<input_event::scancode::Linux>,
} }
impl Server { impl Server {
@@ -77,7 +77,11 @@ impl Server {
} }
} }
pub async fn run(&self) -> anyhow::Result<()> { pub async fn run(
&self,
capture_backend: Option<CaptureBackend>,
emulation_backend: Option<EmulationBackend>,
) -> anyhow::Result<()> {
// create frontend communication adapter // create frontend communication adapter
let frontend = match FrontendListener::new().await { let frontend = match FrontendListener::new().await {
Some(f) => f?, Some(f) => f?,
@@ -97,20 +101,22 @@ impl Server {
// input capture // input capture
let (mut capture_task, capture_channel) = capture_task::new( let (mut capture_task, capture_channel) = capture_task::new(
capture_backend,
self.clone(), self.clone(),
sender_tx.clone(), sender_tx.clone(),
timer_tx.clone(), timer_tx.clone(),
self.release_bind.clone(), self.release_bind.clone(),
); )?;
// input emulation // input emulation
let (mut emulation_task, emulate_channel) = emulation_task::new( let (mut emulation_task, emulate_channel) = emulation_task::new(
emulation_backend,
self.clone(), self.clone(),
receiver_rx, receiver_rx,
sender_tx.clone(), sender_tx.clone(),
capture_channel.clone(), capture_channel.clone(),
timer_tx, timer_tx,
); )?;
// create dns resolver // create dns resolver
let resolver = dns::DnsResolver::new().await?; let resolver = dns::DnsResolver::new().await?;

View File

@@ -4,13 +4,11 @@ use std::{collections::HashSet, net::SocketAddr};
use tokio::{process::Command, sync::mpsc::Sender, task::JoinHandle}; use tokio::{process::Command, sync::mpsc::Sender, task::JoinHandle};
use crate::{ use input_capture::{self, error::CaptureCreationError, CaptureHandle, InputCapture, Position};
capture::{self, InputCapture},
client::{ClientEvent, ClientHandle}, use input_event::{scancode, Event, KeyboardEvent};
event::{Event, KeyboardEvent},
scancode, use crate::{client::ClientHandle, config::CaptureBackend, server::State};
server::State,
};
use super::Server; use super::Server;
@@ -18,21 +16,25 @@ use super::Server;
pub enum CaptureEvent { pub enum CaptureEvent {
/// capture must release the mouse /// capture must release the mouse
Release, Release,
/// capture is notified of a change in client states /// add a capture client
ClientEvent(ClientEvent), Create(CaptureHandle, Position),
/// destory a capture client
Destroy(CaptureHandle),
/// termination signal /// termination signal
Terminate, Terminate,
} }
pub fn new( pub fn new(
backend: Option<CaptureBackend>,
server: Server, server: Server,
sender_tx: Sender<(Event, SocketAddr)>, sender_tx: Sender<(Event, SocketAddr)>,
timer_tx: Sender<()>, timer_tx: Sender<()>,
release_bind: Vec<scancode::Linux>, release_bind: Vec<scancode::Linux>,
) -> (JoinHandle<Result<()>>, Sender<CaptureEvent>) { ) -> Result<(JoinHandle<Result<()>>, Sender<CaptureEvent>), CaptureCreationError> {
let (tx, mut rx) = tokio::sync::mpsc::channel(32); 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 task = tokio::task::spawn_local(async move {
let mut capture = capture::create().await; let mut capture = input_capture::create(backend).await?;
let mut pressed_keys = HashSet::new(); let mut pressed_keys = HashSet::new();
loop { loop {
tokio::select! { tokio::select! {
@@ -50,9 +52,9 @@ pub fn new(
CaptureEvent::Release => { CaptureEvent::Release => {
capture.release()?; capture.release()?;
server.state.replace(State::Receiving); server.state.replace(State::Receiving);
} }
CaptureEvent::ClientEvent(e) => capture.notify(e)?, CaptureEvent::Create(h, p) => capture.create(h, p)?,
CaptureEvent::Destroy(h) => capture.destroy(h)?,
CaptureEvent::Terminate => break, CaptureEvent::Terminate => break,
}, },
None => break, None => break,
@@ -62,7 +64,7 @@ pub fn new(
} }
anyhow::Ok(()) anyhow::Ok(())
}); });
(task, tx) Ok((task, tx))
} }
fn update_pressed_keys(pressed_keys: &mut HashSet<scancode::Linux>, key: u32, state: u8) { fn update_pressed_keys(pressed_keys: &mut HashSet<scancode::Linux>, key: u32, state: u8) {
@@ -80,7 +82,7 @@ async fn handle_capture_event(
capture: &mut Box<dyn InputCapture>, capture: &mut Box<dyn InputCapture>,
sender_tx: &Sender<(Event, SocketAddr)>, sender_tx: &Sender<(Event, SocketAddr)>,
timer_tx: &Sender<()>, timer_tx: &Sender<()>,
event: (ClientHandle, Event), event: (CaptureHandle, Event),
pressed_keys: &mut HashSet<scancode::Linux>, pressed_keys: &mut HashSet<scancode::Linux>,
release_bind: &[scancode::Linux], release_bind: &[scancode::Linux],
) -> Result<()> { ) -> Result<()> {

View File

@@ -6,20 +6,22 @@ use tokio::{
task::JoinHandle, task::JoinHandle,
}; };
use crate::{ use crate::{client::ClientHandle, config::EmulationBackend, server::State};
client::{ClientEvent, ClientHandle}, use input_emulation::{
emulate::{self, InputEmulation}, self,
event::{Event, KeyboardEvent}, error::{EmulationCreationError, EmulationError},
scancode, EmulationHandle, InputEmulation,
server::State,
}; };
use input_event::{Event, KeyboardEvent};
use super::{CaptureEvent, Server}; use super::{CaptureEvent, Server};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum EmulationEvent { pub enum EmulationEvent {
/// input emulation is notified of a change in client states /// create a new client
ClientEvent(ClientEvent), Create(EmulationHandle),
/// destroy a client
Destroy(EmulationHandle),
/// input emulation must release keys for client /// input emulation must release keys for client
ReleaseKeys(ClientHandle), ReleaseKeys(ClientHandle),
/// termination signal /// termination signal
@@ -27,36 +29,36 @@ pub enum EmulationEvent {
} }
pub fn new( pub fn new(
backend: Option<EmulationBackend>,
server: Server, server: Server,
mut udp_rx: Receiver<Result<(Event, SocketAddr)>>, mut udp_rx: Receiver<Result<(Event, SocketAddr)>>,
sender_tx: Sender<(Event, SocketAddr)>, sender_tx: Sender<(Event, SocketAddr)>,
capture_tx: Sender<CaptureEvent>, capture_tx: Sender<CaptureEvent>,
timer_tx: Sender<()>, timer_tx: Sender<()>,
) -> (JoinHandle<Result<()>>, Sender<EmulationEvent>) { ) -> Result<(JoinHandle<Result<()>>, Sender<EmulationEvent>), EmulationCreationError> {
let (tx, mut rx) = tokio::sync::mpsc::channel(32); let (tx, mut rx) = tokio::sync::mpsc::channel(32);
let emulate_task = tokio::task::spawn_local(async move { let emulate_task = tokio::task::spawn_local(async move {
let mut emulate = emulate::create().await; let backend = backend.map(|b| b.into());
let mut emulate = input_emulation::create(backend).await?;
let mut last_ignored = None; let mut last_ignored = None;
loop { loop {
tokio::select! { tokio::select! {
udp_event = udp_rx.recv() => { udp_event = udp_rx.recv() => {
let udp_event = udp_event.ok_or(anyhow!("receiver closed"))??; let udp_event = udp_event.ok_or(anyhow!("receiver closed"))??;
handle_udp_rx(&server, &capture_tx, &mut emulate, &sender_tx, &mut last_ignored, udp_event, &timer_tx).await; handle_udp_rx(&server, &capture_tx, &mut emulate, &sender_tx, &mut last_ignored, udp_event, &timer_tx).await?;
} }
emulate_event = rx.recv() => { emulate_event = rx.recv() => {
match emulate_event { match emulate_event {
Some(e) => match e { Some(e) => match e {
EmulationEvent::ClientEvent(e) => emulate.notify(e).await, EmulationEvent::Create(h) => emulate.create(h).await,
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulate, c).await, EmulationEvent::Destroy(h) => emulate.destroy(h).await,
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulate, c).await?,
EmulationEvent::Terminate => break, EmulationEvent::Terminate => break,
}, },
None => break, None => break,
} }
} }
res = emulate.dispatch() => {
res?;
}
} }
} }
@@ -68,14 +70,12 @@ pub fn new(
.map(|(h, _)| h) .map(|(h, _)| h)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for client in clients { for client in clients {
release_keys(&server, &mut emulate, client).await; release_keys(&server, &mut emulate, client).await?;
} }
// destroy emulator
emulate.destroy().await;
anyhow::Ok(()) anyhow::Ok(())
}); });
(emulate_task, tx) Ok((emulate_task, tx))
} }
async fn handle_udp_rx( async fn handle_udp_rx(
@@ -86,7 +86,7 @@ async fn handle_udp_rx(
last_ignored: &mut Option<SocketAddr>, last_ignored: &mut Option<SocketAddr>,
event: (Event, SocketAddr), event: (Event, SocketAddr),
timer_tx: &Sender<()>, timer_tx: &Sender<()>,
) { ) -> Result<(), EmulationError> {
let (event, addr) = event; let (event, addr) = event;
// get handle for addr // get handle for addr
@@ -97,7 +97,7 @@ async fn handle_udp_rx(
log::warn!("ignoring events from client {addr}"); log::warn!("ignoring events from client {addr}");
last_ignored.replace(addr); last_ignored.replace(addr);
} }
return; return Ok(());
} }
}; };
@@ -111,7 +111,7 @@ async fn handle_udp_rx(
Some((_, s)) => s, Some((_, s)) => s,
None => { None => {
log::error!("unknown handle"); log::error!("unknown handle");
return; return Ok(());
} }
}; };
@@ -127,7 +127,7 @@ async fn handle_udp_rx(
let _ = sender_tx.send((Event::Pong(), addr)).await; let _ = sender_tx.send((Event::Pong(), addr)).await;
} }
(Event::Disconnect(), _) => { (Event::Disconnect(), _) => {
release_keys(server, emulate, 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
@@ -160,7 +160,7 @@ async fn handle_udp_rx(
s s
} else { } else {
log::error!("unknown handle"); log::error!("unknown handle");
return; return Ok(());
}; };
if state == 0 { if state == 0 {
// ignore release event if key not pressed // ignore release event if key not pressed
@@ -175,7 +175,7 @@ async fn handle_udp_rx(
// workaround buggy rdp backend. // workaround buggy rdp backend.
if !ignore_event { if !ignore_event {
// consume event // consume event
emulate.consume(event, handle).await; emulate.consume(event, handle).await?;
log::trace!("{event} => emulate"); log::trace!("{event} => emulate");
} }
} }
@@ -200,13 +200,14 @@ async fn handle_udp_rx(
} }
} }
} }
Ok(())
} }
async fn release_keys( async fn release_keys(
server: &Server, server: &Server,
emulate: &mut Box<dyn InputEmulation>, emulate: &mut Box<dyn InputEmulation>,
client: ClientHandle, client: ClientHandle,
) { ) -> Result<(), EmulationError> {
let keys = server let keys = server
.client_manager .client_manager
.borrow_mut() .borrow_mut()
@@ -221,19 +222,18 @@ async fn release_keys(
key, key,
state: 0, state: 0,
}); });
emulate.consume(event, client).await; emulate.consume(event, client).await?;
if let Ok(key) = scancode::Linux::try_from(key) { if let Ok(key) = input_event::scancode::Linux::try_from(key) {
log::warn!("releasing stuck key: {key:?}"); log::warn!("releasing stuck key: {key:?}");
} }
} }
let modifiers_event = KeyboardEvent::Modifiers { let event = Event::Keyboard(KeyboardEvent::Modifiers {
mods_depressed: 0, mods_depressed: 0,
mods_latched: 0, mods_latched: 0,
mods_locked: 0, mods_locked: 0,
group: 0, group: 0,
}; });
emulate emulate.consume(event, client).await?;
.consume(Event::Keyboard(modifiers_event), client) Ok(())
.await;
} }

View File

@@ -17,7 +17,7 @@ use tokio::{
}; };
use crate::{ use crate::{
client::{ClientEvent, ClientHandle, Position}, client::{ClientHandle, Position},
frontend::{self, FrontendEvent, FrontendListener, FrontendRequest}, frontend::{self, FrontendEvent, FrontendListener, FrontendRequest},
}; };
@@ -205,9 +205,8 @@ pub async fn deactivate_client(
None => return, None => return,
}; };
let event = ClientEvent::Destroy(handle); let _ = capture.send(CaptureEvent::Destroy(handle)).await;
let _ = capture.send(CaptureEvent::ClientEvent(event)).await; let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(event)).await;
} }
pub async fn activate_client( pub async fn activate_client(
@@ -237,9 +236,8 @@ pub async fn activate_client(
}; };
/* notify emulation, capture and frontends */ /* notify emulation, capture and frontends */
let event = ClientEvent::Create(handle, pos); let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
let _ = capture.send(CaptureEvent::ClientEvent(event)).await; let _ = emulate.send(EmulationEvent::Create(handle)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(event)).await;
} }
pub async fn remove_client( pub async fn remove_client(
@@ -258,9 +256,8 @@ pub async fn remove_client(
}; };
if active { if active {
let destroy = ClientEvent::Destroy(handle); let _ = capture.send(CaptureEvent::Destroy(handle)).await;
let _ = capture.send(CaptureEvent::ClientEvent(destroy)).await; let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(destroy)).await;
} }
} }
@@ -335,13 +332,11 @@ async fn update_pos(
// 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 destroy = ClientEvent::Destroy(handle); let _ = capture.send(CaptureEvent::Destroy(handle)).await;
let _ = capture.send(CaptureEvent::ClientEvent(destroy)).await; let _ = emulate.send(EmulationEvent::Destroy(handle)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(destroy)).await;
} }
let create = ClientEvent::Create(handle, pos); let _ = capture.send(CaptureEvent::Create(handle, pos.into())).await;
let _ = capture.send(CaptureEvent::ClientEvent(create)).await; let _ = emulate.send(EmulationEvent::Create(handle)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(create)).await;
} }
} }

View File

@@ -7,7 +7,8 @@ use tokio::{
task::JoinHandle, task::JoinHandle,
}; };
use crate::{event::Event, frontend::FrontendEvent}; use crate::frontend::FrontendEvent;
use input_event::Event;
use super::Server; use super::Server;

View File

@@ -5,7 +5,9 @@ use tokio::{
task::JoinHandle, task::JoinHandle,
}; };
use crate::{client::ClientHandle, event::Event}; use input_event::Event;
use crate::client::ClientHandle;
use super::{capture_task::CaptureEvent, emulation_task::EmulationEvent, Server, State}; use super::{capture_task::CaptureEvent, emulation_task::EmulationEvent, Server, State};