Compare commits

..

1 Commits

Author SHA1 Message Date
Ferdinand Schober
0c7bf3807c cleanup server code + fix a lost update case 2024-09-05 02:09:03 +02:00
33 changed files with 1166 additions and 1564 deletions

View File

@@ -55,9 +55,7 @@ jobs:
# choco install msys2 # choco install msys2
# choco install visualstudio2022-workload-vctools # choco install visualstudio2022-workload-vctools
# choco install pkgconfiglite # choco install pkgconfiglite
py -m venv .venv pipx install gvsbuild
.venv\Scripts\activate.ps1
py -m pip install gvsbuild
# see https://github.com/wingtk/gvsbuild/pull/1004 # see https://github.com/wingtk/gvsbuild/pull/1004
Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin" Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin"
Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin" Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin"

View File

@@ -66,9 +66,7 @@ jobs:
# choco install msys2 # choco install msys2
# choco install visualstudio2022-workload-vctools # choco install visualstudio2022-workload-vctools
# choco install pkgconfiglite # choco install pkgconfiglite
py -m venv .venv pipx install gvsbuild
.venv\Scripts\activate.ps1
py -m pip install gvsbuild
# see https://github.com/wingtk/gvsbuild/pull/1004 # see https://github.com/wingtk/gvsbuild/pull/1004
Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin" Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin"
Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin" Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin"

View File

@@ -51,9 +51,7 @@ jobs:
# choco install msys2 # choco install msys2
# choco install visualstudio2022-workload-vctools # choco install visualstudio2022-workload-vctools
# choco install pkgconfiglite # choco install pkgconfiglite
py -m venv .venv pipx install gvsbuild
.venv\Scripts\activate.ps1
py -m pip install gvsbuild
# see https://github.com/wingtk/gvsbuild/pull/1004 # see https://github.com/wingtk/gvsbuild/pull/1004
Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin" Move-Item "C:\Program Files\Git\usr\bin" "C:\Program Files\Git\usr\notbin"
Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin" Move-Item "C:\Program Files\Git\bin" "C:\Program Files\Git\notbin"

