mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-08 12:30:00 +03:00
Compare commits
1 Commits
fix-wl-obj
...
cleanup-se
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c7bf3807c |
4
.github/workflows/pre-release.yml
vendored
4
.github/workflows/pre-release.yml
vendored
@@ -55,9 +55,7 @@ jobs:
|
||||
# choco install msys2
|
||||
# choco install visualstudio2022-workload-vctools
|
||||
# choco install pkgconfiglite
|
||||
py -m venv .venv
|
||||
.venv\Scripts\activate.ps1
|
||||
py -m pip install gvsbuild
|
||||
pipx install gvsbuild
|
||||
# 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\bin" "C:\Program Files\Git\notbin"
|
||||
|
||||
4
.github/workflows/rust.yml
vendored
4
.github/workflows/rust.yml
vendored
@@ -66,9 +66,7 @@ jobs:
|
||||
# choco install msys2
|
||||
# choco install visualstudio2022-workload-vctools
|
||||
# choco install pkgconfiglite
|
||||
py -m venv .venv
|
||||
.venv\Scripts\activate.ps1
|
||||
py -m pip install gvsbuild
|
||||
pipx install gvsbuild
|
||||
# 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\bin" "C:\Program Files\Git\notbin"
|
||||
|
||||
4
.github/workflows/tagged-release.yml
vendored
4
.github/workflows/tagged-release.yml
vendored
@@ -51,9 +51,7 @@ jobs:
|
||||
# choco install msys2
|
||||
# choco install visualstudio2022-workload-vctools
|
||||
# choco install pkgconfiglite
|
||||
py -m venv .venv
|
||||
.venv\Scripts\activate.ps1
|
||||
py -m pip install gvsbuild
|
||||
pipx install gvsbuild
|
||||
# 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\bin" "C:\Program Files\Git\notbin"
|
||||
|
||||
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1226,7 +1226,6 @@ version = "0.2.1"
|
||||
dependencies = [
|
||||
"ashpd",
|
||||
"async-trait",
|
||||
"bitflags 2.6.0",
|
||||
"core-graphics",
|
||||
"futures",
|
||||
"input-event",
|
||||
|
||||
@@ -20,7 +20,7 @@ Focus lies on performance and a clean, manageable implementation that can easily
|
||||
|
||||
***blazingly fast™*** because it's written in rust.
|
||||
|
||||
For an alternative (with slightly different goals) you may check out [Synergy 1 Community Edition](https://github.com/symless/synergy) or [Input Leap](https://github.com/input-leap) (Synergy fork).
|
||||
For an alternative (with slightly different goals) you may check out [Input Leap](https://github.com/input-leap).
|
||||
|
||||
|
||||
> [!WARNING]
|
||||
@@ -94,13 +94,6 @@ paru -S lan-mouse-bin
|
||||
- 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)
|
||||
|
||||
### Building from source with Nix
|
||||
In the root of the project to build Lan Mouse, run
|
||||
```sh
|
||||
nix-build
|
||||
```
|
||||
|
||||
You can find the executable in `result/bin/lan-mouse` .
|
||||
|
||||
### Manual Installation
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
}:
|
||||
pkgs.callPackage nix/default.nix { }
|
||||
46
flake.lock
generated
46
flake.lock
generated
@@ -1,12 +1,30 @@
|
||||
{
|
||||
"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": {
|
||||
"locked": {
|
||||
"lastModified": 1728018373,
|
||||
"narHash": "sha256-NOiTvBbRLIOe5F6RbHaAh6++BNjsb149fGZd1T4+KBg=",
|
||||
"lastModified": 1716293225,
|
||||
"narHash": "sha256-pU9ViBVE3XYb70xZx+jK6SEVphvt7xMTbm6yDIF4xPs=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "bc947f541ae55e999ffdb4013441347d83b00feb",
|
||||
"rev": "3eaeaeb6b1e08a016380c279f8846e0bd8808916",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -24,16 +42,17 @@
|
||||
},
|
||||
"rust-overlay": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1728181869,
|
||||
"narHash": "sha256-sQXHXsjIcGEoIHkB+RO6BZdrPfB+43V1TEpyoWRI3ww=",
|
||||
"lastModified": 1716257780,
|
||||
"narHash": "sha256-R+NjvJzKEkTVCmdrKRfPE4liX/KMGVqGUwwS5H8ET8A=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "cd46aa3906c14790ef5cbe278d9e54f2c38f95c0",
|
||||
"rev": "4e5e3d2c5c9b2721bd266f9e43c14e96811b89d2",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -41,6 +60,21 @@
|
||||
"repo": "rust-overlay",
|
||||
"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",
|
||||
|
||||
@@ -8,7 +8,7 @@ use futures_core::Stream;
|
||||
use input_event::PointerEvent;
|
||||
use tokio::time::{self, Instant, Interval};
|
||||
|
||||
use super::{Capture, CaptureError, CaptureEvent, Position};
|
||||
use super::{Capture, CaptureError, CaptureEvent, CaptureHandle, Position};
|
||||
|
||||
pub struct DummyInputCapture {
|
||||
start: Option<Instant>,
|
||||
@@ -34,11 +34,11 @@ impl Default for DummyInputCapture {
|
||||
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, _pos: Position) -> Result<(), CaptureError> {
|
||||
async fn destroy(&mut self, _handle: CaptureHandle) -> Result<(), CaptureError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ const FREQUENCY_HZ: f64 = 1.0;
|
||||
const RADIUS: f64 = 100.0;
|
||||
|
||||
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>> {
|
||||
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))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ pub enum CaptureCreationError {
|
||||
#[error("error creating windows capture backend")]
|
||||
Windows,
|
||||
#[cfg(target_os = "macos")]
|
||||
#[error("error creating macos capture backend: `{0}`")]
|
||||
#[error("error creating macos capture backend")]
|
||||
MacOS(#[from] MacosCaptureCreationError),
|
||||
}
|
||||
|
||||
@@ -165,9 +165,6 @@ pub enum X11InputCaptureCreationError {
|
||||
pub enum MacosCaptureCreationError {
|
||||
#[error("event source creation failed!")]
|
||||
EventSourceCreation,
|
||||
#[cfg(target_os = "macos")]
|
||||
#[error("event tap creation failed")]
|
||||
EventTapCreation,
|
||||
#[error("failed to set CG Cursor property")]
|
||||
CGCursorProperty,
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
fmt::Display,
|
||||
mem::swap,
|
||||
task::{ready, Poll},
|
||||
};
|
||||
use std::{collections::HashSet, fmt::Display, task::Poll};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt;
|
||||
@@ -117,48 +112,19 @@ impl Display for Backend {
|
||||
}
|
||||
|
||||
pub struct InputCapture {
|
||||
/// capture backend
|
||||
capture: Box<dyn Capture>,
|
||||
/// keys pressed by active capture
|
||||
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 {
|
||||
/// create a new client with the given id
|
||||
pub async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||
if let Some(v) = self.position_map.get_mut(&pos) {
|
||||
v.push(id);
|
||||
Ok(())
|
||||
} else {
|
||||
self.position_map.insert(pos, vec![id]);
|
||||
self.id_map.insert(id, pos);
|
||||
self.capture.create(pos).await
|
||||
}
|
||||
self.capture.create(id, pos).await
|
||||
}
|
||||
|
||||
/// destroy the client with the given id, if it exists
|
||||
pub async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError> {
|
||||
if let Some(pos) = self.id_map.remove(&id) {
|
||||
let destroy = if let Some(v) = self.position_map.get_mut(&pos) {
|
||||
v.retain(|&i| i != id);
|
||||
// we were the last id registered at this position
|
||||
v.is_empty()
|
||||
} else {
|
||||
// nothing to destroy
|
||||
false
|
||||
};
|
||||
if destroy {
|
||||
self.position_map.remove(&pos);
|
||||
self.capture.destroy(pos).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
self.capture.destroy(id).await
|
||||
}
|
||||
|
||||
/// release mouse
|
||||
@@ -177,9 +143,6 @@ impl InputCapture {
|
||||
let capture = create(backend).await?;
|
||||
Ok(Self {
|
||||
capture,
|
||||
id_map: Default::default(),
|
||||
pending: Default::default(),
|
||||
position_map: Default::default(),
|
||||
pressed_keys: HashSet::new(),
|
||||
})
|
||||
}
|
||||
@@ -207,65 +170,29 @@ impl Stream for InputCapture {
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> Poll<Option<Self::Item>> {
|
||||
if let Some(e) = self.pending.pop_front() {
|
||||
return Poll::Ready(Some(Ok(e)));
|
||||
}
|
||||
|
||||
// ready
|
||||
let event = ready!(self.capture.poll_next_unpin(cx));
|
||||
|
||||
// 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);
|
||||
match self.capture.poll_next_unpin(cx) {
|
||||
Poll::Ready(e) => {
|
||||
if let Some(Ok((
|
||||
_,
|
||||
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key, state, .. })),
|
||||
))) = e
|
||||
{
|
||||
for &id in position_map.get(&pos).expect("position") {
|
||||
self.pending.push_back((id, event));
|
||||
}
|
||||
self.update_pressed_keys(key, state);
|
||||
}
|
||||
swap(&mut self.position_map, &mut position_map);
|
||||
|
||||
Poll::Ready(Some(Ok(self.pending.pop_front().expect("event"))))
|
||||
Poll::Ready(e)
|
||||
}
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
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
|
||||
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError>;
|
||||
async fn destroy(&mut self, id: CaptureHandle) -> Result<(), CaptureError>;
|
||||
|
||||
/// release mouse
|
||||
async fn release(&mut self) -> Result<(), CaptureError>;
|
||||
@@ -277,14 +204,14 @@ trait Capture: Stream<Item = Result<(Position, CaptureEvent), CaptureError>> + U
|
||||
async fn create_backend(
|
||||
backend: Backend,
|
||||
) -> Result<
|
||||
Box<dyn Capture<Item = Result<(Position, CaptureEvent), CaptureError>>>,
|
||||
Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
|
||||
CaptureCreationError,
|
||||
> {
|
||||
match backend {
|
||||
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
|
||||
Backend::InputCapturePortal => Ok(Box::new(libei::LibeiInputCapture::new().await?)),
|
||||
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
|
||||
Backend::LayerShell => Ok(Box::new(wayland::LayerShellInputCapture::new()?)),
|
||||
Backend::LayerShell => Ok(Box::new(wayland::WaylandInputCapture::new()?)),
|
||||
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
|
||||
Backend::X11 => Ok(Box::new(x11::X11InputCapture::new()?)),
|
||||
#[cfg(windows)]
|
||||
@@ -298,7 +225,7 @@ async fn create_backend(
|
||||
async fn create(
|
||||
backend: Option<Backend>,
|
||||
) -> Result<
|
||||
Box<dyn Capture<Item = Result<(Position, CaptureEvent), CaptureError>>>,
|
||||
Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
|
||||
CaptureCreationError,
|
||||
> {
|
||||
if let Some(backend) = backend {
|
||||
|
||||
@@ -40,7 +40,7 @@ use crate::CaptureEvent;
|
||||
|
||||
use super::{
|
||||
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
|
||||
@@ -50,15 +50,15 @@ use super::{
|
||||
/// events that necessitate restarting the capture session
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum LibeiNotifyEvent {
|
||||
Create(Position),
|
||||
Destroy(Position),
|
||||
Create(CaptureHandle, Position),
|
||||
Destroy(CaptureHandle),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct LibeiInputCapture<'a> {
|
||||
input_capture: Pin<Box<InputCapture<'a>>>,
|
||||
capture_task: JoinHandle<Result<(), CaptureError>>,
|
||||
event_rx: Receiver<(Position, CaptureEvent)>,
|
||||
event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
|
||||
notify_capture: Sender<LibeiNotifyEvent>,
|
||||
notify_release: Arc<Notify>,
|
||||
cancellation_token: CancellationToken,
|
||||
@@ -117,13 +117,13 @@ impl From<ICBarrier> for Barrier {
|
||||
|
||||
fn select_barriers(
|
||||
zones: &Zones,
|
||||
clients: &[Position],
|
||||
clients: &[(CaptureHandle, Position)],
|
||||
next_barrier_id: &mut u32,
|
||||
) -> (Vec<ICBarrier>, HashMap<BarrierID, Position>) {
|
||||
let mut pos_for_barrier = HashMap::new();
|
||||
) -> (Vec<ICBarrier>, HashMap<BarrierID, CaptureHandle>) {
|
||||
let mut client_for_barrier = HashMap::new();
|
||||
let mut barriers: Vec<ICBarrier> = vec![];
|
||||
|
||||
for pos in clients {
|
||||
for (handle, pos) in clients {
|
||||
let mut client_barriers = zones
|
||||
.regions()
|
||||
.iter()
|
||||
@@ -131,21 +131,21 @@ fn select_barriers(
|
||||
let id = *next_barrier_id;
|
||||
*next_barrier_id = id + 1;
|
||||
let position = pos_to_barrier(r, *pos);
|
||||
pos_for_barrier.insert(id, *pos);
|
||||
client_for_barrier.insert(id, *handle);
|
||||
ICBarrier::new(id, position)
|
||||
})
|
||||
.collect();
|
||||
barriers.append(&mut client_barriers);
|
||||
}
|
||||
(barriers, pos_for_barrier)
|
||||
(barriers, client_for_barrier)
|
||||
}
|
||||
|
||||
async fn update_barriers(
|
||||
input_capture: &InputCapture<'_>,
|
||||
session: &Session<'_, InputCapture<'_>>,
|
||||
active_clients: &[Position],
|
||||
active_clients: &[(CaptureHandle, Position)],
|
||||
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()?;
|
||||
log::debug!("zones: {zones:?}");
|
||||
|
||||
@@ -203,9 +203,9 @@ async fn connect_to_eis(
|
||||
async fn libei_event_handler(
|
||||
mut ei_event_stream: EiConvertEventStream,
|
||||
context: ei::Context,
|
||||
event_tx: Sender<(Position, CaptureEvent)>,
|
||||
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
|
||||
release_session: Arc<Notify>,
|
||||
current_pos: Rc<Cell<Option<Position>>>,
|
||||
current_client: Rc<Cell<Option<CaptureHandle>>>,
|
||||
) -> Result<(), CaptureError> {
|
||||
loop {
|
||||
let ei_event = ei_event_stream
|
||||
@@ -214,7 +214,7 @@ async fn libei_event_handler(
|
||||
.ok_or(CaptureError::EndOfStream)?
|
||||
.map_err(ReisConvertEventStreamError::from)?;
|
||||
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?;
|
||||
}
|
||||
}
|
||||
@@ -260,14 +260,14 @@ async fn do_capture(
|
||||
mut capture_event: Receiver<LibeiNotifyEvent>,
|
||||
notify_release: Arc<Notify>,
|
||||
session: Option<(Session<'_, InputCapture<'_>>, BitFlags<Capabilities>)>,
|
||||
event_tx: Sender<(Position, CaptureEvent)>,
|
||||
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> Result<(), CaptureError> {
|
||||
let mut session = session.map(|s| s.0);
|
||||
|
||||
/* safety: libei_task does not outlive Self */
|
||||
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 = 1u32;
|
||||
|
||||
let mut zones_changed = input_capture.receive_zones_changed().await?;
|
||||
@@ -341,8 +341,8 @@ async fn do_capture(
|
||||
// update clients if requested
|
||||
if let Some(event) = capture_event_occured.take() {
|
||||
match event {
|
||||
LibeiNotifyEvent::Create(p) => active_clients.push(p),
|
||||
LibeiNotifyEvent::Destroy(p) => active_clients.retain(|&pos| pos != p),
|
||||
LibeiNotifyEvent::Create(c, p) => active_clients.push((c, p)),
|
||||
LibeiNotifyEvent::Destroy(c) => active_clients.retain(|(h, _)| *h != c),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,21 +356,21 @@ async fn do_capture(
|
||||
async fn do_capture_session(
|
||||
input_capture: &InputCapture<'_>,
|
||||
session: &mut Session<'_, InputCapture<'_>>,
|
||||
event_tx: &Sender<(Position, CaptureEvent)>,
|
||||
active_clients: &[Position],
|
||||
event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
|
||||
active_clients: &[(CaptureHandle, Position)],
|
||||
next_barrier_id: &mut u32,
|
||||
notify_release: &Notify,
|
||||
cancel: (CancellationToken, CancellationToken),
|
||||
) -> Result<(), CaptureError> {
|
||||
let (cancel_session, cancel_update) = cancel;
|
||||
// current client
|
||||
let current_pos = Rc::new(Cell::new(None));
|
||||
let current_client = Rc::new(Cell::new(None));
|
||||
|
||||
// connect to eis server
|
||||
let (context, ei_event_stream) = connect_to_eis(input_capture, session).await?;
|
||||
|
||||
// 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?;
|
||||
|
||||
log::debug!("enabling session");
|
||||
@@ -382,7 +382,7 @@ async fn do_capture_session(
|
||||
// async event task
|
||||
let cancel_ei_handler = CancellationToken::new();
|
||||
let event_chan = event_tx.clone();
|
||||
let pos = current_pos.clone();
|
||||
let client = current_client.clone();
|
||||
let cancel_session_clone = cancel_session.clone();
|
||||
let release_session_clone = release_session.clone();
|
||||
let cancel_ei_handler_clone = cancel_ei_handler.clone();
|
||||
@@ -393,7 +393,7 @@ async fn do_capture_session(
|
||||
context,
|
||||
event_chan,
|
||||
release_session_clone,
|
||||
pos,
|
||||
client,
|
||||
) => {
|
||||
log::debug!("libei exited: {r:?} cancelling session task");
|
||||
cancel_session_clone.cancel();
|
||||
@@ -421,11 +421,11 @@ async fn do_capture_session(
|
||||
};
|
||||
|
||||
// find client corresponding to barrier
|
||||
let pos = *pos_for_barrier_id.get(&barrier_id).expect("invalid barrier id");
|
||||
current_pos.replace(Some(pos));
|
||||
let client = *client_for_barrier_id.get(&barrier_id).expect("invalid barrier id");
|
||||
current_client.replace(Some(client));
|
||||
|
||||
// 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! {
|
||||
_ = notify_release.notified() => { /* capture release */
|
||||
@@ -441,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 */
|
||||
@@ -484,7 +484,8 @@ async fn release_capture<'a>(
|
||||
input_capture: &InputCapture<'a>,
|
||||
session: &Session<'a, InputCapture<'a>>,
|
||||
activated: Activated,
|
||||
current_pos: Position,
|
||||
current_client: CaptureHandle,
|
||||
active_clients: &[(CaptureHandle, Position)],
|
||||
) -> Result<(), CaptureError> {
|
||||
if let Some(activation_id) = activated.activation_id() {
|
||||
log::debug!("releasing input capture {activation_id}");
|
||||
@@ -493,7 +494,13 @@ async fn release_capture<'a>(
|
||||
.cursor_position()
|
||||
.expect("compositor did not report cursor position!");
|
||||
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
|
||||
Position::Left => (1., 0.),
|
||||
Position::Right => (-1., 0.),
|
||||
@@ -547,9 +554,9 @@ static ALL_CAPABILITIES: &[DeviceCapability] = &[
|
||||
|
||||
async fn handle_ei_event(
|
||||
ei_event: EiEvent,
|
||||
current_client: Option<Position>,
|
||||
current_client: Option<CaptureHandle>,
|
||||
context: &ei::Context,
|
||||
event_tx: &Sender<(Position, CaptureEvent)>,
|
||||
event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
|
||||
release_session: &Notify,
|
||||
) -> Result<(), CaptureError> {
|
||||
match ei_event {
|
||||
@@ -568,9 +575,9 @@ async fn handle_ei_event(
|
||||
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) {
|
||||
event_tx.send((pos, CaptureEvent::Input(event))).await.expect("no channel");
|
||||
event_tx.send((handle, CaptureEvent::Input(event))).await.expect("no channel");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -580,18 +587,18 @@ async fn handle_ei_event(
|
||||
|
||||
#[async_trait]
|
||||
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
|
||||
.notify_capture
|
||||
.send(LibeiNotifyEvent::Create(pos))
|
||||
.send(LibeiNotifyEvent::Create(handle, pos))
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||
async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
|
||||
let _ = self
|
||||
.notify_capture
|
||||
.send(LibeiNotifyEvent::Destroy(pos))
|
||||
.send(LibeiNotifyEvent::Destroy(handle))
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
@@ -622,7 +629,7 @@ impl<'a> Drop 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>> {
|
||||
match self.capture_task.poll_unpin(cx) {
|
||||
|
||||
@@ -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 bitflags::bitflags;
|
||||
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 libc::c_void;
|
||||
use once_cell::unsync::Lazy;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{c_char, CString};
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{ready, Context, Poll};
|
||||
use std::thread::{self};
|
||||
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||
use tokio::sync::{oneshot, Mutex};
|
||||
use tokio::sync::mpsc::{Receiver, Sender};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Bounds {
|
||||
@@ -37,44 +39,44 @@ struct Bounds {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct InputCaptureState {
|
||||
active_clients: Lazy<HashSet<Position>>,
|
||||
current_pos: Option<Position>,
|
||||
client_for_pos: Lazy<HashMap<Position, CaptureHandle>>,
|
||||
current_client: Option<(CaptureHandle, Position)>,
|
||||
bounds: Bounds,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ProducerEvent {
|
||||
Release,
|
||||
Create(Position),
|
||||
Destroy(Position),
|
||||
Grab(Position),
|
||||
Create(CaptureHandle, Position),
|
||||
Destroy(CaptureHandle),
|
||||
Grab((CaptureHandle, Position)),
|
||||
EventTapDisabled,
|
||||
}
|
||||
|
||||
impl InputCaptureState {
|
||||
fn new() -> Result<Self, MacosCaptureCreationError> {
|
||||
let mut res = Self {
|
||||
active_clients: Lazy::new(HashSet::new),
|
||||
current_pos: None,
|
||||
client_for_pos: Lazy::new(HashMap::new),
|
||||
current_client: None,
|
||||
bounds: Bounds::default(),
|
||||
};
|
||||
res.update_bounds()?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn crossed(&mut self, event: &CGEvent) -> Option<Position> {
|
||||
fn crossed(&mut self, event: &CGEvent) -> Option<(CaptureHandle, Position)> {
|
||||
let location = event.location();
|
||||
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);
|
||||
|
||||
for &position in self.active_clients.iter() {
|
||||
if (position == Position::Left && (location.x + relative_x) <= self.bounds.xmin)
|
||||
|| (position == Position::Right && (location.x + relative_x) >= self.bounds.xmax)
|
||||
|| (position == Position::Top && (location.y + relative_y) <= self.bounds.ymin)
|
||||
|| (position == Position::Bottom && (location.y + relative_y) >= self.bounds.ymax)
|
||||
for (position, client) in self.client_for_pos.iter() {
|
||||
if (position == &Position::Left && (location.x + relative_x) <= self.bounds.xmin)
|
||||
|| (position == &Position::Right && (location.x + relative_x) >= self.bounds.xmax)
|
||||
|| (position == &Position::Top && (location.y + relative_y) <= self.bounds.ymin)
|
||||
|| (position == &Position::Bottom && (location.y + relative_y) >= self.bounds.ymax)
|
||||
{
|
||||
log::debug!("Crossed barrier into position: {position:?}");
|
||||
return Some(position);
|
||||
log::debug!("Crossed barrier into client: {client}, {position:?}");
|
||||
return Some((*client, *position));
|
||||
}
|
||||
}
|
||||
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
|
||||
// random location when we exit the client
|
||||
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 edge_offset = 1.0;
|
||||
|
||||
@@ -144,31 +146,40 @@ impl InputCaptureState {
|
||||
log::debug!("handling event: {producer_event:?}");
|
||||
match producer_event {
|
||||
ProducerEvent::Release => {
|
||||
if self.current_pos.is_some() {
|
||||
if self.current_client.is_some() {
|
||||
CGDisplay::show_cursor(&CGDisplay::main())
|
||||
.map_err(CaptureError::CoreGraphics)?;
|
||||
self.current_pos = None;
|
||||
self.current_client = None;
|
||||
}
|
||||
}
|
||||
ProducerEvent::Grab(pos) => {
|
||||
if self.current_pos.is_none() {
|
||||
ProducerEvent::Grab(client) => {
|
||||
if self.current_client.is_none() {
|
||||
CGDisplay::hide_cursor(&CGDisplay::main())
|
||||
.map_err(CaptureError::CoreGraphics)?;
|
||||
self.current_pos = Some(pos);
|
||||
self.current_client = Some(client);
|
||||
}
|
||||
}
|
||||
ProducerEvent::Create(p) => {
|
||||
self.active_clients.insert(p);
|
||||
ProducerEvent::Create(c, p) => {
|
||||
self.client_for_pos.insert(p, c);
|
||||
}
|
||||
ProducerEvent::Destroy(p) => {
|
||||
if let Some(current) = self.current_pos {
|
||||
if current == p {
|
||||
CGDisplay::show_cursor(&CGDisplay::main())
|
||||
.map_err(CaptureError::CoreGraphics)?;
|
||||
self.current_pos = None;
|
||||
};
|
||||
ProducerEvent::Destroy(c) => {
|
||||
for pos in [
|
||||
Position::Left,
|
||||
Position::Right,
|
||||
Position::Top,
|
||||
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),
|
||||
};
|
||||
@@ -322,11 +333,12 @@ fn get_events(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_event_tap<'a>(
|
||||
fn event_tap_thread(
|
||||
client_state: Arc<Mutex<InputCaptureState>>,
|
||||
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
|
||||
notify_tx: Sender<ProducerEvent>,
|
||||
event_tx: Sender<(Position, CaptureEvent)>,
|
||||
) -> Result<CGEventTap<'a>, MacosCaptureCreationError> {
|
||||
exit: tokio::sync::oneshot::Sender<Result<(), &'static str>>,
|
||||
) {
|
||||
let cg_events_of_interest: Vec<CGEventType> = vec![
|
||||
CGEventType::LeftMouseDown,
|
||||
CGEventType::LeftMouseUp,
|
||||
@@ -344,11 +356,15 @@ fn create_event_tap<'a>(
|
||||
CGEventType::FlagsChanged,
|
||||
];
|
||||
|
||||
let event_tap_callback =
|
||||
move |_proxy: CGEventTapProxy, event_type: CGEventType, cg_ev: &CGEvent| {
|
||||
let tap = CGEventTap::new(
|
||||
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:?}");
|
||||
let mut state = client_state.blocking_lock();
|
||||
let mut pos = None;
|
||||
let mut client = None;
|
||||
let mut res_events = vec![];
|
||||
|
||||
if matches!(
|
||||
@@ -364,8 +380,8 @@ fn create_event_tap<'a>(
|
||||
}
|
||||
|
||||
// Are we in a client?
|
||||
if let Some(current_pos) = state.current_pos {
|
||||
pos = Some(current_pos);
|
||||
if let Some((current_client, _)) = state.current_client {
|
||||
client = Some(current_client);
|
||||
get_events(&event_type, cg_ev, &mut res_events).unwrap_or_else(|e| {
|
||||
log::error!("Failed to get events: {e}");
|
||||
});
|
||||
@@ -379,19 +395,19 @@ fn create_event_tap<'a>(
|
||||
}
|
||||
// Did we cross a barrier?
|
||||
else if matches!(event_type, CGEventType::MouseMoved) {
|
||||
if let Some(new_pos) = state.crossed(cg_ev) {
|
||||
pos = Some(new_pos);
|
||||
if let Some((new_client, pos)) = state.crossed(cg_ev) {
|
||||
client = Some(new_client);
|
||||
res_events.push(CaptureEvent::Begin);
|
||||
notify_tx
|
||||
.blocking_send(ProducerEvent::Grab(new_pos))
|
||||
.blocking_send(ProducerEvent::Grab((new_client, pos)))
|
||||
.expect("Failed to send notification");
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(pos) = pos {
|
||||
if let Some(client) = client {
|
||||
res_events.iter().for_each(|e| {
|
||||
event_tx
|
||||
.blocking_send((pos, *e))
|
||||
.blocking_send((client, *e))
|
||||
.expect("Failed to send event");
|
||||
});
|
||||
// Returning None should stop the event from being processed
|
||||
@@ -399,16 +415,9 @@ fn create_event_tap<'a>(
|
||||
cg_ev.set_type(CGEventType::Null);
|
||||
}
|
||||
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
|
||||
.mach_port
|
||||
@@ -419,43 +428,22 @@ fn create_event_tap<'a>(
|
||||
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();
|
||||
|
||||
let _ = exit.send(Err("tap thread exited"));
|
||||
}
|
||||
|
||||
pub struct MacOSInputCapture {
|
||||
event_rx: Receiver<(Position, CaptureEvent)>,
|
||||
event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
|
||||
notify_tx: Sender<ProducerEvent>,
|
||||
}
|
||||
|
||||
impl MacOSInputCapture {
|
||||
pub async fn new() -> Result<Self, MacosCaptureCreationError> {
|
||||
let state = Arc::new(Mutex::new(InputCaptureState::new()?));
|
||||
let (event_tx, event_rx) = mpsc::channel(32);
|
||||
let (notify_tx, mut notify_rx) = mpsc::channel(32);
|
||||
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
|
||||
let (tap_exit_tx, mut tap_exit_rx) = oneshot::channel();
|
||||
let (event_tx, event_rx) = tokio::sync::mpsc::channel(32);
|
||||
let (notify_tx, mut notify_rx) = tokio::sync::mpsc::channel(32);
|
||||
let (tap_exit_tx, mut tap_exit_rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
unsafe {
|
||||
configure_cf_settings()?;
|
||||
@@ -469,14 +457,10 @@ impl MacOSInputCapture {
|
||||
event_tap_thread_state,
|
||||
event_tx,
|
||||
event_tap_notify,
|
||||
ready_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 {
|
||||
loop {
|
||||
tokio::select! {
|
||||
@@ -507,21 +491,21 @@ impl MacOSInputCapture {
|
||||
|
||||
#[async_trait]
|
||||
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();
|
||||
tokio::task::spawn_local(async move {
|
||||
log::debug!("creating capture, {pos}");
|
||||
let _ = notify_tx.send(ProducerEvent::Create(pos)).await;
|
||||
log::debug!("creating client {id}, {pos}");
|
||||
let _ = notify_tx.send(ProducerEvent::Create(id, pos)).await;
|
||||
log::debug!("done !");
|
||||
});
|
||||
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();
|
||||
tokio::task::spawn_local(async move {
|
||||
log::debug!("destroying capture {pos}");
|
||||
let _ = notify_tx.send(ProducerEvent::Destroy(pos)).await;
|
||||
log::debug!("destroying client {id}");
|
||||
let _ = notify_tx.send(ProducerEvent::Destroy(id)).await;
|
||||
log::debug!("done !");
|
||||
});
|
||||
Ok(())
|
||||
@@ -542,7 +526,7 @@ impl Capture 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>> {
|
||||
match ready!(self.event_rx.poll_recv(cx)) {
|
||||
|
||||
@@ -64,7 +64,7 @@ use crate::{CaptureError, CaptureEvent};
|
||||
|
||||
use super::{
|
||||
error::{LayerShellCaptureCreationError, WaylandBindError},
|
||||
Capture, Position,
|
||||
Capture, CaptureHandle, Position,
|
||||
};
|
||||
|
||||
struct Globals {
|
||||
@@ -102,13 +102,13 @@ struct State {
|
||||
pointer_lock: Option<ZwpLockedPointerV1>,
|
||||
rel_pointer: Option<ZwpRelativePointerV1>,
|
||||
shortcut_inhibitor: Option<ZwpKeyboardShortcutsInhibitorV1>,
|
||||
active_windows: Vec<Arc<Window>>,
|
||||
focused: Option<Arc<Window>>,
|
||||
client_for_window: Vec<(Arc<Window>, CaptureHandle)>,
|
||||
focused: Option<(Arc<Window>, CaptureHandle)>,
|
||||
g: Globals,
|
||||
wayland_fd: RawFd,
|
||||
read_guard: Option<ReadEventsGuard>,
|
||||
qh: QueueHandle<Self>,
|
||||
pending_events: VecDeque<(Position, CaptureEvent)>,
|
||||
pending_events: VecDeque<(CaptureHandle, CaptureEvent)>,
|
||||
output_info: Vec<(WlOutput, OutputInfo)>,
|
||||
scroll_discrete_pending: bool,
|
||||
}
|
||||
@@ -124,7 +124,7 @@ impl AsRawFd for Inner {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LayerShellInputCapture(AsyncFd<Inner>);
|
||||
pub struct WaylandInputCapture(AsyncFd<Inner>);
|
||||
|
||||
struct Window {
|
||||
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> {
|
||||
let conn = Connection::connect_to_env()?;
|
||||
let (g, mut queue) = registry_queue_init::<State>(&conn)?;
|
||||
@@ -323,7 +323,7 @@ impl LayerShellInputCapture {
|
||||
pointer_lock: None,
|
||||
rel_pointer: None,
|
||||
shortcut_inhibitor: None,
|
||||
active_windows: Vec::new(),
|
||||
client_for_window: Vec::new(),
|
||||
focused: None,
|
||||
qh,
|
||||
wayland_fd,
|
||||
@@ -370,18 +370,23 @@ impl LayerShellInputCapture {
|
||||
|
||||
let inner = AsyncFd::new(Inner { queue, state })?;
|
||||
|
||||
Ok(LayerShellInputCapture(inner))
|
||||
Ok(WaylandInputCapture(inner))
|
||||
}
|
||||
|
||||
fn add_client(&mut self, pos: Position) {
|
||||
self.0.get_mut().state.add_client(pos);
|
||||
fn add_client(&mut self, handle: CaptureHandle, pos: Position) {
|
||||
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();
|
||||
// remove all windows corresponding to this client
|
||||
while let Some(i) = inner.state.active_windows.iter().position(|w| w.pos == pos) {
|
||||
inner.state.active_windows.remove(i);
|
||||
while let Some(i) = inner
|
||||
.state
|
||||
.client_for_window
|
||||
.iter()
|
||||
.position(|(_, c)| *c == handle)
|
||||
{
|
||||
inner.state.client_for_window.remove(i);
|
||||
inner.state.focused = None;
|
||||
}
|
||||
}
|
||||
@@ -395,7 +400,7 @@ impl State {
|
||||
serial: u32,
|
||||
qh: &QueueHandle<State>,
|
||||
) {
|
||||
let window = self.focused.as_ref().unwrap();
|
||||
let (window, _) = self.focused.as_ref().unwrap();
|
||||
|
||||
// hide the cursor
|
||||
pointer.set_cursor(serial, None, 0, 0);
|
||||
@@ -438,7 +443,7 @@ impl State {
|
||||
|
||||
fn ungrab(&mut self) {
|
||||
// get focused client
|
||||
let window = match self.focused.as_ref() {
|
||||
let (window, _client) = match self.focused.as_ref() {
|
||||
Some(focused) => focused,
|
||||
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);
|
||||
|
||||
log::debug!("outputs: {outputs:?}");
|
||||
outputs.iter().for_each(|(o, i)| {
|
||||
let window = Window::new(self, &self.qh, o, pos, i.size);
|
||||
let window = Arc::new(window);
|
||||
self.active_windows.push(window);
|
||||
self.client_for_window.push((window, client));
|
||||
});
|
||||
}
|
||||
|
||||
fn update_windows(&mut self) {
|
||||
log::debug!("updating windows");
|
||||
log::debug!("output info: {:?}", self.output_info);
|
||||
let clients: Vec<_> = self.active_windows.drain(..).map(|w| w.pos).collect();
|
||||
for pos in clients {
|
||||
self.add_client(pos);
|
||||
let clients: Vec<_> = self
|
||||
.client_for_window
|
||||
.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]
|
||||
impl Capture for LayerShellInputCapture {
|
||||
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||
self.add_client(pos);
|
||||
impl Capture for WaylandInputCapture {
|
||||
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||
self.add_client(handle, pos);
|
||||
let inner = self.0.get_mut();
|
||||
Ok(inner.flush_events()?)
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||
self.delete_client(pos);
|
||||
async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
|
||||
self.delete_client(handle);
|
||||
let inner = self.0.get_mut();
|
||||
Ok(inner.flush_events()?)
|
||||
}
|
||||
@@ -582,8 +591,8 @@ impl Capture for LayerShellInputCapture {
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for LayerShellInputCapture {
|
||||
type Item = Result<(Position, CaptureEvent), CaptureError>;
|
||||
impl Stream for WaylandInputCapture {
|
||||
type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
|
||||
|
||||
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() {
|
||||
@@ -648,16 +657,10 @@ impl Dispatch<wl_seat::WlSeat, ()> for State {
|
||||
capabilities: WEnum::Value(capabilities),
|
||||
} = event
|
||||
{
|
||||
if capabilities.contains(wl_seat::Capability::Pointer) {
|
||||
if let Some(p) = state.pointer.take() {
|
||||
p.release();
|
||||
}
|
||||
if capabilities.contains(wl_seat::Capability::Pointer) && state.pointer.is_none() {
|
||||
state.pointer.replace(seat.get_pointer(qh, ()));
|
||||
}
|
||||
if capabilities.contains(wl_seat::Capability::Keyboard) {
|
||||
if let Some(k) = state.keyboard.take() {
|
||||
k.release();
|
||||
}
|
||||
if capabilities.contains(wl_seat::Capability::Keyboard) && state.keyboard.is_none() {
|
||||
seat.get_keyboard(qh, ());
|
||||
}
|
||||
}
|
||||
@@ -682,20 +685,23 @@ impl Dispatch<WlPointer, ()> for State {
|
||||
} => {
|
||||
// get client corresponding to the focused surface
|
||||
{
|
||||
if let Some(window) = app.active_windows.iter().find(|w| w.surface == surface) {
|
||||
app.focused = Some(window.clone());
|
||||
if let Some((window, client)) = app
|
||||
.client_for_window
|
||||
.iter()
|
||||
.find(|(w, _c)| w.surface == surface)
|
||||
{
|
||||
app.focused = Some((window.clone(), *client));
|
||||
app.grab(&surface, pointer, serial, qh);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let pos = app
|
||||
.active_windows
|
||||
let (_, client) = app
|
||||
.client_for_window
|
||||
.iter()
|
||||
.find(|w| w.surface == surface)
|
||||
.map(|w| w.pos)
|
||||
.find(|(w, _c)| w.surface == surface)
|
||||
.unwrap();
|
||||
app.pending_events.push_back((pos, CaptureEvent::Begin));
|
||||
app.pending_events.push_back((*client, CaptureEvent::Begin));
|
||||
}
|
||||
wl_pointer::Event::Leave { .. } => {
|
||||
/* There are rare cases, where when a window is opened in
|
||||
@@ -716,9 +722,9 @@ impl Dispatch<WlPointer, ()> for State {
|
||||
button,
|
||||
state,
|
||||
} => {
|
||||
let window = app.focused.as_ref().unwrap();
|
||||
let (_, client) = app.focused.as_ref().unwrap();
|
||||
app.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
|
||||
time,
|
||||
button,
|
||||
@@ -727,7 +733,7 @@ impl Dispatch<WlPointer, ()> for State {
|
||||
));
|
||||
}
|
||||
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 {
|
||||
// each axisvalue120 event is coupled with
|
||||
// a corresponding axis event, which needs to
|
||||
@@ -735,7 +741,7 @@ impl Dispatch<WlPointer, ()> for State {
|
||||
app.scroll_discrete_pending = false;
|
||||
} else {
|
||||
app.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Pointer(PointerEvent::Axis {
|
||||
time,
|
||||
axis: u32::from(axis) as u8,
|
||||
@@ -745,10 +751,10 @@ impl Dispatch<WlPointer, ()> for State {
|
||||
}
|
||||
}
|
||||
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.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Pointer(PointerEvent::AxisDiscrete120 {
|
||||
axis: u32::from(axis) as u8,
|
||||
value: value120,
|
||||
@@ -774,7 +780,10 @@ impl Dispatch<WlKeyboard, ()> for State {
|
||||
_: &Connection,
|
||||
_: &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 {
|
||||
wl_keyboard::Event::Key {
|
||||
serial: _,
|
||||
@@ -782,9 +791,9 @@ impl Dispatch<WlKeyboard, ()> for State {
|
||||
key,
|
||||
state,
|
||||
} => {
|
||||
if let Some(window) = window {
|
||||
if let Some(client) = client {
|
||||
app.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key {
|
||||
time,
|
||||
key,
|
||||
@@ -800,9 +809,9 @@ impl Dispatch<WlKeyboard, ()> for State {
|
||||
mods_locked,
|
||||
group,
|
||||
} => {
|
||||
if let Some(window) = window {
|
||||
if let Some(client) = client {
|
||||
app.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Modifiers {
|
||||
depressed: mods_depressed,
|
||||
latched: mods_latched,
|
||||
@@ -834,10 +843,10 @@ impl Dispatch<ZwpRelativePointerV1, ()> for State {
|
||||
..
|
||||
} = 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;
|
||||
app.pending_events.push_back((
|
||||
window.pos,
|
||||
*client,
|
||||
CaptureEvent::Input(Event::Pointer(PointerEvent::Motion { time, dx, dy })),
|
||||
));
|
||||
}
|
||||
@@ -855,10 +864,10 @@ impl Dispatch<ZwlrLayerSurfaceV1, ()> for State {
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event {
|
||||
if let Some(window) = app
|
||||
.active_windows
|
||||
if let Some((window, _client)) = app
|
||||
.client_for_window
|
||||
.iter()
|
||||
.find(|w| &w.layer_surface == layer_surface)
|
||||
.find(|(w, _c)| &w.layer_surface == layer_surface)
|
||||
{
|
||||
// client corresponding to the layer_surface
|
||||
let surface = &window.surface;
|
||||
|
||||
@@ -3,12 +3,12 @@ use core::task::{Context, Poll};
|
||||
use futures::Stream;
|
||||
use once_cell::unsync::Lazy;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::HashMap;
|
||||
use std::ptr::{addr_of, addr_of_mut};
|
||||
|
||||
use futures::executor::block_on;
|
||||
use std::default::Default;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::{mpsc, Mutex};
|
||||
use std::task::ready;
|
||||
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,
|
||||
};
|
||||
|
||||
use super::{Capture, CaptureError, CaptureEvent, Position};
|
||||
use super::{Capture, CaptureError, CaptureEvent, CaptureHandle, Position};
|
||||
|
||||
enum Request {
|
||||
Create(Position),
|
||||
Destroy(Position),
|
||||
Create(CaptureHandle, Position),
|
||||
Destroy(CaptureHandle),
|
||||
}
|
||||
|
||||
pub struct WindowsInputCapture {
|
||||
event_rx: Receiver<(Position, CaptureEvent)>,
|
||||
event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
|
||||
msg_thread: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
@@ -65,22 +65,22 @@ unsafe fn signal_message_thread(event_type: EventType) {
|
||||
|
||||
#[async_trait]
|
||||
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 {
|
||||
{
|
||||
let mut requests = REQUEST_BUFFER.lock().unwrap();
|
||||
requests.push(Request::Create(pos));
|
||||
requests.push(Request::Create(handle, pos));
|
||||
}
|
||||
signal_message_thread(EventType::Request);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||
async fn destroy(&mut self, handle: CaptureHandle) -> Result<(), CaptureError> {
|
||||
unsafe {
|
||||
{
|
||||
let mut requests = REQUEST_BUFFER.lock().unwrap();
|
||||
requests.push(Request::Destroy(pos));
|
||||
requests.push(Request::Destroy(handle));
|
||||
}
|
||||
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 ACTIVE_CLIENT: Option<Position> = None;
|
||||
static mut CLIENTS: Lazy<HashSet<Position>> = Lazy::new(HashSet::new);
|
||||
static mut EVENT_TX: Option<Sender<(Position, CaptureEvent)>> = None;
|
||||
static mut ACTIVE_CLIENT: Option<CaptureHandle> = None;
|
||||
static mut CLIENT_FOR_POS: Lazy<HashMap<Position, CaptureHandle>> = Lazy::new(HashMap::new);
|
||||
static mut EVENT_TX: Option<Sender<(CaptureHandle, CaptureEvent)>> = None;
|
||||
static mut EVENT_THREAD_ID: AtomicU32 = AtomicU32::new(0);
|
||||
unsafe fn set_event_tid(tid: u32) {
|
||||
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 */
|
||||
if !CLIENTS.contains(&pos) {
|
||||
let Some(client) = CLIENT_FOR_POS.get(&pos) else {
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/* update active client and entry point */
|
||||
ACTIVE_CLIENT.replace(pos);
|
||||
ACTIVE_CLIENT.replace(*client);
|
||||
ENTRY_POINT = clamp_to_display_bounds(prev_pos, curr_pos);
|
||||
|
||||
/* notify main thread */
|
||||
@@ -305,7 +305,7 @@ unsafe extern "system" fn mouse_proc(ncode: i32, wparam: WPARAM, lparam: LPARAM)
|
||||
}
|
||||
|
||||
/* get active client if any */
|
||||
let Some(pos) = ACTIVE_CLIENT else {
|
||||
let Some(client) = ACTIVE_CLIENT else {
|
||||
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 {
|
||||
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) */
|
||||
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<()>) {
|
||||
unsafe {
|
||||
set_event_tid(GetCurrentThreadId());
|
||||
@@ -515,15 +513,9 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if WINDOW_CLASS_REGISTERED
|
||||
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
|
||||
.is_ok()
|
||||
{
|
||||
/* register window class if not yet done so */
|
||||
let ret = RegisterClassW(&window_class);
|
||||
if ret == 0 {
|
||||
panic!("RegisterClassW");
|
||||
}
|
||||
let ret = RegisterClassW(&window_class);
|
||||
if ret == 0 {
|
||||
panic!("RegisterClassW");
|
||||
}
|
||||
|
||||
/* window is used ro receive WM_DISPLAYCHANGE messages */
|
||||
@@ -583,16 +575,23 @@ fn message_thread(ready_tx: mpsc::Sender<()>) {
|
||||
|
||||
fn update_clients(request: Request) {
|
||||
match request {
|
||||
Request::Create(pos) => {
|
||||
unsafe { CLIENTS.insert(pos) };
|
||||
Request::Create(handle, pos) => {
|
||||
unsafe { CLIENT_FOR_POS.insert(pos, handle) };
|
||||
}
|
||||
Request::Destroy(pos) => unsafe {
|
||||
if let Some(active_pos) = ACTIVE_CLIENT {
|
||||
if pos == active_pos {
|
||||
let _ = ACTIVE_CLIENT.take();
|
||||
Request::Destroy(handle) => unsafe {
|
||||
for pos in [
|
||||
Position::Left,
|
||||
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 {
|
||||
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>> {
|
||||
match ready!(self.event_rx.poll_recv(cx)) {
|
||||
None => Poll::Ready(None),
|
||||
|
||||
@@ -3,7 +3,10 @@ use std::task::Poll;
|
||||
use async_trait::async_trait;
|
||||
use futures_core::Stream;
|
||||
|
||||
use super::{error::X11InputCaptureCreationError, Capture, CaptureError, CaptureEvent, Position};
|
||||
use super::{
|
||||
error::X11InputCaptureCreationError, Capture, CaptureError, CaptureEvent, CaptureHandle,
|
||||
Position,
|
||||
};
|
||||
|
||||
pub struct X11InputCapture {}
|
||||
|
||||
@@ -15,11 +18,11 @@ impl X11InputCapture {
|
||||
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
|
||||
async fn destroy(&mut self, _pos: Position) -> Result<(), CaptureError> {
|
||||
async fn destroy(&mut self, _id: CaptureHandle) -> Result<(), CaptureError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -33,7 +36,7 @@ impl Capture for X11InputCapture {
|
||||
}
|
||||
|
||||
impl Stream for X11InputCapture {
|
||||
type Item = Result<(Position, CaptureEvent), CaptureError>;
|
||||
type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;
|
||||
|
||||
fn poll_next(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
|
||||
@@ -44,7 +44,6 @@ ashpd = { version = "0.9", default-features = false, features = [
|
||||
reis = { version = "0.2", features = ["tokio"], optional = true }
|
||||
|
||||
[target.'cfg(target_os="macos")'.dependencies]
|
||||
bitflags = "2.5.0"
|
||||
core-graphics = { version = "0.23", features = ["highsierra"] }
|
||||
keycode = "0.4.0"
|
||||
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
use super::{error::EmulationError, Emulation, EmulationHandle};
|
||||
use async_trait::async_trait;
|
||||
use bitflags::bitflags;
|
||||
use core_graphics::base::CGFloat;
|
||||
use core_graphics::display::{
|
||||
CGDirectDisplayID, CGDisplayBounds, CGGetDisplaysWithRect, CGPoint, CGRect, CGSize,
|
||||
};
|
||||
use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
|
||||
use core_graphics::event::{
|
||||
CGEvent, CGEventFlags, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField,
|
||||
ScrollEventUnit,
|
||||
CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, ScrollEventUnit,
|
||||
};
|
||||
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 std::cell::Cell;
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::{sync::Notify, task::JoinHandle};
|
||||
use tokio::task::AbortHandle;
|
||||
|
||||
use super::error::MacOSEmulationCreationError;
|
||||
|
||||
@@ -26,10 +18,8 @@ const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
|
||||
|
||||
pub(crate) struct MacOSEmulation {
|
||||
event_source: CGEventSource,
|
||||
repeat_task: Option<JoinHandle<()>>,
|
||||
repeat_task: Option<AbortHandle>,
|
||||
button_state: ButtonState,
|
||||
modifier_state: Rc<Cell<XMods>>,
|
||||
notify_repeat_task: Arc<Notify>,
|
||||
}
|
||||
|
||||
struct ButtonState {
|
||||
@@ -75,8 +65,6 @@ impl MacOSEmulation {
|
||||
event_source,
|
||||
button_state,
|
||||
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) {
|
||||
// there can only be one repeating key and it's
|
||||
// always the last to be pressed
|
||||
self.cancel_repeat_task().await;
|
||||
self.kill_repeat_task();
|
||||
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 stop = tokio::select! {
|
||||
_ = tokio::time::sleep(DEFAULT_REPEAT_DELAY) => false,
|
||||
_ = notify.notified() => true,
|
||||
};
|
||||
if !stop {
|
||||
loop {
|
||||
key_event(event_source.clone(), key, 1, modifiers.get());
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(DEFAULT_REPEAT_INTERVAL) => {},
|
||||
_ = notify.notified() => break,
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(DEFAULT_REPEAT_DELAY).await;
|
||||
loop {
|
||||
key_event(event_source.clone(), key, 1);
|
||||
tokio::time::sleep(DEFAULT_REPEAT_INTERVAL).await;
|
||||
}
|
||||
// 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());
|
||||
}
|
||||
|
||||
async fn cancel_repeat_task(&mut self) {
|
||||
fn kill_repeat_task(&mut self) {
|
||||
if let Some(task) = self.repeat_task.take() {
|
||||
self.notify_repeat_task.notify_waiters();
|
||||
let _ = task.await;
|
||||
task.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
Ok(e) => e,
|
||||
Err(_) => {
|
||||
@@ -129,92 +102,7 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8, modifiers: XMods)
|
||||
return;
|
||||
}
|
||||
};
|
||||
event.set_flags(to_cgevent_flags(modifiers));
|
||||
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]
|
||||
@@ -227,6 +115,16 @@ impl Emulation for MacOSEmulation {
|
||||
match event {
|
||||
Event::Pointer(pointer_event) => match pointer_event {
|
||||
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() {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
@@ -235,11 +133,8 @@ impl Emulation for MacOSEmulation {
|
||||
}
|
||||
};
|
||||
|
||||
let (new_mouse_x, new_mouse_y) =
|
||||
clamp_to_screen_space(mouse_location.x, mouse_location.y, dx, dy);
|
||||
|
||||
mouse_location.x = new_mouse_x;
|
||||
mouse_location.y = new_mouse_y;
|
||||
mouse_location.x = (mouse_location.x + dx).clamp(min_x, max_x - 1.);
|
||||
mouse_location.y = (mouse_location.y + dy).clamp(min_y, max_y - 1.);
|
||||
|
||||
let mut event_type = CGEventType::MouseMoved;
|
||||
if self.button_state.left {
|
||||
@@ -384,25 +279,11 @@ impl Emulation for MacOSEmulation {
|
||||
match state {
|
||||
// pressed
|
||||
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,
|
||||
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());
|
||||
key_event(self.event_source.clone(), code, state)
|
||||
}
|
||||
KeyboardEvent::Modifiers { .. } => {}
|
||||
},
|
||||
}
|
||||
// FIXME
|
||||
@@ -415,81 +296,3 @@ impl Emulation for MacOSEmulation {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +44,6 @@ rustPlatform.buildRustPackage {
|
||||
postInstall = ''
|
||||
wrapProgram "$out/bin/lan-mouse" \
|
||||
--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; {
|
||||
|
||||
@@ -11,7 +11,6 @@ pub async fn run(config: Config) -> Result<(), InputCaptureError> {
|
||||
let mut input_capture = InputCapture::new(backend).await?;
|
||||
log::info!("creating clients");
|
||||
input_capture.create(0, Position::Left).await?;
|
||||
input_capture.create(4, Position::Left).await?;
|
||||
input_capture.create(1, Position::Right).await?;
|
||||
input_capture.create(2, Position::Top).await?;
|
||||
input_capture.create(3, Position::Bottom).await?;
|
||||
@@ -29,13 +28,12 @@ async fn do_capture(input_capture: &mut InputCapture) -> Result<(), CaptureError
|
||||
.await
|
||||
.ok_or(CaptureError::EndOfStream)??;
|
||||
let pos = match client {
|
||||
0 | 4 => Position::Left,
|
||||
0 => Position::Left,
|
||||
1 => Position::Right,
|
||||
2 => Position::Top,
|
||||
3 => Position::Bottom,
|
||||
_ => panic!(),
|
||||
_ => Position::Bottom,
|
||||
};
|
||||
log::info!("position: {client} ({pos}), event: {event}");
|
||||
log::info!("position: {pos}, event: {event}");
|
||||
if let CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key: 1, .. })) = event {
|
||||
input_capture.release().await?;
|
||||
break Ok(());
|
||||
|
||||
Reference in New Issue
Block a user