1048
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,7 +12,7 @@ members = [
[package] [package]
name = "lan-mouse" name = "lan-mouse"
description = "Software KVM Switch / mouse & keyboard sharing software for Local Area Networks" description = "Software KVM Switch / mouse & keyboard sharing software for Local Area Networks"
version = "0.10.0" version = "0.9.1"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -22,13 +22,13 @@ strip = true
lto = "fat" lto = "fat"
[dependencies] [dependencies]
input-event = { path = "input-event", version = "0.3.0" } input-event = { path = "input-event", version = "0.2.1" }
input-emulation = { path = "input-emulation", version = "0.3.0", default-features = false } input-emulation = { path = "input-emulation", version = "0.2.1", default-features = false }
input-capture = { path = "input-capture", version = "0.3.0", default-features = false } input-capture = { path = "input-capture", version = "0.2.0", default-features = false }
lan-mouse-cli = { path = "lan-mouse-cli", version = "0.2.0" } lan-mouse-cli = { path = "lan-mouse-cli", version = "0.1.0" }
lan-mouse-gtk = { path = "lan-mouse-gtk", version = "0.2.0", optional = true } lan-mouse-gtk = { path = "lan-mouse-gtk", version = "0.1.0", optional = true }
lan-mouse-ipc = { path = "lan-mouse-ipc", version = "0.2.0" } lan-mouse-ipc = { path = "lan-mouse-ipc", version = "0.1.0" }
lan-mouse-proto = { path = "lan-mouse-proto", version = "0.2.0" } lan-mouse-proto = { path = "lan-mouse-proto", version = "0.1.0" }
hickory-resolver = "0.24.1" hickory-resolver = "0.24.1"
toml = "0.8" toml = "0.8"
@@ -49,7 +49,7 @@ tokio = { version = "1.32.0", features = [
futures = "0.3.28" futures = "0.3.28"
clap = { version = "4.4.11", features = ["derive"] } clap = { version = "4.4.11", features = ["derive"] }
slab = "0.4.9" slab = "0.4.9"
thiserror = "2.0.0" thiserror = "1.0.61"
tokio-util = "0.7.11" tokio-util = "0.7.11"
local-channel = "0.1.5" local-channel = "0.1.5"
@@ -57,21 +57,9 @@ local-channel = "0.1.5"
libc = "0.2.148" libc = "0.2.148"
[features] [features]
default = [ default = ["wayland", "x11", "xdg_desktop_portal", "libei", "gtk"]
"gtk", wayland = ["input-capture/wayland", "input-emulation/wayland"]
"layer_shell_capture", x11 = ["input-capture/x11", "input-emulation/x11"]
"x11_capture", xdg_desktop_portal = ["input-emulation/xdg_desktop_portal"]
"libei_capture", libei = ["input-event/libei", "input-capture/libei", "input-emulation/libei"]
"wlroots_emulation",
"libei_emulation",
"rdp_emulation",
"x11_emulation",
]
gtk = ["dep:lan-mouse-gtk"] gtk = ["dep:lan-mouse-gtk"]
layer_shell_capture = ["input-capture/layer_shell"]
x11_capture = ["input-capture/x11"]
libei_capture = ["input-event/libei", "input-capture/libei"]
libei_emulation = ["input-event/libei", "input-emulation/libei"]
wlroots_emulation = ["input-emulation/wlroots"]
x11_emulation = ["input-emulation/x11"]
rdp_emulation = ["input-emulation/remote_desktop_portal"]

328
README.md
View File

@@ -1,65 +1,78 @@
# Lan Mouse # Lan Mouse
Lan Mouse is a *cross-platform* mouse and keyboard sharing software similar to universal-control on Apple devices. Lan Mouse is a mouse and keyboard sharing software similar to universal-control on Apple devices.
It allows for using multiple PCs via a single set of mouse and keyboard. It allows for using multiple pcs with a single set of mouse and keyboard.
This is also known as a Software KVM switch. This is also known as a Software KVM switch.
Goal of this project is to be an open-source alternative to proprietary tools like [Synergy 2/3](https://symless.com/synergy), [Share Mouse](https://www.sharemouse.com/de/) 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).
and an alternative to other open source tools like [Deskflow](https://github.com/deskflow/deskflow) or [Input Leap](https://github.com/input-leap) (Synergy fork).
Focus lies on performance, ease of use and a maintainable implementation that can be expanded to support additional backends for e.g. Android, iOS, ... in the future.
***blazingly fast™*** because it's written in rust.
- _Now with a gtk frontend_ - _Now with a gtk frontend_
<picture> <picture>
<source media="(prefers-color-scheme: dark)" srcset="/screenshots/dark.png?raw=true"> <source media="(prefers-color-scheme: dark)" srcset="https://github.com/feschber/lan-mouse/assets/40996949/016a06a9-76db-4951-9dcc-127d012c59df">
<source media="(prefers-color-scheme: light)" srcset="/screenshots/light.png?raw=true"> <source media="(prefers-color-scheme: light)" srcset="https://github.com/feschber/lan-mouse/assets/40996949/d6318340-f811-4e16-9d6e-d1b79883c709">
<img alt="Screenshot of Lan-Mouse" srcset="/screenshots/dark.png"> <img alt="Screenshot of Lan-Mouse" srcset="https://github.com/feschber/lan-mouse/assets/40996949/016a06a9-76db-4951-9dcc-127d012c59df">
</picture> </picture>
Goal of this project is to be an open-source replacement for proprietary tools like [Synergy 2/3](https://symless.com/synergy), [Share Mouse](https://www.sharemouse.com/de/).
Focus lies on performance and a clean, manageable implementation that can easily be expanded to support additional backends like e.g. Android, iOS, ... .
***blazingly fast™*** because it's written in rust.
For an alternative (with slightly different goals) you may check out [Input Leap](https://github.com/input-leap).
> [!WARNING] > [!WARNING]
> DISCLAIMER: > Since this tool has gained a bit of popularity over the past couple of days:
> Until [#200](https://github.com/feschber/lan-mouse/pull/200) is merged, all network traffic is **unencrypted** and sent in **plaintext**! >
> All network traffic is currently **unencrypted** and sent in **plaintext**.
> >
> A malicious actor with access to the network could read input data or send input events with spoofed IPs to take control over a device. > A malicious actor with access to the network could read input data or send input events with spoofed IPs to take control over a device.
> >
> Therefore you should only use this tool in your local network with trusted devices. > Therefore you should only use this tool in your local network with trusted devices for now
> I take no responsibility for any security breaches! > and I take no responsibility for any leakage of data!
## OS Support ## OS Support
Most current desktop environments and operating systems are fully supported, this includes The following table shows support for input emulation (to emulate events received from other clients) and
- GNOME >= 45 input capture (to send events *to* other clients) on different operating systems:
- KDE Plasma >= 6.1
- Most wlroots based compositors, including Sway (>= 1.8), Hyprland and Wayfire
- Windows
- MacOS
| OS / Desktop Environment | input emulation | input capture |
### Caveats / Known Issues |---------------------------|--------------------------|--------------------------------------|
| Wayland (wlroots) | :heavy_check_mark: | :heavy_check_mark: |
| Wayland (KDE) | :heavy_check_mark: | :heavy_check_mark: |
| Wayland (Gnome) | :heavy_check_mark: | :heavy_check_mark: (starting at GNOME 45) |
| Windows | :heavy_check_mark: | :heavy_check_mark: |
| X11 | :heavy_check_mark: | WIP |
| MacOS | :heavy_check_mark: | WIP |
> [!Important] > [!Important]
> - **X11** currently only has support for input emulation, i.e. can only be used on the receiving end. > Gnome -> Sway only partially works (modifier events are not handled correctly)
> [!Important]
> **Wayfire**
> >
> - **Sway / wlroots**: Wlroots based compositors without libei support on the receiving end currently do not handle modifier events on the client side. > If you are using [Wayfire](https://github.com/WayfireWM/wayfire), make sure to use a recent version (must be newer than October 23rd) and **add `shortcuts-inhibit` to the list of plugins in your wayfire config!**
> This results in CTRL / SHIFT / ALT / SUPER keys not working with a sending device that is NOT using the `layer-shell` backend
>
> - **Wayfire**: If you are using [Wayfire](https://github.com/WayfireWM/wayfire), make sure to use a recent version (must be newer than October 23rd) and **add `shortcuts-inhibit` to the list of plugins in your wayfire config!**
> Otherwise input capture will not work. > Otherwise input capture will not work.
>
> - **Windows**: The mouse cursor will be invisible when sending input to a Windows system if > [!Important]
> The mouse cursor will be invisible when sending input to a Windows system if
> there is no real mouse connected to the machine. > there is no real mouse connected to the machine.
For more detailed information about os support see [Detailed OS Support](#detailed-os-support)
## Installation ## Installation
### Install via cargo
```sh
cargo install lan-mouse
```
<details> ### Download from Releases
<summary>Arch Linux</summary> Precompiled release binaries for Windows, MacOS and Linux are available in the [releases section](https://github.com/feschber/lan-mouse/releases).
For Windows, the depenedencies are included in the .zip file, for other operating systems see [Installing Dependencies](#installing-dependencies).
### Arch Linux
Lan Mouse can be installed from the [official repositories](https://archlinux.org/packages/extra/x86_64/lan-mouse/): Lan Mouse can be installed from the [official repositories](https://archlinux.org/packages/extra/x86_64/lan-mouse/):
@@ -67,37 +80,39 @@ Lan Mouse can be installed from the [official repositories](https://archlinux.or
pacman -S lan-mouse pacman -S lan-mouse
``` ```
The prerelease version (following `main`) is available on the AUR: It is also available on the AUR:
```sh ```sh
# git version (includes latest changes)
paru -S lan-mouse-git paru -S lan-mouse-git
# alternatively
paru -S lan-mouse-bin
``` ```
</details>
<details>
<summary>Nix (OS)</summary>
### Nix
- nixpkgs: [search.nixos.org](https://search.nixos.org/packages?channel=unstable&show=lan-mouse&from=0&size=50&sort=relevance&type=packages&query=lan-mouse) - nixpkgs: [search.nixos.org](https://search.nixos.org/packages?channel=unstable&show=lan-mouse&from=0&size=50&sort=relevance&type=packages&query=lan-mouse)
- flake: [README.md](./nix/README.md) - flake: [README.md](./nix/README.md)
</details>
### Manual Installation
<details>
<summary>Manual Installation</summary>
First make sure to [install the necessary dependencies](#installing-dependencies). First make sure to [install the necessary dependencies](#installing-dependencies).
Precompiled release binaries for Windows, MacOS and Linux are available in the [releases section](https://github.com/feschber/lan-mouse/releases). Build in release mode:
For Windows, the depenedencies are included in the .zip file, for other operating systems see [Installing Dependencies](#installing-dependencies).
Alternatively, the `lan-mouse` binary can be compiled from source (see below).
### Installing desktop file, app icon and firewall rules (optional)
```sh ```sh
# install lan-mouse (replace path/to/ with the correct path) cargo build --release
sudo cp path/to/lan-mouse /usr/local/bin/ ```
Run directly:
```sh
cargo run --release
```
Install the files:
```sh
# install lan-mouse
sudo cp target/release/lan-mouse /usr/local/bin/
# install app icon # install app icon
sudo mkdir -p /usr/local/share/icons/hicolor/scalable/apps sudo mkdir -p /usr/local/share/icons/hicolor/scalable/apps
@@ -115,34 +130,17 @@ sudo cp firewall/lan-mouse.xml /etc/firewalld/services
# -> enable the service in firewalld settings # -> enable the service in firewalld settings
``` ```
Instead of downloading from the releases, the `lan-mouse` binary ### Conditional Compilation
can be easily compiled via cargo or nix:
### Compiling and installing manually: Currently only x11, wayland, windows and MacOS are supported backends.
```sh Depending on the toolchain used, support for other platforms is omitted
# compile in release mode automatically (it does not make sense to build a Windows `.exe` with
cargo build --release support for x11 and wayland backends).
# install lan-mouse However one might still want to omit support for e.g. wayland, x11 or libei on
sudo cp target/release/lan-mouse /usr/local/bin/ a Linux system.
```
### Compiling and installing via cargo: This is possible through
```sh
# will end up in ~/.cargo/bin
cargo install lan-mouse
```
### Compiling and installing via nix:
```sh
# you can find the executable in result/bin/lan-mouse
nix-build
```
### Conditional compilation
Support for other platforms is omitted automatically based on the active
rust toolchain.
Additionally, available backends and frontends can be configured manually via
[cargo features](https://doc.rust-lang.org/cargo/reference/features.html). [cargo features](https://doc.rust-lang.org/cargo/reference/features.html).
E.g. if only wayland support is needed, the following command produces E.g. if only wayland support is needed, the following command produces
@@ -151,17 +149,14 @@ an executable with just support for wayland:
cargo build --no-default-features --features wayland cargo build --no-default-features --features wayland
``` ```
For a detailed list of available features, checkout the [Cargo.toml](./Cargo.toml) For a detailed list of available features, checkout the [Cargo.toml](./Cargo.toml)
</details>
## Installing Dependencies
## Installing Dependencies for Development / Compiling from Source
<details> <details>
<summary>MacOS</summary> <summary>MacOS</summary>
```sh ```sh
brew install libadwaita pkg-config brew install libadwaita
``` ```
</details> </details>
@@ -188,24 +183,12 @@ sudo pacman -S libadwaita gtk libx11 libxtst
sudo dnf install libadwaita-devel libXtst-devel libX11-devel sudo dnf install libadwaita-devel libXtst-devel libX11-devel
``` ```
</details> </details>
<details>
<summary>Nix</summary>
```sh
nix-shell .
```
</details>
<details>
<summary>Nix (flake)</summary>
```sh
nix develop
```
</details>
<details> <details>
<summary>Windows</summary> <summary>Windows</summary>
> [!NOTE]
> This is only necessary when building lan-mouse from source. The windows release comes with precompiled gtk dlls.
- First install [Rust](https://www.rust-lang.org/tools/install). - First install [Rust](https://www.rust-lang.org/tools/install).
- Then follow the instructions at [gtk-rs.org](https://gtk-rs.org/gtk4-rs/stable/latest/book/installation_windows.html) - Then follow the instructions at [gtk-rs.org](https://gtk-rs.org/gtk4-rs/stable/latest/book/installation_windows.html)
@@ -236,20 +219,18 @@ python -m pipx ensurepath
pipx install gvsbuild pipx install gvsbuild
# build gtk + libadwaita # build gtk + libadwaita
gvsbuild build gtk4 libadwaita librsvg adwaita-icon-theme gvsbuild build gtk4 libadwaita librsvg
``` ```
- **Make sure to add the directory** `C:\gtk-build\gtk\x64\release\bin` - **Make sure to add the directory** `C:\gtk-build\gtk\x64\release\bin`
[**to the `PATH` environment variable**]((https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee537574(v=office.14))). Otherwise the project will fail to build. [**to the `PATH` environment variable**]((https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee537574(v=office.14))). Otherwise the project will fail to build.
To avoid building GTK from source, it is possible to disable To avoid building GTK from source, it is possible to disable
the gtk frontend (see conditional compilation). the gtk frontend (see conditional compilation below).
</details> </details>
## Usage ## Usage
<details> ### Gtk Frontend
<summary>Gtk Frontend</summary>
By default the gtk frontend will open when running `lan-mouse`. By default the gtk frontend will open when running `lan-mouse`.
To add a new connection, simply click the `Add` button on *both* devices, To add a new connection, simply click the `Add` button on *both* devices,
@@ -257,11 +238,8 @@ enter the corresponding hostname and activate it.
If the mouse can not be moved onto a device, make sure you have port `4242` (or the one selected) If the mouse can not be moved onto a device, make sure you have port `4242` (or the one selected)
opened up in your firewall. opened up in your firewall.
</details>
<details>
<summary>Command Line Interface</summary>
### Command Line Interface
The cli interface can be enabled using `--frontend cli` as commandline arguments. The cli interface can be enabled using `--frontend cli` as commandline arguments.
Type `help` to list the available commands. Type `help` to list the available commands.
@@ -275,17 +253,13 @@ $ cargo run --release -- --frontend cli
(...) (...)
> activate 0 > activate 0
``` ```
</details>
<details>
<summary>Daemon Mode</summary>
Lan Mouse can be launched in daemon mode to keep it running in the background (e.g. for use in a systemd-service).
### Daemon
Lan Mouse can be launched in daemon mode to keep it running in the background.
To do so, add `--daemon` to the commandline args: To do so, add `--daemon` to the commandline args:
```sh ```sh
lan-mouse --daemon $ cargo run --release -- --daemon
``` ```
In order to start lan-mouse with a graphical session automatically, In order to start lan-mouse with a graphical session automatically,
@@ -298,7 +272,6 @@ cp service/lan-mouse.service ~/.config/systemd/user
systemctl --user daemon-reload systemctl --user daemon-reload
systemctl --user enable --now lan-mouse.service systemctl --user enable --now lan-mouse.service
``` ```
</details>
## Configuration ## Configuration
To automatically load clients on startup, the file `$XDG_CONFIG_HOME/lan-mouse/config.toml` is parsed. To automatically load clients on startup, the file `$XDG_CONFIG_HOME/lan-mouse/config.toml` is parsed.
@@ -354,60 +327,109 @@ Where `left` can be either `left`, `right`, `top` or `bottom`.
- [x] Liveness tracking: Automatically release keys, when server offline - [x] Liveness tracking: Automatically release keys, when server offline
- [x] MacOS KeyCode Translation - [x] MacOS KeyCode Translation
- [x] Libei Input Capture - [x] Libei Input Capture
- [x] MacOS Input Capture
- [x] Windows Input Capture
- [ ] *Encryption* (WIP)
- [ ] X11 Input Capture - [ ] X11 Input Capture
- [ ] Windows Input Capture
- [ ] MacOS Input Capture
- [ ] Latency measurement and visualization - [ ] Latency measurement and visualization
- [ ] Bandwidth usage measurement and visualization - [ ] Bandwidth usage measurement and visualization
- [ ] Clipboard support - [ ] Clipboard support
- [ ] *Encryption*
## Protocol
Currently *all* mouse and keyboard events are sent via **UDP** for performance reasons.
Each event is sent as one single datagram, currently without any acknowledgement to guarantee 0% packet loss.
This means, any packet that is lost results in a discarded mouse / key event, which is ignored for now.
**UDP** also has the additional benefit that no reconnection logic is required.
Any client can just go offline and it will simply start working again as soon as it comes back online.
Additionally a tcp server is hosted for data that needs to be sent reliably (e.g. the keymap from the server or clipboard contents in the future) can be requested via a tcp connection.
## Bandwidth considerations
The most bandwidth is taken up by mouse events. A typical office mouse has a polling rate of 125Hz
while gaming mice typically have a much higher polling rate of 1000Hz.
A mouse Event consists of 21 Bytes:
- 1 Byte for the event type enum,
- 4 Bytes (u32) for the timestamp,
- 8 Bytes (f64) for dx,
- 8 Bytes (f64) for dy.
Additionally the IP header with 20 Bytes and the udp header with 8 Bytes take up another 28 Byte.
So in total there is 49 * 1000 Bytes/s for a 1000Hz gaming mouse.
This makes for a bandwidth requirement of 392 kbit/s in total _even_ for a high end gaming mouse.
So bandwidth is a non-issue.
Larger data chunks, like the keymap are offered by the server via tcp listening on the same port.
This way we dont need to implement any congestion control and leave this up to tcp.
In the future this can be used for e.g. clipboard contents as well.
## Packets per Second
While on LAN the performance is great,
some WIFI cards seem to struggle with the amount of packets per second,
particularly on high-end gaming mice with 1000Hz+ polling rates.
The plan is to implement a way of accumulating packets and sending them as
one single key event to reduce the packet rate (basically reducing the polling
rate artificially).
The way movement data is currently sent is also quite wasteful since even a 16bit integer
is likely enough to represent even the fastest possible mouse movement.
A different encoding that is more efficient for smaller values like
[Protocol Buffers](https://protobuf.dev/programming-guides/encoding/)
would be a better choice for the future and could also help for WIFI connections.
## Security
Sending key and mouse event data over the local network might not be the biggest security concern but in any public network or business environment it's *QUITE* a problem to basically broadcast your keystrokes.
- There should be an encryption layer below the application to enable a secure link.
- The encryption keys could be generated by the graphical frontend.
## Detailed OS Support ## Wayland support
### Input Emulation (for receiving events)
On wayland input-emulation is in an early/unstable state as of writing this.
In order to use a device for sending events, an **input-capture** backend is required, while receiving events requires For this reason a suitable backend is chosen based on the active desktop environment / compositor.
a supported **input-emulation** *and* **input-capture** backend.
A suitable backend is chosen automatically based on the active desktop environment / compositor. Different compositors have different ways of enabling input emulation:
The following sections detail the emulation and capture backends provided by lan-mouse and their support in desktop environments / operating systems. #### Wlroots
Most wlroots-based compositors like Hyprland and Sway support the following
unstable wayland protocols for keyboard and mouse emulation:
- [virtual-keyboard-unstable-v1](https://wayland.app/protocols/virtual-keyboard-unstable-v1)
- [wlr-virtual-pointer-unstable-v1](https://wayland.app/protocols/wlr-virtual-pointer-unstable-v1)
### Input Emulation Support #### KDE
KDE also has a protocol for input emulation ([kde-fake-input](https://wayland.app/protocols/kde-fake-input)),
it is however not exposed to third party applications.
| Desktop / Backend | layer-shell | libei | windows | macos | x11 | The recommended way to emulate input on KDE is the
|---------------------------|--------------------------|--------------------------|--------------------------|----------------------------------------|-----| [freedesktop remote-desktop-portal](https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.portal.RemoteDesktop).
| Wayland (wlroots) | :heavy_check_mark: | | | | |
| Wayland (KDE) | :heavy_check_mark: | :heavy_check_mark: | | | |
| Wayland (Gnome) | | :heavy_check_mark: | | | |
| Windows | | | :heavy_check_mark: | | |
| MacOS | | | | :heavy_check_mark: | |
| X11 | | | | | WIP |
- `wlroots`: This backend makes use of the [wlr-virtual-pointer-unstable-v1](https://wayland.app/protocols/wlr-virtual-pointer-unstable-v1) and [virtual-keyboard-unstable-v1](https://wayland.app/protocols/virtual-keyboard-unstable-v1) protocols and is supported by most wlroots based compositors. #### Gnome
- `libei`: This backend uses [libei](https://gitlab.freedesktop.org/libinput/libei) and is supported by GNOME >= 45 or KDE Plasma >= 6.1. Gnome uses [libei](https://gitlab.freedesktop.org/libinput/libei) for input emulation and capture,
- `xdp`: This backend uses the [freedesktop remote-desktop-portal](https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.portal.RemoteDesktop) and is supported on GNOME and Plasma. which has the goal to become the general approach for emulating and capturing Input on Wayland.
- `x11`: Backend for X11 sessions.
- `windows`: Backend for Windows.
- `macos`: Backend for MacOS.
### Input capture
To capture mouse and keyboard input, a few things are necessary:
- Displaying an immovable surface at screen edges
- Locking the mouse in place
- (optionally but highly recommended) reading unaccelerated mouse input
### Input Capture Support | Required Protocols (Event Emitting) | Sway | Kwin | Gnome |
|----------------------------------------|--------------------|----------------------|----------------------|
| pointer-constraints-unstable-v1 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| relative-pointer-unstable-v1 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| keyboard-shortcuts-inhibit-unstable-v1 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| wlr-layer-shell-unstable-v1 | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Desktop / Backend | wlroots | libei | remote-desktop portal | windows | macos | x11 | The [zwlr\_virtual\_pointer\_manager\_v1](wlr-virtual-pointer-unstable-v1) is required
|---------------------------|--------------------------|--------------------------|--------------------------|--------------------------|----------------------------------------|--------------------| to display surfaces on screen edges and used to display the immovable window on
| Wayland (wlroots) | :heavy_check_mark: | | | | | | both wlroots based compositors and KDE.
| Wayland (KDE) | | :heavy_check_mark: | :heavy_check_mark: | | | |
| Wayland (Gnome) | | :heavy_check_mark: | :heavy_check_mark: | | | |
| Windows | | | | :heavy_check_mark: | | |
| MacOS | | | | | :heavy_check_mark: | |
| X11 | | | | | | :heavy_check_mark: |
- `layer-shell`: This backend creates a single pixel wide window on the edges of Displays to capture the cursor using the [layer-shell protocol](https://wayland.app/protocols/wlr-layer-shell-unstable-v1).
- `libei`: This backend uses [libei](https://gitlab.freedesktop.org/libinput/libei) and is supported by GNOME >= 45 or KDE Plasma >= 6.1.
- `windows`: Backend for input capture on Windows.
- `macos`: Backend for input capture on MacOS.
- `x11`: TODO (not yet supported)
Gnome unfortunately does not support this protocol
and [likely won't ever support it](https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/1141).
~In order for layershell surfaces to be able to lock the pointer using the pointer\_constraints protocol [this patch](https://github.com/swaywm/sway/pull/7178) needs to be applied to sway.~
(this works natively on sway versions >= 1.8)

View File

@@ -1,3 +0,0 @@
{ pkgs ? import <nixpkgs> { }
}:
pkgs.callPackage nix/default.nix { }

46
flake.lock generated
View File

@@ -1,12 +1,30 @@
{ {
"nodes": { "nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1728018373, "lastModified": 1716293225,
"narHash": "sha256-NOiTvBbRLIOe5F6RbHaAh6++BNjsb149fGZd1T4+KBg=", "narHash": "sha256-pU9ViBVE3XYb70xZx+jK6SEVphvt7xMTbm6yDIF4xPs=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "bc947f541ae55e999ffdb4013441347d83b00feb", "rev": "3eaeaeb6b1e08a016380c279f8846e0bd8808916",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -24,16 +42,17 @@
}, },
"rust-overlay": { "rust-overlay": {
"inputs": { "inputs": {
"flake-utils": "flake-utils",
"nixpkgs": [ "nixpkgs": [
"nixpkgs" "nixpkgs"
] ]
}, },
"locked": { "locked": {
"lastModified": 1728181869, "lastModified": 1716257780,
"narHash": "sha256-sQXHXsjIcGEoIHkB+RO6BZdrPfB+43V1TEpyoWRI3ww=", "narHash": "sha256-R+NjvJzKEkTVCmdrKRfPE4liX/KMGVqGUwwS5H8ET8A=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "cd46aa3906c14790ef5cbe278d9e54f2c38f95c0", "rev": "4e5e3d2c5c9b2721bd266f9e43c14e96811b89d2",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -41,6 +60,21 @@
"repo": "rust-overlay", "repo": "rust-overlay",
"type": "github" "type": "github"
} }
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
} }
}, },
"root": "root", "root": "root",

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "input-capture" name = "input-capture"
description = "cross-platform input-capture library used by lan-mouse" description = "cross-platform input-capture library used by lan-mouse"
version = "0.3.0" version = "0.2.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -10,10 +10,10 @@ repository = "https://github.com/feschber/lan-mouse"
futures = "0.3.28" futures = "0.3.28"
futures-core = "0.3.30" futures-core = "0.3.30"
log = "0.4.22" log = "0.4.22"
input-event = { path = "../input-event", version = "0.3.0" } input-event = { path = "../input-event", version = "0.2.1" }
memmap = "0.7" memmap = "0.7"
tempfile = "3.8" tempfile = "3.8"
thiserror = "2.0.0" thiserror = "1.0.61"
tokio = { version = "1.32.0", features = [ tokio = { version = "1.32.0", features = [
"io-util", "io-util",
"io-std", "io-std",
@@ -40,18 +40,18 @@ wayland-protocols-wlr = { version = "0.3.1", features = [
"client", "client",
], optional = true } ], optional = true }
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true } x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
ashpd = { version = "0.10", default-features = false, features = [ ashpd = { version = "0.9", default-features = false, features = [
"tokio", "tokio",
], optional = true } ], optional = true }
reis = { version = "0.4", features = ["tokio"], optional = true } reis = { version = "0.2", features = ["tokio"], optional = true }
[target.'cfg(target_os="macos")'.dependencies] [target.'cfg(target_os="macos")'.dependencies]
core-graphics = { version = "0.24.0", features = ["highsierra"] } core-graphics = { version = "0.23", features = ["highsierra"] }
core-foundation = "0.10.0" core-foundation = "0.9.4"
core-foundation-sys = "0.8.6" core-foundation-sys = "0.8.6"
libc = "0.2.155" libc = "0.2.155"
keycode = "0.4.0" keycode = "0.4.0"
bitflags = "2.6.0" bitflags = "2.5.0"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows = { version = "0.58.0", features = [ windows = { version = "0.58.0", features = [
@@ -65,8 +65,8 @@ windows = { version = "0.58.0", features = [
] } ] }
[features] [features]
default = ["layer_shell", "x11", "libei"] default = ["wayland", "x11", "libei"]
layer_shell = [ wayland = [
"dep:wayland-client", "dep:wayland-client",
"dep:wayland-protocols", "dep:wayland-protocols",
"dep:wayland-protocols-wlr", "dep:wayland-protocols-wlr",

View File

@@ -8,7 +8,7 @@ use futures_core::Stream;
use input_event::PointerEvent; use input_event::PointerEvent;
use tokio::time::{self, Instant, Interval}; use tokio::time::{self, Instant, Interval};
use super::{Capture, CaptureError, CaptureEvent, Position}; use super::{Capture, CaptureError, CaptureEvent, CaptureHandle, Position};
pub struct DummyInputCapture { pub struct DummyInputCapture {
start: Option<Instant>, start: Option<Instant>,
@@ -34,11 +34,11 @@ impl Default for DummyInputCapture {
#[async_trait] #[async_trait]
impl Capture for DummyInputCapture { impl Capture for DummyInputCapture {
async fn create(&mut self, _pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, _handle: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
Ok(()) Ok(())
} }
async fn destroy(&mut self, _pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, _handle: CaptureHandle) -> Result<(), CaptureError> {
Ok(()) Ok(())
} }
@@ -55,7 +55,7 @@ const FREQUENCY_HZ: f64 = 1.0;
const RADIUS: f64 = 100.0; const RADIUS: f64 = 100.0;
impl Stream for DummyInputCapture { impl Stream for DummyInputCapture {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
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>> {
let current = ready!(self.interval.poll_tick(cx)); let current = ready!(self.interval.poll_tick(cx));
@@ -81,6 +81,6 @@ impl Stream for DummyInputCapture {
})) }))
} }
}; };
Poll::Ready(Some(Ok((Position::Left, event)))) Poll::Ready(Some(Ok((0, event))))
} }
} }

View File

@@ -8,9 +8,9 @@ pub enum InputCaptureError {
Capture(#[from] CaptureError), Capture(#[from] CaptureError),
} }
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use std::io; use std::io;
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use wayland_client::{ use wayland_client::{
backend::WaylandError, backend::WaylandError,
globals::{BindError, GlobalError}, globals::{BindError, GlobalError},
@@ -19,10 +19,26 @@ use wayland_client::{
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use ashpd::desktop::ResponseError; use ashpd::desktop::ResponseError;
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::{EiConvertEventStreamError, HandshakeError};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use core_graphics::base::CGError; use core_graphics::base::CGError;
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[derive(Debug, Error)]
#[error("error in libei stream: {inner:?}")]
pub struct ReisConvertEventStreamError {
inner: EiConvertEventStreamError,
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
impl From<EiConvertEventStreamError> for ReisConvertEventStreamError {
fn from(e: EiConvertEventStreamError) -> Self {
Self { inner: e }
}
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum CaptureError { pub enum CaptureError {
#[error("activation stream closed unexpectedly")] #[error("activation stream closed unexpectedly")]
@@ -32,8 +48,11 @@ pub enum CaptureError {
#[error("io error: `{0}`")] #[error("io error: `{0}`")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei error: `{0}`")] #[error("error in libei stream: `{0}`")]
Reis(#[from] reis::Error), Reis(#[from] ReisConvertEventStreamError),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei handshake failed: `{0}`")]
Handshake(#[from] HandshakeError),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error(transparent)] #[error(transparent)]
Portal(#[from] ashpd::Error), Portal(#[from] ashpd::Error),
@@ -64,7 +83,7 @@ pub enum CaptureCreationError {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("error creating input-capture-portal backend: `{0}`")] #[error("error creating input-capture-portal backend: `{0}`")]
Libei(#[from] LibeiCaptureCreationError), Libei(#[from] LibeiCaptureCreationError),
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[error("error creating layer-shell capture backend: `{0}`")] #[error("error creating layer-shell capture backend: `{0}`")]
LayerShell(#[from] LayerShellCaptureCreationError), LayerShell(#[from] LayerShellCaptureCreationError),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
@@ -74,7 +93,7 @@ pub enum CaptureCreationError {
#[error("error creating windows capture backend")] #[error("error creating windows capture backend")]
Windows, Windows,
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[error("error creating macos capture backend: `{0}`")] #[error("error creating macos capture backend")]
MacOS(#[from] MacosCaptureCreationError), MacOS(#[from] MacosCaptureCreationError),
} }
@@ -102,7 +121,7 @@ pub enum LibeiCaptureCreationError {
Ashpd(#[from] ashpd::Error), Ashpd(#[from] ashpd::Error),
} }
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("{protocol} protocol not supported: {inner}")] #[error("{protocol} protocol not supported: {inner}")]
pub struct WaylandBindError { pub struct WaylandBindError {
@@ -110,14 +129,14 @@ pub struct WaylandBindError {
protocol: &'static str, protocol: &'static str,
} }
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl WaylandBindError { impl WaylandBindError {
pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self { pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
Self { inner, protocol } Self { inner, protocol }
} }
} }
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum LayerShellCaptureCreationError { pub enum LayerShellCaptureCreationError {
#[error(transparent)] #[error(transparent)]
@@ -146,9 +165,6 @@ pub enum X11InputCaptureCreationError {
pub enum MacosCaptureCreationError { pub enum MacosCaptureCreationError {
#[error("event source creation failed!")] #[error("event source creation failed!")]
EventSourceCreation, EventSourceCreation,
#[cfg(target_os = "macos")]
#[error("event tap creation failed")]
EventTapCreation,
#[error("failed to set CG Cursor property")] #[error("failed to set CG Cursor property")]
CGCursorProperty, CGCursorProperty,
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]

View File

@@ -1,9 +1,4 @@
use std::{ use std::{collections::HashSet, fmt::Display, task::Poll};
collections::{HashMap, HashSet, VecDeque},
fmt::Display,
mem::swap,
task::{ready, Poll},
};
use async_trait::async_trait; use async_trait::async_trait;
use futures::StreamExt; use futures::StreamExt;
@@ -21,8 +16,8 @@ mod libei;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod macos; mod macos;
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
mod layer_shell; mod wayland;
#[cfg(windows)] #[cfg(windows)]
mod windows; mod windows;
@@ -87,7 +82,7 @@ impl Display for Position {
pub enum Backend { pub enum Backend {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
InputCapturePortal, InputCapturePortal,
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
LayerShell, LayerShell,
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
X11, X11,
@@ -103,7 +98,7 @@ impl Display for Backend {
match self { match self {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::InputCapturePortal => write!(f, "input-capture-portal"), Backend::InputCapturePortal => write!(f, "input-capture-portal"),
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::LayerShell => write!(f, "layer-shell"), Backend::LayerShell => write!(f, "layer-shell"),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11 => write!(f, "X11"), Backend::X11 => write!(f, "X11"),
@@ -117,52 +112,19 @@ impl Display for Backend {
} }
pub struct InputCapture { pub struct InputCapture {
/// capture backend
capture: Box<dyn Capture>, capture: Box<dyn Capture>,
/// keys pressed by active capture
pressed_keys: HashSet<scancode::Linux>, pressed_keys: HashSet<scancode::Linux>,
/// map from position to ids
position_map: HashMap<Position, Vec<CaptureHandle>>,
/// map from id to position
id_map: HashMap<CaptureHandle, Position>,
/// pending events
pending: VecDeque<(CaptureHandle, CaptureEvent)>,
} }
impl InputCapture { impl InputCapture {
/// create a new client with the given id /// create a new client with the given id
pub async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError> { pub async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
assert!(!self.id_map.contains_key(&id)); self.capture.create(id, pos).await
self.id_map.insert(id, pos);
if let Some(v) = self.position_map.get_mut(&pos) {
v.push(id);
Ok(())
} else {
self.position_map.insert(pos, vec![id]);
self.capture.create(pos).await
}
} }
/// destroy the client with the given id, if it exists /// destroy the client with the given id, if it exists
pub async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError> { pub async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError> {
let pos = self self.capture.destroy(id).await
.id_map
.remove(&id)
.expect("no position for this handle");
log::debug!("destroying capture {id} @ {pos}");
let remaining = self.position_map.get_mut(&pos).expect("id vector");
remaining.retain(|&i| i != id);
log::debug!("remaining ids @ {pos}: {remaining:?}");
if remaining.is_empty() {
log::debug!("destroying capture @ {pos} - no remaining ids");
self.position_map.remove(&pos);
self.capture.destroy(pos).await?;
}
Ok(())
} }
/// release mouse /// release mouse
@@ -181,9 +143,6 @@ impl InputCapture {
let capture = create(backend).await?; let capture = create(backend).await?;
Ok(Self { Ok(Self {
capture, capture,
id_map: Default::default(),
pending: Default::default(),
position_map: Default::default(),
pressed_keys: HashSet::new(), pressed_keys: HashSet::new(),
}) })
} }
@@ -211,65 +170,29 @@ impl Stream for InputCapture {
mut self: std::pin::Pin<&mut Self>, mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>, cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> { ) -> Poll<Option<Self::Item>> {
if let Some(e) = self.pending.pop_front() { match self.capture.poll_next_unpin(cx) {
return Poll::Ready(Some(Ok(e))); Poll::Ready(e) => {
} if let Some(Ok((
_,
// ready CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key, state, .. })),
let event = ready!(self.capture.poll_next_unpin(cx)); ))) = e
// stream closed
let event = match event {
Some(e) => e,
None => return Poll::Ready(None),
};
// error occurred
let (pos, event) = match event {
Ok(e) => e,
Err(e) => return Poll::Ready(Some(Err(e))),
};
// handle key presses
if let CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key, state, .. })) = event {
self.update_pressed_keys(key, state);
}
let len = self
.position_map
.get(&pos)
.map(|ids| ids.len())
.unwrap_or(0);
match len {
0 => Poll::Pending,
1 => Poll::Ready(Some(Ok((
self.position_map.get(&pos).expect("no id")[0],
event,
)))),
_ => {
let mut position_map = HashMap::new();
swap(&mut self.position_map, &mut position_map);
{ {
for &id in position_map.get(&pos).expect("position") { self.update_pressed_keys(key, state);
self.pending.push_back((id, event));
}
} }
swap(&mut self.position_map, &mut position_map); Poll::Ready(e)
Poll::Ready(Some(Ok(self.pending.pop_front().expect("event"))))
} }
Poll::Pending => Poll::Pending,
} }
} }
} }
#[async_trait] #[async_trait]
trait Capture: Stream<Item = Result<(Position, CaptureEvent), CaptureError>> + Unpin { trait Capture: Stream<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>> + Unpin {
/// create a new client with the given id /// create a new client with the given id
async fn create(&mut self, pos: Position) -> Result<(), CaptureError>; async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError>;
/// destroy the client with the given id, if it exists /// destroy the client with the given id, if it exists
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError>; async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError>;
/// release mouse /// release mouse
async fn release(&mut self) -> Result<(), CaptureError>; async fn release(&mut self) -> Result<(), CaptureError>;
@@ -281,14 +204,14 @@ trait Capture: Stream<Item = Result<(Position, CaptureEvent), CaptureError>> + U
async fn create_backend( async fn create_backend(
backend: Backend, backend: Backend,
) -> Result< ) -> Result<
Box<dyn Capture<Item = Result<(Position, CaptureEvent), CaptureError>>>, Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
CaptureCreationError, CaptureCreationError,
> { > {
match backend { match backend {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::InputCapturePortal => Ok(Box::new(libei::LibeiInputCapture::new().await?)), Backend::InputCapturePortal => Ok(Box::new(libei::LibeiInputCapture::new().await?)),
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::LayerShell => Ok(Box::new(layer_shell::LayerShellInputCapture::new()?)), Backend::LayerShell => Ok(Box::new(wayland::WaylandInputCapture::new()?)),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11 => Ok(Box::new(x11::X11InputCapture::new()?)), Backend::X11 => Ok(Box::new(x11::X11InputCapture::new()?)),
#[cfg(windows)] #[cfg(windows)]
@@ -302,7 +225,7 @@ async fn create_backend(
async fn create( async fn create(
backend: Option<Backend>, backend: Option<Backend>,
) -> Result< ) -> Result<
Box<dyn Capture<Item = Result<(Position, CaptureEvent), CaptureError>>>, Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
CaptureCreationError, CaptureCreationError,
> { > {
if let Some(backend) = backend { if let Some(backend) = backend {
@@ -316,7 +239,7 @@ async fn create(
for backend in [ for backend in [
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::InputCapturePortal, Backend::InputCapturePortal,
#[cfg(all(unix, feature = "layer_shell", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::LayerShell, Backend::LayerShell,
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11, Backend::X11,

View File

@@ -1,9 +1,6 @@
use ashpd::{ use ashpd::{
desktop::{ desktop::{
input_capture::{ input_capture::{Activated, Barrier, BarrierID, Capabilities, InputCapture, Region, Zones},
Activated, ActivatedBarrier, Barrier, BarrierID, Capabilities, InputCapture, Region,
Zones,
},
Session, Session,
}, },
enumflags2::BitFlags, enumflags2::BitFlags,
@@ -11,15 +8,14 @@ use ashpd::{
use async_trait::async_trait; use async_trait::async_trait;
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use reis::{ use reis::{
ei::{self, handshake::ContextType}, ei,
event::{Connection, DeviceCapability, EiEvent}, event::{DeviceCapability, EiEvent},
tokio::EiConvertEventStream, tokio::{EiConvertEventStream, EiEventStream},
}; };
use std::{ use std::{
cell::Cell, cell::Cell,
collections::HashMap, collections::HashMap,
io, io,
num::NonZeroU32,
os::unix::net::UnixStream, os::unix::net::UnixStream,
pin::Pin, pin::Pin,
rc::Rc, rc::Rc,
@@ -36,14 +32,15 @@ use tokio::{
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use futures_core::Stream; use futures_core::Stream;
use once_cell::sync::Lazy;
use input_event::Event; use input_event::Event;
use crate::CaptureEvent; use crate::CaptureEvent;
use super::{ use super::{
error::{CaptureError, LibeiCaptureCreationError}, error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
Capture as LanMouseInputCapture, Position, Capture as LanMouseInputCapture, CaptureHandle, Position,
}; };
/* there is a bug in xdg-remote-desktop-portal-gnome / mutter that /* there is a bug in xdg-remote-desktop-portal-gnome / mutter that
@@ -53,21 +50,37 @@ use super::{
/// events that necessitate restarting the capture session /// events that necessitate restarting the capture session
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
enum LibeiNotifyEvent { enum LibeiNotifyEvent {
Create(Position), Create(CaptureHandle, Position),
Destroy(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>>>,
capture_task: JoinHandle<Result<(), CaptureError>>, capture_task: JoinHandle<Result<(), CaptureError>>,
event_rx: Receiver<(Position, CaptureEvent)>, event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
notify_capture: Sender<LibeiNotifyEvent>, notify_capture: Sender<LibeiNotifyEvent>,
notify_release: Arc<Notify>, notify_release: Arc<Notify>,
cancellation_token: CancellationToken, cancellation_token: CancellationToken,
terminated: bool, terminated: bool,
} }
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
});
/// returns (start pos, end pos), inclusive /// returns (start pos, end pos), inclusive
fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) { fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) {
let (x, y) = (r.x_offset(), r.y_offset()); let (x, y) = (r.x_offset(), r.y_offset());
@@ -104,37 +117,35 @@ impl From<ICBarrier> for Barrier {
fn select_barriers( fn select_barriers(
zones: &Zones, zones: &Zones,
clients: &[Position], clients: &[(CaptureHandle, Position)],
next_barrier_id: &mut NonZeroU32, next_barrier_id: &mut u32,
) -> (Vec<ICBarrier>, HashMap<BarrierID, Position>) { ) -> (Vec<ICBarrier>, HashMap<BarrierID, CaptureHandle>) {
let mut pos_for_barrier = HashMap::new(); let mut client_for_barrier = HashMap::new();
let mut barriers: Vec<ICBarrier> = vec![]; let mut barriers: Vec<ICBarrier> = vec![];
for pos in clients { for (handle, pos) in clients {
let mut client_barriers = zones let mut client_barriers = zones
.regions() .regions()
.iter() .iter()
.map(|r| { .map(|r| {
let id = *next_barrier_id; let id = *next_barrier_id;
*next_barrier_id = next_barrier_id *next_barrier_id = id + 1;
.checked_add(1)
.expect("barrier id out of range");
let position = pos_to_barrier(r, *pos); let position = pos_to_barrier(r, *pos);
pos_for_barrier.insert(id, *pos); client_for_barrier.insert(id, *handle);
ICBarrier::new(id, position) ICBarrier::new(id, position)
}) })
.collect(); .collect();
barriers.append(&mut client_barriers); barriers.append(&mut client_barriers);
} }
(barriers, pos_for_barrier) (barriers, client_for_barrier)
} }
async fn update_barriers( async fn update_barriers(
input_capture: &InputCapture<'_>, input_capture: &InputCapture<'_>,
session: &Session<'_, InputCapture<'_>>, session: &Session<'_, InputCapture<'_>>,
active_clients: &[Position], active_clients: &[(CaptureHandle, Position)],
next_barrier_id: &mut NonZeroU32, next_barrier_id: &mut u32,
) -> Result<(Vec<ICBarrier>, HashMap<BarrierID, Position>), ashpd::Error> { ) -> Result<(Vec<ICBarrier>, HashMap<BarrierID, CaptureHandle>), ashpd::Error> {
let zones = input_capture.zones(session).await?.response()?; let zones = input_capture.zones(session).await?.response()?;
log::debug!("zones: {zones:?}"); log::debug!("zones: {zones:?}");
@@ -157,7 +168,7 @@ async fn create_session<'a>(
log::debug!("creating input capture session"); log::debug!("creating input capture session");
input_capture input_capture
.create_session( .create_session(
None, &ashpd::WindowIdentifier::default(),
Capabilities::Keyboard | Capabilities::Pointer | Capabilities::Touchscreen, Capabilities::Keyboard | Capabilities::Pointer | Capabilities::Touchscreen,
) )
.await .await
@@ -166,7 +177,7 @@ async fn create_session<'a>(
async fn connect_to_eis( async fn connect_to_eis(
input_capture: &InputCapture<'_>, input_capture: &InputCapture<'_>,
session: &Session<'_, InputCapture<'_>>, session: &Session<'_, InputCapture<'_>>,
) -> Result<(ei::Context, Connection, EiConvertEventStream), CaptureError> { ) -> Result<(ei::Context, EiConvertEventStream), CaptureError> {
log::debug!("connect_to_eis"); log::debug!("connect_to_eis");
let fd = input_capture.connect_to_eis(session).await?; let fd = input_capture.connect_to_eis(session).await?;
@@ -176,27 +187,34 @@ async fn connect_to_eis(
// create ei context // create ei context
let context = ei::Context::new(stream)?; let context = ei::Context::new(stream)?;
let (conn, event_stream) = context let mut event_stream = EiEventStream::new(context.clone())?;
.handshake_tokio("de.feschber.LanMouse", ContextType::Receiver) let response = reis::tokio::ei_handshake(
.await?; &mut event_stream,
"de.feschber.LanMouse",
ei::handshake::ContextType::Receiver,
&INTERFACES,
)
.await?;
let event_stream = EiConvertEventStream::new(event_stream, response.serial);
Ok((context, conn, event_stream)) Ok((context, event_stream))
} }
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<(Position, CaptureEvent)>, event_tx: Sender<(CaptureHandle, CaptureEvent)>,
release_session: Arc<Notify>, release_session: Arc<Notify>,
current_pos: Rc<Cell<Option<Position>>>, current_client: Rc<Cell<Option<CaptureHandle>>>,
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
loop { loop {
let ei_event = ei_event_stream let ei_event = ei_event_stream
.next() .next()
.await .await
.ok_or(CaptureError::EndOfStream)??; .ok_or(CaptureError::EndOfStream)?
.map_err(ReisConvertEventStreamError::from)?;
log::trace!("from ei: {ei_event:?}"); log::trace!("from ei: {ei_event:?}");
let client = current_pos.get(); let client = current_client.get();
handle_ei_event(ei_event, client, &context, &event_tx, &release_session).await?; handle_ei_event(ei_event, client, &context, &event_tx, &release_session).await?;
} }
} }
@@ -242,15 +260,15 @@ async fn do_capture(
mut capture_event: Receiver<LibeiNotifyEvent>, mut capture_event: Receiver<LibeiNotifyEvent>,
notify_release: Arc<Notify>, notify_release: Arc<Notify>,
session: Option<(Session<'_, InputCapture<'_>>, BitFlags<Capabilities>)>, session: Option<(Session<'_, InputCapture<'_>>, BitFlags<Capabilities>)>,
event_tx: Sender<(Position, CaptureEvent)>, event_tx: Sender<(CaptureHandle, CaptureEvent)>,
cancellation_token: CancellationToken, cancellation_token: CancellationToken,
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
let mut session = session.map(|s| s.0); let mut session = session.map(|s| s.0);
/* safety: libei_task does not outlive Self */ /* safety: libei_task does not outlive Self */
let input_capture = unsafe { &*input_capture }; let input_capture = unsafe { &*input_capture };
let mut active_clients: Vec<Position> = vec![]; let mut active_clients: Vec<(CaptureHandle, Position)> = vec![];
let mut next_barrier_id = NonZeroU32::new(1).expect("id must be non-zero"); let mut next_barrier_id = 1u32;
let mut zones_changed = input_capture.receive_zones_changed().await?; let mut zones_changed = input_capture.receive_zones_changed().await?;
@@ -323,8 +341,8 @@ async fn do_capture(
// update clients if requested // update clients if requested
if let Some(event) = capture_event_occured.take() { if let Some(event) = capture_event_occured.take() {
match event { match event {
LibeiNotifyEvent::Create(p) => active_clients.push(p), LibeiNotifyEvent::Create(c, p) => active_clients.push((c, p)),
LibeiNotifyEvent::Destroy(p) => active_clients.retain(|&pos| pos != p), LibeiNotifyEvent::Destroy(c) => active_clients.retain(|(h, _)| *h != c),
} }
} }
@@ -338,21 +356,21 @@ async fn do_capture(
async fn do_capture_session( async fn do_capture_session(
input_capture: &InputCapture<'_>, input_capture: &InputCapture<'_>,
session: &mut Session<'_, InputCapture<'_>>, session: &mut Session<'_, InputCapture<'_>>,
event_tx: &Sender<(Position, CaptureEvent)>, event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
active_clients: &[Position], active_clients: &[(CaptureHandle, Position)],
next_barrier_id: &mut NonZeroU32, next_barrier_id: &mut u32,
notify_release: &Notify, notify_release: &Notify,
cancel: (CancellationToken, CancellationToken), cancel: (CancellationToken, CancellationToken),
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
let (cancel_session, cancel_update) = cancel; let (cancel_session, cancel_update) = cancel;
// current client // current client
let current_pos = Rc::new(Cell::new(None)); let current_client = Rc::new(Cell::new(None));
// connect to eis server // connect to eis server
let (context, _conn, ei_event_stream) = connect_to_eis(input_capture, session).await?; let (context, ei_event_stream) = connect_to_eis(input_capture, session).await?;
// set barriers // set barriers
let (barriers, pos_for_barrier_id) = let (barriers, client_for_barrier_id) =
update_barriers(input_capture, session, active_clients, next_barrier_id).await?; update_barriers(input_capture, session, active_clients, next_barrier_id).await?;
log::debug!("enabling session"); log::debug!("enabling session");
@@ -364,7 +382,7 @@ async fn do_capture_session(
// async event task // async event task
let cancel_ei_handler = CancellationToken::new(); let cancel_ei_handler = CancellationToken::new();
let event_chan = event_tx.clone(); let event_chan = event_tx.clone();
let pos = current_pos.clone(); let client = current_client.clone();
let cancel_session_clone = cancel_session.clone(); let cancel_session_clone = cancel_session.clone();
let release_session_clone = release_session.clone(); let release_session_clone = release_session.clone();
let cancel_ei_handler_clone = cancel_ei_handler.clone(); let cancel_ei_handler_clone = cancel_ei_handler.clone();
@@ -375,7 +393,7 @@ async fn do_capture_session(
context, context,
event_chan, event_chan,
release_session_clone, release_session_clone,
pos, client,
) => { ) => {
log::debug!("libei exited: {r:?} cancelling session task"); log::debug!("libei exited: {r:?} cancelling session task");
cancel_session_clone.cancel(); cancel_session_clone.cancel();
@@ -397,17 +415,17 @@ async fn do_capture_session(
// get barrier id from activation // get barrier id from activation
let barrier_id = match activated.barrier_id() { let barrier_id = match activated.barrier_id() {
Some(ActivatedBarrier::Barrier(id)) => id, Some(bid) => bid,
// workaround for KDE plasma not reporting barrier ids // workaround for KDE plasma not reporting barrier ids
Some(ActivatedBarrier::UnknownBarrier) | None => find_corresponding_client(&barriers, activated.cursor_position().expect("no cursor position reported by compositor")), None => find_corresponding_client(&barriers, activated.cursor_position().expect("no cursor position reported by compositor")),
}; };
// find client corresponding to barrier // find client corresponding to barrier
let pos = *pos_for_barrier_id.get(&barrier_id).expect("invalid barrier id"); let client = *client_for_barrier_id.get(&barrier_id).expect("invalid barrier id");
current_pos.replace(Some(pos)); current_client.replace(Some(client));
// client entered => send event // client entered => send event
event_tx.send((pos, CaptureEvent::Begin)).await.expect("no channel"); event_tx.send((client, CaptureEvent::Begin)).await.expect("no channel");
tokio::select! { tokio::select! {
_ = notify_release.notified() => { /* capture release */ _ = notify_release.notified() => { /* capture release */
@@ -423,7 +441,7 @@ async fn do_capture_session(
}, },
} }
release_capture(input_capture, session, activated, pos).await?; release_capture(input_capture, session, activated, client, active_clients).await?;
} }
_ = notify_release.notified() => { /* capture release -> we are not capturing anyway, so ignore */ _ = notify_release.notified() => { /* capture release -> we are not capturing anyway, so ignore */
@@ -466,7 +484,8 @@ async fn release_capture<'a>(
input_capture: &InputCapture<'a>, input_capture: &InputCapture<'a>,
session: &Session<'a, InputCapture<'a>>, session: &Session<'a, InputCapture<'a>>,
activated: Activated, activated: Activated,
current_pos: Position, current_client: CaptureHandle,
active_clients: &[(CaptureHandle, Position)],
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
if let Some(activation_id) = activated.activation_id() { if let Some(activation_id) = activated.activation_id() {
log::debug!("releasing input capture {activation_id}"); log::debug!("releasing input capture {activation_id}");
@@ -475,7 +494,13 @@ async fn release_capture<'a>(
.cursor_position() .cursor_position()
.expect("compositor did not report cursor position!"); .expect("compositor did not report cursor position!");
log::debug!("client entered @ ({x}, {y})"); log::debug!("client entered @ ({x}, {y})");
let (dx, dy) = match current_pos { let pos = active_clients
.iter()
.filter(|(c, _)| *c == current_client)
.map(|(_, p)| p)
.next()
.unwrap(); // FIXME
let (dx, dy) = match pos {
// offset cursor position to not enter again immediately // offset cursor position to not enter again immediately
Position::Left => (1., 0.), Position::Left => (1., 0.),
Position::Right => (-1., 0.), Position::Right => (-1., 0.),
@@ -529,9 +554,9 @@ static ALL_CAPABILITIES: &[DeviceCapability] = &[
async fn handle_ei_event( async fn handle_ei_event(
ei_event: EiEvent, ei_event: EiEvent,
current_client: Option<Position>, current_client: Option<CaptureHandle>,
context: &ei::Context, context: &ei::Context,
event_tx: &Sender<(Position, CaptureEvent)>, event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
release_session: &Notify, release_session: &Notify,
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
match ei_event { match ei_event {
@@ -550,9 +575,9 @@ async fn handle_ei_event(
return Err(CaptureError::Disconnected(format!("{:?}", d.reason))) return Err(CaptureError::Disconnected(format!("{:?}", d.reason)))
} }
_ => { _ => {
if let Some(pos) = current_client { if let Some(handle) = current_client {
for event in Event::from_ei_event(ei_event) { for event in Event::from_ei_event(ei_event) {
event_tx.send((pos, CaptureEvent::Input(event))).await.expect("no channel"); event_tx.send((handle, CaptureEvent::Input(event))).await.expect("no channel");
} }
} }
} }
@@ -562,18 +587,18 @@ async fn handle_ei_event(
#[async_trait] #[async_trait]
impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> { impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
let _ = self let _ = self
.notify_capture .notify_capture
.send(LibeiNotifyEvent::Create(pos)) .send(LibeiNotifyEvent::Create(handle, pos))
.await; .await;
Ok(()) Ok(())
} }
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
let _ = self let _ = self
.notify_capture .notify_capture
.send(LibeiNotifyEvent::Destroy(pos)) .send(LibeiNotifyEvent::Destroy(handle))
.await; .await;
Ok(()) Ok(())
} }
@@ -604,7 +629,7 @@ impl<'a> Drop for LibeiInputCapture<'a> {
} }
impl<'a> Stream for LibeiInputCapture<'a> { impl<'a> Stream for LibeiInputCapture<'a> {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
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 self.capture_task.poll_unpin(cx) { match self.capture_task.poll_unpin(cx) {

View File

@@ -1,4 +1,6 @@
use super::{error::MacosCaptureCreationError, Capture, CaptureError, CaptureEvent, Position}; use super::{
error::MacosCaptureCreationError, Capture, CaptureError, CaptureEvent, CaptureHandle, Position,
};
use async_trait::async_trait; use async_trait::async_trait;
use bitflags::bitflags; use bitflags::bitflags;
use core_foundation::base::{kCFAllocatorDefault, CFRelease}; use core_foundation::base::{kCFAllocatorDefault, CFRelease};
@@ -18,14 +20,14 @@ use input_event::{Event, KeyboardEvent, PointerEvent, BTN_LEFT, BTN_MIDDLE, BTN_
use keycode::{KeyMap, KeyMapping}; use keycode::{KeyMap, KeyMapping};
use libc::c_void; use libc::c_void;
use once_cell::unsync::Lazy; use once_cell::unsync::Lazy;
use std::collections::HashSet; use std::collections::HashMap;
use std::ffi::{c_char, CString}; use std::ffi::{c_char, CString};
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
use std::task::{ready, Context, Poll}; use std::task::{ready, Context, Poll};
use std::thread::{self}; use std::thread::{self};
use tokio::sync::mpsc::{self, Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::{oneshot, Mutex}; use tokio::sync::Mutex;
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct Bounds { struct Bounds {
@@ -37,44 +39,44 @@ struct Bounds {
#[derive(Debug)] #[derive(Debug)]
struct InputCaptureState { struct InputCaptureState {
active_clients: Lazy<HashSet<Position>>, client_for_pos: Lazy<HashMap<Position, CaptureHandle>>,
current_pos: Option<Position>, current_client: Option<(CaptureHandle, Position)>,
bounds: Bounds, bounds: Bounds,
} }
#[derive(Debug)] #[derive(Debug)]
enum ProducerEvent { enum ProducerEvent {
Release, Release,
Create(Position), Create(CaptureHandle, Position),
Destroy(Position), Destroy(CaptureHandle),
Grab(Position), Grab((CaptureHandle, Position)),
EventTapDisabled, EventTapDisabled,
} }
impl InputCaptureState { impl InputCaptureState {
fn new() -> Result<Self, MacosCaptureCreationError> { fn new() -> Result<Self, MacosCaptureCreationError> {
let mut res = Self { let mut res = Self {
active_clients: Lazy::new(HashSet::new), client_for_pos: Lazy::new(HashMap::new),
current_pos: None, current_client: None,
bounds: Bounds::default(), bounds: Bounds::default(),
}; };
res.update_bounds()?; res.update_bounds()?;
Ok(res) Ok(res)
} }
fn crossed(&mut self, event: &CGEvent) -> Option<Position> { fn crossed(&mut self, event: &CGEvent) -> Option<(CaptureHandle, Position)> {
let location = event.location(); let location = event.location();
let relative_x = event.get_double_value_field(EventField::MOUSE_EVENT_DELTA_X); let relative_x = event.get_double_value_field(EventField::MOUSE_EVENT_DELTA_X);
let relative_y = event.get_double_value_field(EventField::MOUSE_EVENT_DELTA_Y); let relative_y = event.get_double_value_field(EventField::MOUSE_EVENT_DELTA_Y);
for &position in self.active_clients.iter() { for (position, client) in self.client_for_pos.iter() {
if (position == Position::Left && (location.x + relative_x) <= self.bounds.xmin) if (position == &Position::Left && (location.x + relative_x) <= self.bounds.xmin)
|| (position == Position::Right && (location.x + relative_x) >= self.bounds.xmax) || (position == &Position::Right && (location.x + relative_x) >= self.bounds.xmax)
|| (position == Position::Top && (location.y + relative_y) <= self.bounds.ymin) || (position == &Position::Top && (location.y + relative_y) <= self.bounds.ymin)
|| (position == Position::Bottom && (location.y + relative_y) >= self.bounds.ymax) || (position == &Position::Bottom && (location.y + relative_y) >= self.bounds.ymax)
{ {
log::debug!("Crossed barrier into position: {position:?}"); log::debug!("Crossed barrier into client: {client}, {position:?}");
return Some(position); return Some((*client, *position));
} }
} }
None None
@@ -100,7 +102,7 @@ impl InputCaptureState {
// to the edge of the screen, the cursor will be hidden but we dont want it to appear in a // to the edge of the screen, the cursor will be hidden but we dont want it to appear in a
// random location when we exit the client // random location when we exit the client
fn reset_mouse_position(&self, event: &CGEvent) -> Result<(), CaptureError> { fn reset_mouse_position(&self, event: &CGEvent) -> Result<(), CaptureError> {
if let Some(pos) = self.current_pos { if let Some((_, pos)) = self.current_client {
let location = event.location(); let location = event.location();
let edge_offset = 1.0; let edge_offset = 1.0;
@@ -144,31 +146,40 @@ impl InputCaptureState {
log::debug!("handling event: {producer_event:?}"); log::debug!("handling event: {producer_event:?}");
match producer_event { match producer_event {
ProducerEvent::Release => { ProducerEvent::Release => {
if self.current_pos.is_some() { if self.current_client.is_some() {
CGDisplay::show_cursor(&CGDisplay::main()) CGDisplay::show_cursor(&CGDisplay::main())
.map_err(CaptureError::CoreGraphics)?; .map_err(CaptureError::CoreGraphics)?;
self.current_pos = None; self.current_client = None;
} }
} }
ProducerEvent::Grab(pos) => { ProducerEvent::Grab(client) => {
if self.current_pos.is_none() { if self.current_client.is_none() {
CGDisplay::hide_cursor(&CGDisplay::main()) CGDisplay::hide_cursor(&CGDisplay::main())
.map_err(CaptureError::CoreGraphics)?; .map_err(CaptureError::CoreGraphics)?;
self.current_pos = Some(pos); self.current_client = Some(client);
} }
} }
ProducerEvent::Create(p) => { ProducerEvent::Create(c, p) => {
self.active_clients.insert(p); self.client_for_pos.insert(p, c);
} }
ProducerEvent::Destroy(p) => { ProducerEvent::Destroy(c) => {
if let Some(current) = self.current_pos { for pos in [
if current == p { Position::Left,
CGDisplay::show_cursor(&CGDisplay::main()) Position::Right,
.map_err(CaptureError::CoreGraphics)?; Position::Top,
self.current_pos = None; Position::Bottom,
}; ] {
if let Some((current_c, _)) = self.current_client {
if current_c == c {
CGDisplay::show_cursor(&CGDisplay::main())
.map_err(CaptureError::CoreGraphics)?;
self.current_client = None;
};
}
if self.client_for_pos.get(&pos).copied() == Some(c) {
self.client_for_pos.remove(&pos);
}
} }
self.active_clients.remove(&p);
} }
ProducerEvent::EventTapDisabled => return Err(CaptureError::EventTapDisabled), ProducerEvent::EventTapDisabled => return Err(CaptureError::EventTapDisabled),
}; };
@@ -322,11 +333,12 @@ fn get_events(
Ok(()) Ok(())
} }
fn create_event_tap<'a>( fn event_tap_thread(
client_state: Arc<Mutex<InputCaptureState>>, client_state: Arc<Mutex<InputCaptureState>>,
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
notify_tx: Sender<ProducerEvent>, notify_tx: Sender<ProducerEvent>,
event_tx: Sender<(Position, CaptureEvent)>, exit: tokio::sync::oneshot::Sender<Result<(), &'static str>>,
) -> Result<CGEventTap<'a>, MacosCaptureCreationError> { ) {
let cg_events_of_interest: Vec<CGEventType> = vec![ let cg_events_of_interest: Vec<CGEventType> = vec![
CGEventType::LeftMouseDown, CGEventType::LeftMouseDown,
CGEventType::LeftMouseUp, CGEventType::LeftMouseUp,
@@ -344,11 +356,15 @@ fn create_event_tap<'a>(
CGEventType::FlagsChanged, CGEventType::FlagsChanged,
]; ];
let event_tap_callback = let tap = CGEventTap::new(
move |_proxy: CGEventTapProxy, event_type: CGEventType, cg_ev: &CGEvent| { CGEventTapLocation::Session,
CGEventTapPlacement::HeadInsertEventTap,
CGEventTapOptions::Default,
cg_events_of_interest,
|_proxy: CGEventTapProxy, event_type: CGEventType, cg_ev: &CGEvent| {
log::trace!("Got event from tap: {event_type:?}"); log::trace!("Got event from tap: {event_type:?}");
let mut state = client_state.blocking_lock(); let mut state = client_state.blocking_lock();
let mut pos = None; let mut client = None;
let mut res_events = vec![]; let mut res_events = vec![];
if matches!( if matches!(
@@ -364,8 +380,8 @@ fn create_event_tap<'a>(
} }
// Are we in a client? // Are we in a client?
if let Some(current_pos) = state.current_pos { if let Some((current_client, _)) = state.current_client {
pos = Some(current_pos); client = Some(current_client);
get_events(&event_type, cg_ev, &mut res_events).unwrap_or_else(|e| { get_events(&event_type, cg_ev, &mut res_events).unwrap_or_else(|e| {
log::error!("Failed to get events: {e}"); log::error!("Failed to get events: {e}");
}); });
@@ -379,19 +395,19 @@ fn create_event_tap<'a>(
} }
// Did we cross a barrier? // Did we cross a barrier?
else if matches!(event_type, CGEventType::MouseMoved) { else if matches!(event_type, CGEventType::MouseMoved) {
if let Some(new_pos) = state.crossed(cg_ev) { if let Some((new_client, pos)) = state.crossed(cg_ev) {
pos = Some(new_pos); client = Some(new_client);
res_events.push(CaptureEvent::Begin); res_events.push(CaptureEvent::Begin);
notify_tx notify_tx
.blocking_send(ProducerEvent::Grab(new_pos)) .blocking_send(ProducerEvent::Grab((new_client, pos)))
.expect("Failed to send notification"); .expect("Failed to send notification");
} }
} }
if let Some(pos) = pos { if let Some(client) = client {
res_events.iter().for_each(|e| { res_events.iter().for_each(|e| {
event_tx event_tx
.blocking_send((pos, *e)) .blocking_send((client, *e))
.expect("Failed to send event"); .expect("Failed to send event");
}); });
// Returning None should stop the event from being processed // Returning None should stop the event from being processed
@@ -399,16 +415,9 @@ fn create_event_tap<'a>(
cg_ev.set_type(CGEventType::Null); cg_ev.set_type(CGEventType::Null);
} }
Some(cg_ev.to_owned()) Some(cg_ev.to_owned())
}; },
let tap = CGEventTap::new(
CGEventTapLocation::Session,
CGEventTapPlacement::HeadInsertEventTap,
CGEventTapOptions::Default,
cg_events_of_interest,
event_tap_callback,
) )
.map_err(|_| MacosCaptureCreationError::EventTapCreation)?; .expect("Failed creating tap");
let tap_source: CFRunLoopSource = tap let tap_source: CFRunLoopSource = tap
.mach_port .mach_port
@@ -419,43 +428,22 @@ fn create_event_tap<'a>(
CFRunLoop::get_current().add_source(&tap_source, kCFRunLoopCommonModes); CFRunLoop::get_current().add_source(&tap_source, kCFRunLoopCommonModes);
} }
Ok(tap)
}
fn event_tap_thread(
client_state: Arc<Mutex<InputCaptureState>>,
event_tx: Sender<(Position, CaptureEvent)>,
notify_tx: Sender<ProducerEvent>,
ready: std::sync::mpsc::Sender<Result<(), MacosCaptureCreationError>>,
exit: oneshot::Sender<Result<(), &'static str>>,
) {
let _tap = match create_event_tap(client_state, notify_tx, event_tx) {
Err(e) => {
ready.send(Err(e)).expect("channel closed");
return;
}
Ok(tap) => {
ready.send(Ok(())).expect("channel closed");
tap
}
};
CFRunLoop::run_current(); CFRunLoop::run_current();
let _ = exit.send(Err("tap thread exited")); let _ = exit.send(Err("tap thread exited"));
} }
pub struct MacOSInputCapture { pub struct MacOSInputCapture {
event_rx: Receiver<(Position, CaptureEvent)>, event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
notify_tx: Sender<ProducerEvent>, notify_tx: Sender<ProducerEvent>,
} }
impl MacOSInputCapture { impl MacOSInputCapture {
pub async fn new() -> Result<Self, MacosCaptureCreationError> { pub async fn new() -> Result<Self, MacosCaptureCreationError> {
let state = Arc::new(Mutex::new(InputCaptureState::new()?)); let state = Arc::new(Mutex::new(InputCaptureState::new()?));
let (event_tx, event_rx) = mpsc::channel(32); let (event_tx, event_rx) = tokio::sync::mpsc::channel(32);
let (notify_tx, mut notify_rx) = mpsc::channel(32); let (notify_tx, mut notify_rx) = tokio::sync::mpsc::channel(32);
let (ready_tx, ready_rx) = std::sync::mpsc::channel(); let (tap_exit_tx, mut tap_exit_rx) = tokio::sync::oneshot::channel();
let (tap_exit_tx, mut tap_exit_rx) = oneshot::channel();
unsafe { unsafe {
configure_cf_settings()?; configure_cf_settings()?;
@@ -469,14 +457,10 @@ impl MacOSInputCapture {
event_tap_thread_state, event_tap_thread_state,
event_tx, event_tx,
event_tap_notify, event_tap_notify,
ready_tx,
tap_exit_tx, tap_exit_tx,
) )
}); });
// wait for event tap creation result
ready_rx.recv().expect("channel closed")?;
let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move { let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move {
loop { loop {
tokio::select! { tokio::select! {
@@ -507,21 +491,21 @@ impl MacOSInputCapture {
#[async_trait] #[async_trait]
impl Capture for MacOSInputCapture { impl Capture for MacOSInputCapture {
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
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!("creating capture, {pos}"); log::debug!("creating client {id}, {pos}");
let _ = notify_tx.send(ProducerEvent::Create(pos)).await; let _ = notify_tx.send(ProducerEvent::Create(id, pos)).await;
log::debug!("done !"); log::debug!("done !");
}); });
Ok(()) Ok(())
} }
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError> {
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!("destroying capture {pos}"); log::debug!("destroying client {id}");
let _ = notify_tx.send(ProducerEvent::Destroy(pos)).await; let _ = notify_tx.send(ProducerEvent::Destroy(id)).await;
log::debug!("done !"); log::debug!("done !");
}); });
Ok(()) Ok(())
@@ -542,7 +526,7 @@ impl Capture for MacOSInputCapture {
} }
impl Stream for MacOSInputCapture { impl Stream for MacOSInputCapture {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
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

@@ -64,7 +64,7 @@ use crate::{CaptureError, CaptureEvent};
use super::{ use super::{
error::{LayerShellCaptureCreationError, WaylandBindError}, error::{LayerShellCaptureCreationError, WaylandBindError},
Capture, Position, Capture, CaptureHandle, Position,
}; };
struct Globals { struct Globals {
@@ -102,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>,
active_windows: Vec<Arc<Window>>, client_for_window: Vec<(Arc<Window>, CaptureHandle)>,
focused: Option<Arc<Window>>, focused: Option<(Arc<Window>, CaptureHandle)>,
g: Globals, g: Globals,
wayland_fd: RawFd, wayland_fd: RawFd,
read_guard: Option<ReadEventsGuard>, read_guard: Option<ReadEventsGuard>,
qh: QueueHandle<Self>, qh: QueueHandle<Self>,
pending_events: VecDeque<(Position, CaptureEvent)>, pending_events: VecDeque<(CaptureHandle, CaptureEvent)>,
output_info: Vec<(WlOutput, OutputInfo)>, output_info: Vec<(WlOutput, OutputInfo)>,
scroll_discrete_pending: bool, scroll_discrete_pending: bool,
} }
@@ -124,7 +124,7 @@ impl AsRawFd for Inner {
} }
} }
pub struct LayerShellInputCapture(AsyncFd<Inner>); pub struct WaylandInputCapture(AsyncFd<Inner>);
struct Window { struct Window {
buffer: wl_buffer::WlBuffer, buffer: wl_buffer::WlBuffer,
@@ -256,7 +256,7 @@ fn draw(f: &mut File, (width, height): (u32, u32)) {
} }
} }
impl LayerShellInputCapture { impl WaylandInputCapture {
pub fn new() -> std::result::Result<Self, LayerShellCaptureCreationError> { pub fn new() -> std::result::Result<Self, LayerShellCaptureCreationError> {
let conn = Connection::connect_to_env()?; let conn = Connection::connect_to_env()?;
let (g, mut queue) = registry_queue_init::<State>(&conn)?; let (g, mut queue) = registry_queue_init::<State>(&conn)?;
@@ -323,7 +323,7 @@ impl LayerShellInputCapture {
pointer_lock: None, pointer_lock: None,
rel_pointer: None, rel_pointer: None,
shortcut_inhibitor: None, shortcut_inhibitor: None,
active_windows: Vec::new(), client_for_window: Vec::new(),
focused: None, focused: None,
qh, qh,
wayland_fd, wayland_fd,
@@ -370,18 +370,23 @@ impl LayerShellInputCapture {
let inner = AsyncFd::new(Inner { queue, state })?; let inner = AsyncFd::new(Inner { queue, state })?;
Ok(LayerShellInputCapture(inner)) Ok(WaylandInputCapture(inner))
} }
fn add_client(&mut self, pos: Position) { fn add_client(&mut self, handle: CaptureHandle, pos: Position) {
self.0.get_mut().state.add_client(pos); self.0.get_mut().state.add_client(handle, pos);
} }
fn delete_client(&mut self, pos: Position) { 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.state.active_windows.iter().position(|w| w.pos == pos) { while let Some(i) = inner
inner.state.active_windows.remove(i); .state
.client_for_window
.iter()
.position(|(_, c)| *c == handle)
{
inner.state.client_for_window.remove(i);
inner.state.focused = None; inner.state.focused = None;
} }
} }
@@ -395,7 +400,7 @@ impl State {
serial: u32, serial: u32,
qh: &QueueHandle<State>, qh: &QueueHandle<State>,
) { ) {
let window = self.focused.as_ref().unwrap(); let (window, _) = self.focused.as_ref().unwrap();
// hide the cursor // hide the cursor
pointer.set_cursor(serial, None, 0, 0); pointer.set_cursor(serial, None, 0, 0);
@@ -438,7 +443,7 @@ impl State {
fn ungrab(&mut self) { fn ungrab(&mut self) {
// get focused client // get focused client
let window = match self.focused.as_ref() { let (window, _client) = match self.focused.as_ref() {
Some(focused) => focused, Some(focused) => focused,
None => return, None => return,
}; };
@@ -468,23 +473,27 @@ impl State {
} }
} }
fn add_client(&mut self, 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:?}");
outputs.iter().for_each(|(o, i)| { outputs.iter().for_each(|(o, i)| {
let window = Window::new(self, &self.qh, o, pos, i.size); let window = Window::new(self, &self.qh, o, pos, i.size);
let window = Arc::new(window); let window = Arc::new(window);
self.active_windows.push(window); self.client_for_window.push((window, client));
}); });
} }
fn update_windows(&mut self) { fn update_windows(&mut self) {
log::debug!("updating windows"); log::debug!("updating windows");
log::debug!("output info: {:?}", self.output_info); log::debug!("output info: {:?}", self.output_info);
let clients: Vec<_> = self.active_windows.drain(..).map(|w| w.pos).collect(); let clients: Vec<_> = self
for pos in clients { .client_for_window
self.add_client(pos); .drain(..)
.map(|(w, c)| (c, w.pos))
.collect();
for (client, pos) in clients {
self.add_client(client, pos);
} }
} }
} }
@@ -557,15 +566,15 @@ impl Inner {
} }
#[async_trait] #[async_trait]
impl Capture for LayerShellInputCapture { impl Capture for WaylandInputCapture {
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
self.add_client(pos); self.add_client(handle, pos);
let inner = self.0.get_mut(); let inner = self.0.get_mut();
Ok(inner.flush_events()?) Ok(inner.flush_events()?)
} }
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
self.delete_client(pos); self.delete_client(handle);
let inner = self.0.get_mut(); let inner = self.0.get_mut();
Ok(inner.flush_events()?) Ok(inner.flush_events()?)
} }
@@ -582,8 +591,8 @@ impl Capture for LayerShellInputCapture {
} }
} }
impl Stream for LayerShellInputCapture { impl Stream for WaylandInputCapture {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
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() {
@@ -648,16 +657,10 @@ impl Dispatch<wl_seat::WlSeat, ()> for State {
capabilities: WEnum::Value(capabilities), capabilities: WEnum::Value(capabilities),
} = event } = event
{ {
if capabilities.contains(wl_seat::Capability::Pointer) { if capabilities.contains(wl_seat::Capability::Pointer) && state.pointer.is_none() {
if let Some(p) = state.pointer.take() {
p.release();
}
state.pointer.replace(seat.get_pointer(qh, ())); state.pointer.replace(seat.get_pointer(qh, ()));
} }
if capabilities.contains(wl_seat::Capability::Keyboard) { if capabilities.contains(wl_seat::Capability::Keyboard) && state.keyboard.is_none() {
if let Some(k) = state.keyboard.take() {
k.release();
}
seat.get_keyboard(qh, ()); seat.get_keyboard(qh, ());
} }
} }
@@ -682,20 +685,23 @@ impl Dispatch<WlPointer, ()> for State {
} => { } => {
// get client corresponding to the focused surface // get client corresponding to the focused surface
{ {
if let Some(window) = app.active_windows.iter().find(|w| w.surface == surface) { if let Some((window, client)) = app
app.focused = Some(window.clone()); .client_for_window
.iter()
.find(|(w, _c)| w.surface == surface)
{
app.focused = Some((window.clone(), *client));
app.grab(&surface, pointer, serial, qh); app.grab(&surface, pointer, serial, qh);
} else { } else {
return; return;
} }
} }
let pos = app let (_, client) = app
.active_windows .client_for_window
.iter() .iter()
.find(|w| w.surface == surface) .find(|(w, _c)| w.surface == surface)
.map(|w| w.pos)
.unwrap(); .unwrap();
app.pending_events.push_back((pos, CaptureEvent::Begin)); app.pending_events.push_back((*client, CaptureEvent::Begin));
} }
wl_pointer::Event::Leave { .. } => { wl_pointer::Event::Leave { .. } => {
/* There are rare cases, where when a window is opened in /* There are rare cases, where when a window is opened in
@@ -716,9 +722,9 @@ impl Dispatch<WlPointer, ()> for State {
button, button,
state, state,
} => { } => {
let window = app.focused.as_ref().unwrap(); let (_, client) = app.focused.as_ref().unwrap();
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Pointer(PointerEvent::Button { CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time, time,
button, button,
@@ -727,7 +733,7 @@ impl Dispatch<WlPointer, ()> for State {
)); ));
} }
wl_pointer::Event::Axis { time, axis, value } => { wl_pointer::Event::Axis { time, axis, value } => {
let window = app.focused.as_ref().unwrap(); let (_, client) = app.focused.as_ref().unwrap();
if app.scroll_discrete_pending { if app.scroll_discrete_pending {
// each axisvalue120 event is coupled with // each axisvalue120 event is coupled with
// a corresponding axis event, which needs to // a corresponding axis event, which needs to
@@ -735,7 +741,7 @@ impl Dispatch<WlPointer, ()> for State {
app.scroll_discrete_pending = false; app.scroll_discrete_pending = false;
} else { } else {
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Pointer(PointerEvent::Axis { CaptureEvent::Input(Event::Pointer(PointerEvent::Axis {
time, time,
axis: u32::from(axis) as u8, axis: u32::from(axis) as u8,
@@ -745,10 +751,10 @@ impl Dispatch<WlPointer, ()> for State {
} }
} }
wl_pointer::Event::AxisValue120 { axis, value120 } => { wl_pointer::Event::AxisValue120 { axis, value120 } => {
let window = app.focused.as_ref().unwrap(); let (_, client) = app.focused.as_ref().unwrap();
app.scroll_discrete_pending = true; app.scroll_discrete_pending = true;
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Pointer(PointerEvent::AxisDiscrete120 { CaptureEvent::Input(Event::Pointer(PointerEvent::AxisDiscrete120 {
axis: u32::from(axis) as u8, axis: u32::from(axis) as u8,
value: value120, value: value120,
@@ -774,7 +780,10 @@ impl Dispatch<WlKeyboard, ()> for State {
_: &Connection, _: &Connection,
_: &QueueHandle<Self>, _: &QueueHandle<Self>,
) { ) {
let window = &app.focused; let (_window, client) = match &app.focused {
Some(focused) => (Some(&focused.0), Some(&focused.1)),
None => (None, None),
};
match event { match event {
wl_keyboard::Event::Key { wl_keyboard::Event::Key {
serial: _, serial: _,
@@ -782,9 +791,9 @@ impl Dispatch<WlKeyboard, ()> for State {
key, key,
state, state,
} => { } => {
if let Some(window) = window { if let Some(client) = client {
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key {
time, time,
key, key,
@@ -800,9 +809,9 @@ impl Dispatch<WlKeyboard, ()> for State {
mods_locked, mods_locked,
group, group,
} => { } => {
if let Some(window) = window { if let Some(client) = client {
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Modifiers { CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Modifiers {
depressed: mods_depressed, depressed: mods_depressed,
latched: mods_latched, latched: mods_latched,
@@ -834,10 +843,10 @@ impl Dispatch<ZwpRelativePointerV1, ()> for State {
.. ..
} = event } = event
{ {
if let Some(window) = &app.focused { if let Some((_window, client)) = &app.focused {
let time = (((utime_hi as u64) << 32 | utime_lo as u64) / 1000) as u32; let time = (((utime_hi as u64) << 32 | utime_lo as u64) / 1000) as u32;
app.pending_events.push_back(( app.pending_events.push_back((
window.pos, *client,
CaptureEvent::Input(Event::Pointer(PointerEvent::Motion { time, dx, dy })), CaptureEvent::Input(Event::Pointer(PointerEvent::Motion { time, dx, dy })),
)); ));
} }
@@ -855,10 +864,10 @@ impl Dispatch<ZwlrLayerSurfaceV1, ()> for State {
_: &QueueHandle<Self>, _: &QueueHandle<Self>,
) { ) {
if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event { if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event {
if let Some(window) = app if let Some((window, _client)) = app
.active_windows .client_for_window
.iter() .iter()
.find(|w| &w.layer_surface == layer_surface) .find(|(w, _c)| &w.layer_surface == layer_surface)
{ {
// client corresponding to the layer_surface // client corresponding to the layer_surface
let surface = &window.surface; let surface = &window.surface;

View File

@@ -3,12 +3,12 @@ use core::task::{Context, Poll};
use futures::Stream; use futures::Stream;
use once_cell::unsync::Lazy; use once_cell::unsync::Lazy;
use std::collections::HashSet; use std::collections::HashMap;
use std::ptr::{addr_of, addr_of_mut}; 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::{AtomicBool, AtomicU32, Ordering}; use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{mpsc, Mutex}; use std::sync::{mpsc, Mutex};
use std::task::ready; use std::task::ready;
use std::{pin::Pin, thread}; use std::{pin::Pin, thread};
@@ -37,15 +37,15 @@ use input_event::{
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
}; };
use super::{Capture, CaptureError, CaptureEvent, Position}; use super::{Capture, CaptureError, CaptureEvent, CaptureHandle, Position};
enum Request { enum Request {
Create(Position), Create(CaptureHandle, Position),
Destroy(Position), Destroy(CaptureHandle),
} }
pub struct WindowsInputCapture { pub struct WindowsInputCapture {
event_rx: Receiver<(Position, CaptureEvent)>, event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
msg_thread: Option<std::thread::JoinHandle<()>>, msg_thread: Option<std::thread::JoinHandle<()>>,
} }
@@ -65,22 +65,22 @@ unsafe fn signal_message_thread(event_type: EventType) {
#[async_trait] #[async_trait]
impl Capture for WindowsInputCapture { impl Capture for WindowsInputCapture {
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
unsafe { unsafe {
{ {
let mut requests = REQUEST_BUFFER.lock().unwrap(); let mut requests = REQUEST_BUFFER.lock().unwrap();
requests.push(Request::Create(pos)); requests.push(Request::Create(handle, pos));
} }
signal_message_thread(EventType::Request); signal_message_thread(EventType::Request);
} }
Ok(()) Ok(())
} }
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
unsafe { unsafe {
{ {
let mut requests = REQUEST_BUFFER.lock().unwrap(); let mut requests = REQUEST_BUFFER.lock().unwrap();
requests.push(Request::Destroy(pos)); requests.push(Request::Destroy(handle));
} }
signal_message_thread(EventType::Request); signal_message_thread(EventType::Request);
} }
@@ -98,9 +98,9 @@ impl Capture for WindowsInputCapture {
} }
static mut REQUEST_BUFFER: Mutex<Vec<Request>> = Mutex::new(Vec::new()); static mut REQUEST_BUFFER: Mutex<Vec<Request>> = Mutex::new(Vec::new());
static mut ACTIVE_CLIENT: Option<Position> = None; static mut ACTIVE_CLIENT: Option<CaptureHandle> = None;
static mut CLIENTS: Lazy<HashSet<Position>> = Lazy::new(HashSet::new); static mut CLIENT_FOR_POS: Lazy<HashMap<Position, CaptureHandle>> = Lazy::new(HashMap::new);
static mut EVENT_TX: Option<Sender<(Position, CaptureEvent)>> = None; static mut EVENT_TX: Option<Sender<(CaptureHandle, CaptureEvent)>> = 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);
@@ -281,12 +281,12 @@ unsafe fn check_client_activation(wparam: WPARAM, lparam: LPARAM) -> bool {
}; };
/* check if a client is registered for the barrier */ /* check if a client is registered for the barrier */
if !CLIENTS.contains(&pos) { let Some(client) = CLIENT_FOR_POS.get(&pos) else {
return ret; return ret;
} };
/* update active client and entry point */ /* update active client and entry point */
ACTIVE_CLIENT.replace(pos); ACTIVE_CLIENT.replace(*client);
ENTRY_POINT = clamp_to_display_bounds(prev_pos, curr_pos); ENTRY_POINT = clamp_to_display_bounds(prev_pos, curr_pos);
/* notify main thread */ /* notify main thread */
@@ -305,7 +305,7 @@ unsafe extern "system" fn mouse_proc(ncode: i32, wparam: WPARAM, lparam: LPARAM)
} }
/* get active client if any */ /* get active client if any */
let Some(pos) = ACTIVE_CLIENT else { let Some(client) = ACTIVE_CLIENT else {
return LRESULT(1); return LRESULT(1);
}; };
@@ -313,7 +313,7 @@ unsafe extern "system" fn mouse_proc(ncode: i32, wparam: WPARAM, lparam: LPARAM)
let Some(pointer_event) = to_mouse_event(wparam, lparam) else { let Some(pointer_event) = to_mouse_event(wparam, lparam) else {
return LRESULT(1); return LRESULT(1);
}; };
let event = (pos, CaptureEvent::Input(Event::Pointer(pointer_event))); let event = (client, CaptureEvent::Input(Event::Pointer(pointer_event)));
/* notify mainthread (drop events if sending too fast) */ /* notify mainthread (drop events if sending too fast) */
if let Err(e) = EVENT_TX.as_ref().unwrap().try_send(event) { if let Err(e) = EVENT_TX.as_ref().unwrap().try_send(event) {
@@ -493,8 +493,6 @@ fn get_msg() -> Option<MSG> {
} }
} }
static WINDOW_CLASS_REGISTERED: AtomicBool = AtomicBool::new(false);
fn message_thread(ready_tx: mpsc::Sender<()>) { fn message_thread(ready_tx: mpsc::Sender<()>) {
unsafe { unsafe {
set_event_tid(GetCurrentThreadId()); set_event_tid(GetCurrentThreadId());
@@ -515,15 +513,9 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
..Default::default() ..Default::default()
}; };
if WINDOW_CLASS_REGISTERED let ret = RegisterClassW(&window_class);
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) if ret == 0 {
.is_ok() panic!("RegisterClassW");
{
/* register window class if not yet done so */
let ret = RegisterClassW(&window_class);
if ret == 0 {
panic!("RegisterClassW");
}
} }
/* window is used ro receive WM_DISPLAYCHANGE messages */ /* window is used ro receive WM_DISPLAYCHANGE messages */
@@ -583,16 +575,23 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
fn update_clients(request: Request) { fn update_clients(request: Request) {
match request { match request {
Request::Create(pos) => { Request::Create(handle, pos) => {
unsafe { CLIENTS.insert(pos) }; unsafe { CLIENT_FOR_POS.insert(pos, handle) };
} }
Request::Destroy(pos) => unsafe { Request::Destroy(handle) => unsafe {
if let Some(active_pos) = ACTIVE_CLIENT { for pos in [
if pos == active_pos { Position::Left,
let _ = ACTIVE_CLIENT.take(); Position::Right,
Position::Top,
Position::Bottom,
] {
if ACTIVE_CLIENT == Some(handle) {
ACTIVE_CLIENT.take();
}
if CLIENT_FOR_POS.get(&pos).copied() == Some(handle) {
CLIENT_FOR_POS.remove(&pos);
} }
} }
CLIENTS.remove(&pos);
}, },
} }
} }
@@ -615,7 +614,7 @@ impl WindowsInputCapture {
} }
impl Stream for WindowsInputCapture { impl Stream for WindowsInputCapture {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
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),

View File

@@ -3,7 +3,10 @@ use std::task::Poll;
use async_trait::async_trait; use async_trait::async_trait;
use futures_core::Stream; use futures_core::Stream;
use super::{error::X11InputCaptureCreationError, Capture, CaptureError, CaptureEvent, Position}; use super::{
error::X11InputCaptureCreationError, Capture, CaptureError, CaptureEvent, CaptureHandle,
Position,
};
pub struct X11InputCapture {} pub struct X11InputCapture {}
@@ -15,11 +18,11 @@ impl X11InputCapture {
#[async_trait] #[async_trait]
impl Capture for X11InputCapture { impl Capture for X11InputCapture {
async fn create(&mut self, _pos: Position) -> Result<(), CaptureError> { async fn create(&mut self, _id: CaptureHandle, _pos: Position) -> Result<(), CaptureError> {
Ok(()) Ok(())
} }
async fn destroy(&mut self, _pos: Position) -> Result<(), CaptureError> { async fn destroy(&mut self, _id: CaptureHandle) -> Result<(), CaptureError> {
Ok(()) Ok(())
} }
@@ -33,7 +36,7 @@ impl Capture for X11InputCapture {
} }
impl Stream for X11InputCapture { impl Stream for X11InputCapture {
type Item = Result<(Position, CaptureEvent), CaptureError>; type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
fn poll_next( fn poll_next(
self: std::pin::Pin<&mut Self>, self: std::pin::Pin<&mut Self>,

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "input-emulation" name = "input-emulation"
description = "cross-platform input emulation library used by lan-mouse" description = "cross-platform input emulation library used by lan-mouse"
version = "0.3.0" version = "0.2.1"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -10,8 +10,8 @@ repository = "https://github.com/feschber/lan-mouse"
async-trait = "0.1.80" async-trait = "0.1.80"
futures = "0.3.28" futures = "0.3.28"
log = "0.4.22" log = "0.4.22"
input-event = { path = "../input-event", version = "0.3.0" } input-event = { path = "../input-event", version = "0.2.1" }
thiserror = "2.0.0" thiserror = "1.0.61"
tokio = { version = "1.32.0", features = [ tokio = { version = "1.32.0", features = [
"io-util", "io-util",
"io-std", "io-std",
@@ -38,14 +38,13 @@ wayland-protocols-misc = { version = "0.3.1", features = [
"client", "client",
], optional = true } ], optional = true }
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true } x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
ashpd = { version = "0.10", default-features = false, features = [ ashpd = { version = "0.9", default-features = false, features = [
"tokio", "tokio",
], optional = true } ], optional = true }
reis = { version = "0.4", features = ["tokio"], optional = true } reis = { version = "0.2", features = ["tokio"], optional = true }
[target.'cfg(target_os="macos")'.dependencies] [target.'cfg(target_os="macos")'.dependencies]
bitflags = "2.6.0" core-graphics = { version = "0.23", features = ["highsierra"] }
core-graphics = { version = "0.24.0", features = ["highsierra"] }
keycode = "0.4.0" keycode = "0.4.0"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
@@ -60,13 +59,13 @@ windows = { version = "0.58.0", features = [
] } ] }
[features] [features]
default = ["wlroots", "x11", "remote_desktop_portal", "libei"] default = ["wayland", "x11", "xdg_desktop_portal", "libei"]
wlroots = [ wayland = [
"dep:wayland-client", "dep:wayland-client",
"dep:wayland-protocols", "dep:wayland-protocols",
"dep:wayland-protocols-wlr", "dep:wayland-protocols-wlr",
"dep:wayland-protocols-misc", "dep:wayland-protocols-misc",
] ]
x11 = ["dep:x11"] x11 = ["dep:x11"]
remote_desktop_portal = ["dep:ashpd"] xdg_desktop_portal = ["dep:ashpd"]
libei = ["dep:reis", "dep:ashpd"] libei = ["dep:reis", "dep:ashpd"]

View File

@@ -6,35 +6,53 @@ pub enum InputEmulationError {
Emulate(#[from] EmulationError), Emulate(#[from] EmulationError),
} }
#[cfg(all( #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
unix,
any(feature = "remote_desktop_portal", feature = "libei"),
not(target_os = "macos")
))]
use ashpd::{desktop::ResponseError, Error::Response}; use ashpd::{desktop::ResponseError, Error::Response};
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::EiConvertEventStreamError;
use std::io; use std::io;
use thiserror::Error; use thiserror::Error;
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
use wayland_client::{ use wayland_client::{
backend::WaylandError, backend::WaylandError,
globals::{BindError, GlobalError}, globals::{BindError, GlobalError},
ConnectError, DispatchError, ConnectError, DispatchError,
}; };
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::HandshakeError;
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[derive(Debug, Error)]
#[error("error in libei stream: {inner:?}")]
pub struct ReisConvertStreamError {
inner: EiConvertEventStreamError,
}
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
impl From<EiConvertEventStreamError> for ReisConvertStreamError {
fn from(e: EiConvertEventStreamError) -> Self {
Self { inner: e }
}
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum EmulationError { pub enum EmulationError {
#[error("event stream closed")] #[error("event stream closed")]
EndOfStream, EndOfStream,
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei error: `{0}`")] #[error("libei error flushing events: `{0}`")]
Libei(#[from] reis::Error), Libei(#[from] reis::event::Error),
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("")]
LibeiConvertStream(#[from] ReisConvertStreamError),
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[error("wayland error: `{0}`")] #[error("wayland error: `{0}`")]
Wayland(#[from] wayland_client::backend::WaylandError), Wayland(#[from] wayland_client::backend::WaylandError),
#[cfg(all( #[cfg(all(
unix, unix,
any(feature = "remote_desktop_portal", feature = "libei"), any(feature = "xdg_desktop_portal", feature = "libei"),
not(target_os = "macos") not(target_os = "macos")
))] ))]
#[error("xdg-desktop-portal: `{0}`")] #[error("xdg-desktop-portal: `{0}`")]
@@ -45,13 +63,13 @@ pub enum EmulationError {
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum EmulationCreationError { pub enum EmulationCreationError {
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[error("wlroots backend: `{0}`")] #[error("wlroots backend: `{0}`")]
Wlroots(#[from] WlrootsEmulationCreationError), Wlroots(#[from] WlrootsEmulationCreationError),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei backend: `{0}`")] #[error("libei backend: `{0}`")]
Libei(#[from] LibeiEmulationCreationError), Libei(#[from] LibeiEmulationCreationError),
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
#[error("xdg-desktop-portal: `{0}`")] #[error("xdg-desktop-portal: `{0}`")]
Xdp(#[from] XdpEmulationCreationError), Xdp(#[from] XdpEmulationCreationError),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
@@ -79,7 +97,7 @@ impl EmulationCreationError {
) { ) {
return true; return true;
} }
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
if matches!( if matches!(
self, self,
EmulationCreationError::Xdp(XdpEmulationCreationError::Ashpd(Response( EmulationCreationError::Xdp(XdpEmulationCreationError::Ashpd(Response(
@@ -92,7 +110,7 @@ impl EmulationCreationError {
} }
} }
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum WlrootsEmulationCreationError { pub enum WlrootsEmulationCreationError {
#[error(transparent)] #[error(transparent)]
@@ -109,7 +127,7 @@ pub enum WlrootsEmulationCreationError {
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
} }
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("wayland protocol \"{protocol}\" not supported: {inner}")] #[error("wayland protocol \"{protocol}\" not supported: {inner}")]
pub struct WaylandBindError { pub struct WaylandBindError {
@@ -117,7 +135,7 @@ pub struct WaylandBindError {
protocol: &'static str, protocol: &'static str,
} }
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl WaylandBindError { impl WaylandBindError {
pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self { pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
Self { inner, protocol } Self { inner, protocol }
@@ -132,10 +150,10 @@ pub enum LibeiEmulationCreationError {
#[error(transparent)] #[error(transparent)]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error(transparent)] #[error(transparent)]
Reis(#[from] reis::Error), Handshake(#[from] HandshakeError),
} }
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum XdpEmulationCreationError { pub enum XdpEmulationCreationError {
#[error(transparent)] #[error(transparent)]

View File

@@ -14,10 +14,10 @@ mod windows;
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
mod x11; mod x11;
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
mod wlroots; mod wlroots;
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
mod xdg_desktop_portal; mod xdg_desktop_portal;
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
@@ -34,11 +34,11 @@ pub type EmulationHandle = u64;
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Backend { pub enum Backend {
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Wlroots, Wlroots,
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Libei, Libei,
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Xdp, Xdp,
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
X11, X11,
@@ -52,11 +52,11 @@ pub enum Backend {
impl Display for Backend { impl Display for Backend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::Wlroots => write!(f, "wlroots"), Backend::Wlroots => write!(f, "wlroots"),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::Libei => write!(f, "libei"), Backend::Libei => write!(f, "libei"),
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Backend::Xdp => write!(f, "xdg-desktop-portal"), Backend::Xdp => write!(f, "xdg-desktop-portal"),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11 => write!(f, "X11"), Backend::X11 => write!(f, "X11"),
@@ -78,13 +78,13 @@ pub struct InputEmulation {
impl InputEmulation { impl InputEmulation {
async fn with_backend(backend: Backend) -> Result<InputEmulation, EmulationCreationError> { async fn with_backend(backend: Backend) -> Result<InputEmulation, EmulationCreationError> {
let emulation: Box<dyn Emulation> = match backend { let emulation: Box<dyn Emulation> = match backend {
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::Wlroots => Box::new(wlroots::WlrootsEmulation::new()?), Backend::Wlroots => Box::new(wlroots::WlrootsEmulation::new()?),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::Libei => Box::new(libei::LibeiEmulation::new().await?), Backend::Libei => Box::new(libei::LibeiEmulation::new().await?),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11 => Box::new(x11::X11Emulation::new()?), Backend::X11 => Box::new(x11::X11Emulation::new()?),
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Backend::Xdp => Box::new(xdg_desktop_portal::DesktopPortalEmulation::new().await?), Backend::Xdp => Box::new(xdg_desktop_portal::DesktopPortalEmulation::new().await?),
#[cfg(windows)] #[cfg(windows)]
Backend::Windows => Box::new(windows::WindowsEmulation::new()?), Backend::Windows => Box::new(windows::WindowsEmulation::new()?),
@@ -109,11 +109,11 @@ impl InputEmulation {
} }
for backend in [ for backend in [
#[cfg(all(unix, feature = "wlroots", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Backend::Wlroots, Backend::Wlroots,
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Backend::Libei, Backend::Libei,
#[cfg(all(unix, feature = "remote_desktop_portal", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Backend::Xdp, Backend::Xdp,
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
Backend::X11, Backend::X11,

View File

@@ -1,18 +1,23 @@
use futures::{future, StreamExt}; use futures::{future, StreamExt};
use once_cell::sync::Lazy;
use std::{ use std::{
collections::HashMap,
io, io,
os::{fd::OwnedFd, unix::net::UnixStream}, os::{fd::OwnedFd, unix::net::UnixStream},
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, AtomicU32, Ordering},
Arc, Mutex, RwLock, Arc, Mutex, RwLock,
}, },
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use ashpd::desktop::{ use ashpd::{
remote_desktop::{DeviceType, RemoteDesktop}, desktop::{
PersistMode, Session, remote_desktop::{DeviceType, RemoteDesktop},
PersistMode, Session,
},
WindowIdentifier,
}; };
use async_trait::async_trait; use async_trait::async_trait;
@@ -21,16 +26,32 @@ use reis::{
self, button::ButtonState, handshake::ContextType, keyboard::KeyState, Button, Keyboard, self, button::ButtonState, handshake::ContextType, keyboard::KeyState, Button, Keyboard,
Pointer, Scroll, Pointer, Scroll,
}, },
event::{self, Connection, DeviceCapability, DeviceEvent, EiEvent, SeatEvent}, event::{DeviceCapability, DeviceEvent, EiEvent, SeatEvent},
tokio::EiConvertEventStream, tokio::{ei_handshake, EiConvertEventStream, EiEventStream},
}; };
use input_event::{Event, KeyboardEvent, PointerEvent}; use input_event::{Event, KeyboardEvent, PointerEvent};
use crate::error::EmulationError; use crate::error::{EmulationError, ReisConvertStreamError};
use super::{error::LibeiEmulationCreationError, Emulation, EmulationHandle}; use super::{error::LibeiEmulationCreationError, Emulation, EmulationHandle};
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)] #[derive(Clone, Default)]
struct Devices { struct Devices {
pointer: Arc<RwLock<Option<(ei::Device, ei::Pointer)>>>, pointer: Arc<RwLock<Option<(ei::Device, ei::Pointer)>>>,
@@ -41,11 +62,11 @@ struct Devices {
pub(crate) struct LibeiEmulation<'a> { pub(crate) struct LibeiEmulation<'a> {
context: ei::Context, context: ei::Context,
conn: event::Connection,
devices: Devices, devices: Devices,
ei_task: JoinHandle<()>, ei_task: JoinHandle<()>,
error: Arc<Mutex<Option<EmulationError>>>, error: Arc<Mutex<Option<EmulationError>>>,
libei_error: Arc<AtomicBool>, libei_error: Arc<AtomicBool>,
serial: AtomicU32,
_remote_desktop: RemoteDesktop<'a>, _remote_desktop: RemoteDesktop<'a>,
session: Session<'a, RemoteDesktop<'a>>, session: Session<'a, RemoteDesktop<'a>>,
} }
@@ -68,7 +89,10 @@ async fn get_ei_fd<'a>(
.await?; .await?;
log::info!("requesting permission for input emulation"); log::info!("requesting permission for input emulation");
let _devices = remote_desktop.start(&session, None).await?.response()?; let _devices = remote_desktop
.start(&session, &WindowIdentifier::default())
.await?
.response()?;
let fd = remote_desktop.connect_to_eis(&session).await?; let fd = remote_desktop.connect_to_eis(&session).await?;
Ok((remote_desktop, session, fd)) Ok((remote_desktop, session, fd))
@@ -80,15 +104,20 @@ impl<'a> LibeiEmulation<'a> {
let stream = UnixStream::from(eifd); let stream = UnixStream::from(eifd);
stream.set_nonblocking(true)?; stream.set_nonblocking(true)?;
let context = ei::Context::new(stream)?; let context = ei::Context::new(stream)?;
let (conn, events) = context let mut events = EiEventStream::new(context.clone())?;
.handshake_tokio("de.feschber.LanMouse", ContextType::Sender) let handshake = ei_handshake(
.await?; &mut events,
"de.feschber.LanMouse",
ContextType::Sender,
&INTERFACES,
)
.await?;
let events = EiConvertEventStream::new(events, handshake.serial);
let devices = Devices::default(); let devices = Devices::default();
let libei_error = Arc::new(AtomicBool::default()); let libei_error = Arc::new(AtomicBool::default());
let error = Arc::new(Mutex::new(None)); let error = Arc::new(Mutex::new(None));
let ei_handler = ei_task( let ei_handler = ei_task(
events, events,
conn.clone(),
context.clone(), context.clone(),
devices.clone(), devices.clone(),
libei_error.clone(), libei_error.clone(),
@@ -96,13 +125,15 @@ impl<'a> LibeiEmulation<'a> {
); );
let ei_task = tokio::task::spawn_local(ei_handler); let ei_task = tokio::task::spawn_local(ei_handler);
let serial = AtomicU32::new(handshake.serial);
Ok(Self { Ok(Self {
context, context,
conn,
devices, devices,
ei_task, ei_task,
error, error,
libei_error, libei_error,
serial,
_remote_desktop, _remote_desktop,
session, session,
}) })
@@ -138,7 +169,7 @@ impl<'a> Emulation for LibeiEmulation<'a> {
let pointer_device = self.devices.pointer.read().unwrap(); let pointer_device = self.devices.pointer.read().unwrap();
if let Some((d, p)) = pointer_device.as_ref() { if let Some((d, p)) = pointer_device.as_ref() {
p.motion_relative(dx as f32, dy as f32); p.motion_relative(dx as f32, dy as f32);
d.frame(self.conn.serial(), now); d.frame(self.serial.load(Ordering::SeqCst), now);
} }
} }
PointerEvent::Button { PointerEvent::Button {
@@ -155,7 +186,7 @@ impl<'a> Emulation for LibeiEmulation<'a> {
_ => ButtonState::Press, _ => ButtonState::Press,
}, },
); );
d.frame(self.conn.serial(), now); d.frame(self.serial.load(Ordering::SeqCst), now);
} }
} }
PointerEvent::Axis { PointerEvent::Axis {
@@ -169,7 +200,7 @@ impl<'a> Emulation for LibeiEmulation<'a> {
0 => s.scroll(0., value as f32), 0 => s.scroll(0., value as f32),
_ => s.scroll(value as f32, 0.), _ => s.scroll(value as f32, 0.),
} }
d.frame(self.conn.serial(), now); d.frame(self.serial.load(Ordering::SeqCst), now);
} }
} }
PointerEvent::AxisDiscrete120 { axis, value } => { PointerEvent::AxisDiscrete120 { axis, value } => {
@@ -179,7 +210,7 @@ impl<'a> Emulation for LibeiEmulation<'a> {
0 => s.scroll_discrete(0, value), 0 => s.scroll_discrete(0, value),
_ => s.scroll_discrete(value, 0), _ => s.scroll_discrete(value, 0),
} }
d.frame(self.conn.serial(), now); d.frame(self.serial.load(Ordering::SeqCst), now);
} }
} }
}, },
@@ -198,7 +229,7 @@ impl<'a> Emulation for LibeiEmulation<'a> {
_ => KeyState::Press, _ => KeyState::Press,
}, },
); );
d.frame(self.conn.serial(), now); d.frame(self.serial.load(Ordering::SeqCst), now);
} }
} }
KeyboardEvent::Modifiers { .. } => {} KeyboardEvent::Modifiers { .. } => {}
@@ -221,7 +252,6 @@ impl<'a> Emulation for LibeiEmulation<'a> {
async fn ei_task( async fn ei_task(
mut events: EiConvertEventStream, mut events: EiConvertEventStream,
_conn: Connection,
context: ei::Context, context: ei::Context,
devices: Devices, devices: Devices,
libei_error: Arc<AtomicBool>, libei_error: Arc<AtomicBool>,
@@ -246,7 +276,11 @@ async fn ei_event_handler(
devices: &Devices, devices: &Devices,
) -> Result<(), EmulationError> { ) -> Result<(), EmulationError> {
loop { loop {
let event = events.next().await.ok_or(EmulationError::EndOfStream)??; let event = events
.next()
.await
.ok_or(EmulationError::EndOfStream)?
.map_err(ReisConvertStreamError::from)?;
const CAPABILITIES: &[DeviceCapability] = &[ const CAPABILITIES: &[DeviceCapability] = &[
DeviceCapability::Pointer, DeviceCapability::Pointer,
DeviceCapability::PointerAbsolute, DeviceCapability::PointerAbsolute,

View File

@@ -1,23 +1,15 @@
use super::{error::EmulationError, Emulation, EmulationHandle}; use super::{error::EmulationError, Emulation, EmulationHandle};
use async_trait::async_trait; use async_trait::async_trait;
use bitflags::bitflags; use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
use core_graphics::base::CGFloat;
use core_graphics::display::{
CGDirectDisplayID, CGDisplayBounds, CGGetDisplaysWithRect, CGPoint, CGRect, CGSize,
};
use core_graphics::event::{ use core_graphics::event::{
CGEvent, CGEventFlags, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, ScrollEventUnit,
ScrollEventUnit,
}; };
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID}; use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use input_event::{scancode, Event, KeyboardEvent, PointerEvent}; use input_event::{Event, KeyboardEvent, PointerEvent};
use keycode::{KeyMap, KeyMapping}; use keycode::{KeyMap, KeyMapping};
use std::cell::Cell;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::{sync::Notify, task::JoinHandle}; use tokio::task::AbortHandle;
use super::error::MacOSEmulationCreationError; use super::error::MacOSEmulationCreationError;
@@ -26,10 +18,8 @@ const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
pub(crate) struct MacOSEmulation { pub(crate) struct MacOSEmulation {
event_source: CGEventSource, event_source: CGEventSource,
repeat_task: Option<JoinHandle<()>>, repeat_task: Option<AbortHandle>,
button_state: ButtonState, button_state: ButtonState,
modifier_state: Rc<Cell<XMods>>,
notify_repeat_task: Arc<Notify>,
} }
struct ButtonState { struct ButtonState {
@@ -75,8 +65,6 @@ impl MacOSEmulation {
event_source, event_source,
button_state, button_state,
repeat_task: None, repeat_task: None,
notify_repeat_task: Arc::new(Notify::new()),
modifier_state: Rc::new(Cell::new(XMods::empty())),
}) })
} }
@@ -88,40 +76,25 @@ impl MacOSEmulation {
async fn spawn_repeat_task(&mut self, key: u16) { async fn spawn_repeat_task(&mut self, key: u16) {
// there can only be one repeating key and it's // there can only be one repeating key and it's
// always the last to be pressed // always the last to be pressed
self.cancel_repeat_task().await; self.kill_repeat_task();
let event_source = self.event_source.clone(); let event_source = self.event_source.clone();
let notify = self.notify_repeat_task.clone();
let modifiers = self.modifier_state.clone();
let repeat_task = tokio::task::spawn_local(async move { let repeat_task = tokio::task::spawn_local(async move {
let stop = tokio::select! { tokio::time::sleep(DEFAULT_REPEAT_DELAY).await;
_ = tokio::time::sleep(DEFAULT_REPEAT_DELAY) => false, loop {
_ = notify.notified() => true, key_event(event_source.clone(), key, 1);
}; tokio::time::sleep(DEFAULT_REPEAT_INTERVAL).await;
if !stop {
loop {
key_event(event_source.clone(), key, 1, modifiers.get());
tokio::select! {
_ = tokio::time::sleep(DEFAULT_REPEAT_INTERVAL) => {},
_ = notify.notified() => break,
}
}
} }
// release key when cancelled
update_modifiers(&modifiers, key as u32, 0);
key_event(event_source.clone(), key, 0, modifiers.get());
}); });
self.repeat_task = Some(repeat_task); self.repeat_task = Some(repeat_task.abort_handle());
} }
fn kill_repeat_task(&mut self) {
async fn cancel_repeat_task(&mut self) {
if let Some(task) = self.repeat_task.take() { if let Some(task) = self.repeat_task.take() {
self.notify_repeat_task.notify_waiters(); task.abort();
let _ = task.await;
} }
} }
} }
fn key_event(event_source: CGEventSource, key: u16, state: u8, modifiers: XMods) { fn key_event(event_source: CGEventSource, key: u16, state: u8) {
let event = match CGEvent::new_keyboard_event(event_source, key, state != 0) { let event = match CGEvent::new_keyboard_event(event_source, key, state != 0) {
Ok(e) => e, Ok(e) => e,
Err(_) => { Err(_) => {
@@ -129,92 +102,7 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8, modifiers: XMods)
return; return;
} }
}; };
event.set_flags(to_cgevent_flags(modifiers));
event.post(CGEventTapLocation::HID); event.post(CGEventTapLocation::HID);
log::trace!("key event: {key} {state}");
}
fn modifier_event(event_source: CGEventSource, depressed: XMods) {
let Ok(event) = CGEvent::new(event_source) else {
log::warn!("could not create CGEvent");
return;
};
let flags = to_cgevent_flags(depressed);
event.set_type(CGEventType::FlagsChanged);
event.set_flags(flags);
event.post(CGEventTapLocation::HID);
log::trace!("modifiers updated: {depressed:?}");
}
fn get_display_at_point(x: CGFloat, y: CGFloat) -> Option<CGDirectDisplayID> {
let mut displays: [CGDirectDisplayID; 16] = [0; 16];
let mut display_count: u32 = 0;
let rect = CGRect::new(&CGPoint::new(x, y), &CGSize::new(0.0, 0.0));
let error = unsafe {
CGGetDisplaysWithRect(
rect,
1,
displays.as_mut_ptr(),
&mut display_count as *mut u32,
)
};
if error != 0 {
log::warn!("error getting displays at point ({}, {}): {}", x, y, error);
return Option::None;
}
if display_count == 0 {
log::debug!("no displays found at point ({}, {})", x, y);
return Option::None;
}
return displays.first().copied();
}
fn get_display_bounds(display: CGDirectDisplayID) -> (CGFloat, CGFloat, CGFloat, CGFloat) {
unsafe {
let bounds = CGDisplayBounds(display);
let min_x = bounds.origin.x;
let max_x = bounds.origin.x + bounds.size.width;
let min_y = bounds.origin.y;
let max_y = bounds.origin.y + bounds.size.height;
(min_x as f64, min_y as f64, max_x as f64, max_y as f64)
}
}
fn clamp_to_screen_space(
current_x: CGFloat,
current_y: CGFloat,
dx: CGFloat,
dy: CGFloat,
) -> (CGFloat, CGFloat) {
// Check which display the mouse is currently on
// Determine what the location of the mouse would be after applying the move
// Get the display at the new location
// If the point is not on a display
// Clamp the mouse to the current display
// Else If the point is on a display
// Clamp the mouse to the new display
let current_display = match get_display_at_point(current_x, current_y) {
Some(display) => display,
None => {
log::warn!("could not get current display!");
return (current_x, current_y);
}
};
let new_x = current_x + dx;
let new_y = current_y + dy;
let final_display = get_display_at_point(new_x, new_y).unwrap_or(current_display);
let (min_x, min_y, max_x, max_y) = get_display_bounds(final_display);
(
new_x.clamp(min_x, max_x - 1.),
new_y.clamp(min_y, max_y - 1.),
)
} }
#[async_trait] #[async_trait]
@@ -227,6 +115,16 @@ impl Emulation for MacOSEmulation {
match event { match event {
Event::Pointer(pointer_event) => match pointer_event { Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion { time: _, dx, dy } => { PointerEvent::Motion { time: _, dx, dy } => {
// FIXME secondary displays?
let (min_x, min_y, max_x, max_y) = unsafe {
let display = CGMainDisplayID();
let bounds = CGDisplayBounds(display);
let min_x = bounds.origin.x;
let max_x = bounds.origin.x + bounds.size.width;
let min_y = bounds.origin.y;
let max_y = bounds.origin.y + bounds.size.height;
(min_x as f64, min_y as f64, max_x as f64, max_y as f64)
};
let mut mouse_location = match self.get_mouse_location() { let mut mouse_location = match self.get_mouse_location() {
Some(l) => l, Some(l) => l,
None => { None => {
@@ -235,11 +133,8 @@ impl Emulation for MacOSEmulation {
} }
}; };
let (new_mouse_x, new_mouse_y) = mouse_location.x = (mouse_location.x + dx).clamp(min_x, max_x - 1.);
clamp_to_screen_space(mouse_location.x, mouse_location.y, dx, dy); mouse_location.y = (mouse_location.y + dy).clamp(min_y, max_y - 1.);
mouse_location.x = new_mouse_x;
mouse_location.y = new_mouse_y;
let mut event_type = CGEventType::MouseMoved; let mut event_type = CGEventType::MouseMoved;
if self.button_state.left { if self.button_state.left {
@@ -384,25 +279,11 @@ impl Emulation for MacOSEmulation {
match state { match state {
// pressed // pressed
1 => self.spawn_repeat_task(code).await, 1 => self.spawn_repeat_task(code).await,
_ => self.cancel_repeat_task().await, _ => self.kill_repeat_task(),
} }
update_modifiers(&self.modifier_state, key, state); key_event(self.event_source.clone(), code, state)
key_event(
self.event_source.clone(),
code,
state,
self.modifier_state.get(),
);
}
KeyboardEvent::Modifiers {
depressed,
latched,
locked,
group,
} => {
set_modifiers(&self.modifier_state, depressed, latched, locked, group);
modifier_event(self.event_source.clone(), self.modifier_state.get());
} }
KeyboardEvent::Modifiers { .. } => {}
}, },
} }
// FIXME // FIXME
@@ -415,81 +296,3 @@ impl Emulation for MacOSEmulation {
async fn terminate(&mut self) {} async fn terminate(&mut self) {}
} }
fn update_modifiers(modifiers: &Cell<XMods>, key: u32, state: u8) -> bool {
if let Ok(key) = scancode::Linux::try_from(key) {
let mask = match key {
scancode::Linux::KeyLeftShift | scancode::Linux::KeyRightShift => XMods::ShiftMask,
scancode::Linux::KeyCapsLock => XMods::LockMask,
scancode::Linux::KeyLeftCtrl | scancode::Linux::KeyRightCtrl => XMods::ControlMask,
scancode::Linux::KeyLeftAlt | scancode::Linux::KeyRightalt => XMods::Mod1Mask,
scancode::Linux::KeyLeftMeta | scancode::Linux::KeyRightmeta => XMods::Mod4Mask,
_ => XMods::empty(),
};
// unchanged
if mask.is_empty() {
return false;
}
let mut mods = modifiers.get();
match state {
1 => mods.insert(mask),
_ => mods.remove(mask),
}
modifiers.set(mods);
true
} else {
false
}
}
fn set_modifiers(
active_modifiers: &Cell<XMods>,
depressed: u32,
latched: u32,
locked: u32,
group: u32,
) {
let depressed = XMods::from_bits(depressed).unwrap_or_default();
let _latched = XMods::from_bits(latched).unwrap_or_default();
let _locked = XMods::from_bits(locked).unwrap_or_default();
let _group = XMods::from_bits(group).unwrap_or_default();
// we only care about the depressed modifiers for now
active_modifiers.replace(depressed);
}
fn to_cgevent_flags(depressed: XMods) -> CGEventFlags {
let mut flags = CGEventFlags::empty();
if depressed.contains(XMods::ShiftMask) {
flags |= CGEventFlags::CGEventFlagShift;
}
if depressed.contains(XMods::LockMask) {
flags |= CGEventFlags::CGEventFlagAlphaShift;
}
if depressed.contains(XMods::ControlMask) {
flags |= CGEventFlags::CGEventFlagControl;
}
if depressed.contains(XMods::Mod1Mask) {
flags |= CGEventFlags::CGEventFlagAlternate;
}
if depressed.contains(XMods::Mod4Mask) {
flags |= CGEventFlags::CGEventFlagCommand;
}
flags
}
// From X11/X.h
bitflags! {
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct XMods: u32 {
const ShiftMask = (1<<0);
const LockMask = (1<<1);
const ControlMask = (1<<2);
const Mod1Mask = (1<<3);
const Mod2Mask = (1<<4);
const Mod3Mask = (1<<5);
const Mod4Mask = (1<<6);
const Mod5Mask = (1<<7);
}
}

View File

@@ -4,6 +4,7 @@ use ashpd::{
PersistMode, Session, PersistMode, Session,
}, },
zbus::AsyncDrop, zbus::AsyncDrop,
WindowIdentifier,
}; };
use async_trait::async_trait; use async_trait::async_trait;
@@ -42,7 +43,10 @@ impl<'a> DesktopPortalEmulation<'a> {
.await?; .await?;
log::info!("requesting permission for input emulation"); log::info!("requesting permission for input emulation");
let _devices = proxy.start(&session, None).await?.response()?; let _devices = proxy
.start(&session, &WindowIdentifier::default())
.await?
.response()?;
log::debug!("started session"); log::debug!("started session");
let session = session; let session = session;

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "input-event" name = "input-event"
description = "cross-platform input-event types for input-capture / input-emulation" description = "cross-platform input-event types for input-capture / input-emulation"
version = "0.3.0" version = "0.2.1"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -11,10 +11,10 @@ futures-core = "0.3.30"
log = "0.4.22" log = "0.4.22"
num_enum = "0.7.2" num_enum = "0.7.2"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
thiserror = "2.0.0" thiserror = "1.0.61"
[target.'cfg(all(unix, not(target_os="macos")))'.dependencies] [target.'cfg(all(unix, not(target_os="macos")))'.dependencies]
reis = { version = "0.4", optional = true } reis = { version = "0.2.0", optional = true }
[features] [features]
default = ["libei"] default = ["libei"]

View File

@@ -1,14 +1,14 @@
[package] [package]
name = "lan-mouse-cli" name = "lan-mouse-cli"
description = "CLI Frontend for lan-mouse" description = "CLI Frontend for lan-mouse"
version = "0.2.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
[dependencies] [dependencies]
futures = "0.3.30" futures = "0.3.30"
lan-mouse-ipc = { path = "../lan-mouse-ipc", version = "0.2.0" } lan-mouse-ipc = { path = "../lan-mouse-ipc", version = "0.1.0" }
tokio = { version = "1.32.0", features = [ tokio = { version = "1.32.0", features = [
"io-util", "io-util",
"io-std", "io-std",

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "lan-mouse-gtk" name = "lan-mouse-gtk"
description = "GTK4 / Libadwaita Frontend for lan-mouse" description = "GTK4 / Libadwaita Frontend for lan-mouse"
version = "0.2.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -12,7 +12,7 @@ adw = { package = "libadwaita", version = "0.7.0", features = ["v1_1"] }
async-channel = { version = "2.1.1" } async-channel = { version = "2.1.1" }
hostname = "0.4.0" hostname = "0.4.0"
log = "0.4.20" log = "0.4.20"
lan-mouse-ipc = { path = "../lan-mouse-ipc", version = "0.2.0" } lan-mouse-ipc = { path = "../lan-mouse-ipc", version = "0.1.0" }
[build-dependencies] [build-dependencies]
glib-build-tools = { version = "0.20.0" } glib-build-tools = { version = "0.20.0" }

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "lan-mouse-ipc" name = "lan-mouse-ipc"
description = "library for communication between lan-mouse service and frontends" description = "library for communication between lan-mouse service and frontends"
version = "0.2.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
@@ -11,6 +11,6 @@ futures = "0.3.30"
log = "0.4.22" log = "0.4.22"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.107" serde_json = "1.0.107"
thiserror = "2.0.0" thiserror = "1.0.63"
tokio = { version = "1.32.0", features = ["net", "io-util", "time"] } tokio = { version = "1.32.0", features = ["net", "io-util", "time"] }
tokio-stream = { version = "0.1.15", features = ["io-util"] } tokio-stream = { version = "0.1.15", features = ["io-util"] }

View File

@@ -1,13 +1,13 @@
[package] [package]
name = "lan-mouse-proto" name = "lan-mouse-proto"
description = "network protocol for lan-mouse" description = "network protocol for lan-mouse"
version = "0.2.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
repository = "https://github.com/feschber/lan-mouse" repository = "https://github.com/feschber/lan-mouse"
[dependencies] [dependencies]
num_enum = "0.7.2" num_enum = "0.7.2"
thiserror = "2.0.0" thiserror = "1.0.61"
input-event = { path = "../input-event", version = "0.3.0" } input-event = { path = "../input-event", version = "0.2.1" }
paste = "1.0" paste = "1.0"

View File

@@ -44,9 +44,6 @@ rustPlatform.buildRustPackage {
postInstall = '' postInstall = ''
wrapProgram "$out/bin/lan-mouse" \ wrapProgram "$out/bin/lan-mouse" \
--set GDK_PIXBUF_MODULE_FILE ${pkgs.librsvg.out}/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache --set GDK_PIXBUF_MODULE_FILE ${pkgs.librsvg.out}/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
install -Dm444 *.desktop -t $out/share/applications
install -Dm444 lan-mouse-gtk/resources/*.svg -t $out/share/icons/hicolor/scalable/apps
''; '';
meta = with lib; { meta = with lib; {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

View File

@@ -11,7 +11,6 @@ pub async fn run(config: Config) -> Result<(), InputCaptureError> {
let mut input_capture = InputCapture::new(backend).await?; let mut input_capture = InputCapture::new(backend).await?;
log::info!("creating clients"); log::info!("creating clients");
input_capture.create(0, Position::Left).await?; input_capture.create(0, Position::Left).await?;
input_capture.create(4, Position::Left).await?;
input_capture.create(1, Position::Right).await?; input_capture.create(1, Position::Right).await?;
input_capture.create(2, Position::Top).await?; input_capture.create(2, Position::Top).await?;
input_capture.create(3, Position::Bottom).await?; input_capture.create(3, Position::Bottom).await?;
@@ -29,13 +28,12 @@ async fn do_capture(input_capture: &mut InputCapture) -> Result<(), CaptureError
.await .await
.ok_or(CaptureError::EndOfStream)??; .ok_or(CaptureError::EndOfStream)??;
let pos = match client { let pos = match client {
0 | 4 => Position::Left, 0 => Position::Left,
1 => Position::Right, 1 => Position::Right,
2 => Position::Top, 2 => Position::Top,
3 => Position::Bottom, _ => Position::Bottom,
_ => panic!(),
}; };
log::info!("position: {client} ({pos}), event: {event}"); log::info!("position: {pos}, event: {event}");
if let CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key: 1, .. })) = event { if let CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key: 1, .. })) = event {
input_capture.release().await?; input_capture.release().await?;
break Ok(()); break Ok(());

View File

@@ -85,33 +85,27 @@ struct CliArgs {
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)] #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)]
pub enum CaptureBackend { pub enum CaptureBackend {
#[cfg(all(unix, feature = "libei_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[serde(rename = "input-capture-portal")]
InputCapturePortal, InputCapturePortal,
#[cfg(all(unix, feature = "layer_shell_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[serde(rename = "layer-shell")]
LayerShell, LayerShell,
#[cfg(all(unix, feature = "x11_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
#[serde(rename = "x11")]
X11, X11,
#[cfg(windows)] #[cfg(windows)]
#[serde(rename = "windows")]
Windows, Windows,
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[serde(rename = "macos")]
MacOs, MacOs,
#[serde(rename = "dummy")]
Dummy, Dummy,
} }
impl Display for CaptureBackend { impl Display for CaptureBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
#[cfg(all(unix, feature = "libei_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
CaptureBackend::InputCapturePortal => write!(f, "input-capture-portal"), CaptureBackend::InputCapturePortal => write!(f, "input-capture-portal"),
#[cfg(all(unix, feature = "layer_shell_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
CaptureBackend::LayerShell => write!(f, "layer-shell"), CaptureBackend::LayerShell => write!(f, "layer-shell"),
#[cfg(all(unix, feature = "x11_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
CaptureBackend::X11 => write!(f, "X11"), CaptureBackend::X11 => write!(f, "X11"),
#[cfg(windows)] #[cfg(windows)]
CaptureBackend::Windows => write!(f, "windows"), CaptureBackend::Windows => write!(f, "windows"),
@@ -125,11 +119,11 @@ impl Display for CaptureBackend {
impl From<CaptureBackend> for input_capture::Backend { impl From<CaptureBackend> for input_capture::Backend {
fn from(backend: CaptureBackend) -> Self { fn from(backend: CaptureBackend) -> Self {
match backend { match backend {
#[cfg(all(unix, feature = "libei_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
CaptureBackend::InputCapturePortal => Self::InputCapturePortal, CaptureBackend::InputCapturePortal => Self::InputCapturePortal,
#[cfg(all(unix, feature = "layer_shell_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
CaptureBackend::LayerShell => Self::LayerShell, CaptureBackend::LayerShell => Self::LayerShell,
#[cfg(all(unix, feature = "x11_capture", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
CaptureBackend::X11 => Self::X11, CaptureBackend::X11 => Self::X11,
#[cfg(windows)] #[cfg(windows)]
CaptureBackend::Windows => Self::Windows, CaptureBackend::Windows => Self::Windows,
@@ -142,38 +136,31 @@ impl From<CaptureBackend> for input_capture::Backend {
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)] #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, ValueEnum)]
pub enum EmulationBackend { pub enum EmulationBackend {
#[cfg(all(unix, feature = "wlroots_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[serde(rename = "wlroots")]
Wlroots, Wlroots,
#[cfg(all(unix, feature = "libei_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[serde(rename = "libei")]
Libei, Libei,
#[cfg(all(unix, feature = "rdp_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
#[serde(rename = "xdp")]
Xdp, Xdp,
#[cfg(all(unix, feature = "x11_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
#[serde(rename = "x11")]
X11, X11,
#[cfg(windows)] #[cfg(windows)]
#[serde(rename = "windows")]
Windows, Windows,
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[serde(rename = "macos")]
MacOs, MacOs,
#[serde(rename = "dummy")]
Dummy, Dummy,
} }
impl From<EmulationBackend> for input_emulation::Backend { impl From<EmulationBackend> for input_emulation::Backend {
fn from(backend: EmulationBackend) -> Self { fn from(backend: EmulationBackend) -> Self {
match backend { match backend {
#[cfg(all(unix, feature = "wlroots_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
EmulationBackend::Wlroots => Self::Wlroots, EmulationBackend::Wlroots => Self::Wlroots,
#[cfg(all(unix, feature = "libei_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
EmulationBackend::Libei => Self::Libei, EmulationBackend::Libei => Self::Libei,
#[cfg(all(unix, feature = "rdp_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
EmulationBackend::Xdp => Self::Xdp, EmulationBackend::Xdp => Self::Xdp,
#[cfg(all(unix, feature = "x11_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
EmulationBackend::X11 => Self::X11, EmulationBackend::X11 => Self::X11,
#[cfg(windows)] #[cfg(windows)]
EmulationBackend::Windows => Self::Windows, EmulationBackend::Windows => Self::Windows,
@@ -187,13 +174,13 @@ impl From<EmulationBackend> for input_emulation::Backend {
impl Display for EmulationBackend { impl Display for EmulationBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
#[cfg(all(unix, feature = "wlroots_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
EmulationBackend::Wlroots => write!(f, "wlroots"), EmulationBackend::Wlroots => write!(f, "wlroots"),
#[cfg(all(unix, feature = "libei_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
EmulationBackend::Libei => write!(f, "libei"), EmulationBackend::Libei => write!(f, "libei"),
#[cfg(all(unix, feature = "rdp_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
EmulationBackend::Xdp => write!(f, "xdg-desktop-portal"), EmulationBackend::Xdp => write!(f, "xdg-desktop-portal"),
#[cfg(all(unix, feature = "x11_emulation", not(target_os = "macos")))] #[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
EmulationBackend::X11 => write!(f, "X11"), EmulationBackend::X11 => write!(f, "X11"),
#[cfg(windows)] #[cfg(windows)]
EmulationBackend::Windows => write!(f, "windows"), EmulationBackend::Windows => write!(f, "windows"),
@@ -206,9 +193,7 @@ impl Display for EmulationBackend {
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, ValueEnum)] #[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, ValueEnum)]
pub enum Frontend { pub enum Frontend {
#[serde(rename = "gtk")]
Gtk, Gtk,
#[serde(rename = "cli")]
Cli, Cli,
} }