Skip to content

Commit

Permalink
move refcounting of key presses to input-emulation
Browse files Browse the repository at this point in the history
extract proto into its own crate

asdf

asdf

asdf
  • Loading branch information
feschber committed Aug 9, 2024
1 parent fe06ca1 commit e9de367
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 530 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["input-capture", "input-emulation", "input-event"]
members = ["input-capture", "input-emulation", "input-event", "lan-mouse-proto"]

[package]
name = "lan-mouse"
Expand All @@ -17,6 +17,7 @@ lto = "fat"
input-event = { path = "input-event", version = "0.2.1" }
input-emulation = { path = "input-emulation", version = "0.2.1", default-features = false }
input-capture = { path = "input-capture", version = "0.2.0", default-features = false }
lan-mouse-proto = { path = "lan-mouse-proto", version = "0.1.0" }

hickory-resolver = "0.24.1"
toml = "0.8"
Expand Down
8 changes: 2 additions & 6 deletions input-capture/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use std::task::{Context, Poll};
use async_trait::async_trait;
use futures_core::Stream;

use input_event::Event;

use crate::CaptureError;

use super::{Capture, CaptureHandle, Position};
use super::{Capture, CaptureError, CaptureEvent, CaptureHandle, Position};

pub struct DummyInputCapture {}

Expand Down Expand Up @@ -44,7 +40,7 @@ impl Capture for DummyInputCapture {
}

impl Stream for DummyInputCapture {
type Item = Result<(CaptureHandle, Event), CaptureError>;
type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;

fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Pending
Expand Down
31 changes: 26 additions & 5 deletions input-capture/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ mod dummy;

pub type CaptureHandle = u64;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum CaptureEvent {
/// capture on this capture handle is now active
Begin,
/// input event coming from capture handle
Input(Event),
}

impl Display for CaptureEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CaptureEvent::Begin => write!(f, "begin capture"),
CaptureEvent::Input(e) => write!(f, "{e}"),
}
}
}

#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum Position {
Left,
Expand Down Expand Up @@ -147,15 +164,19 @@ impl InputCapture {
}

impl Stream for InputCapture {
type Item = Result<(CaptureHandle, Event), CaptureError>;
type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;

fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
match self.capture.poll_next_unpin(cx) {
Poll::Ready(e) => {
if let Some(Ok((_, Event::Keyboard(KeyboardEvent::Key { key, state, .. })))) = e {
if let Some(Ok((
_,
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key { key, state, .. })),
))) = e
{
self.update_pressed_keys(key, state);
}
Poll::Ready(e)
Expand All @@ -166,7 +187,7 @@ impl Stream for InputCapture {
}

#[async_trait]
trait Capture: Stream<Item = Result<(CaptureHandle, Event), CaptureError>> + Unpin {
trait Capture: Stream<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>> + Unpin {
/// create a new client with the given id
async fn create(&mut self, id: CaptureHandle, pos: Position) -> Result<(), CaptureError>;

Expand All @@ -183,7 +204,7 @@ trait Capture: Stream<Item = Result<(CaptureHandle, Event), CaptureError>> + Unp
async fn create_backend(
backend: Backend,
) -> Result<
Box<dyn Capture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
CaptureCreationError,
> {
match backend {
Expand All @@ -204,7 +225,7 @@ async fn create_backend(
async fn create(
backend: Option<Backend>,
) -> Result<
Box<dyn Capture<Item = Result<(CaptureHandle, Event), CaptureError>>>,
Box<dyn Capture<Item = Result<(CaptureHandle, CaptureEvent), CaptureError>>>,
CaptureCreationError,
> {
if let Some(backend) = backend {
Expand Down
18 changes: 10 additions & 8 deletions input-capture/src/libei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use once_cell::sync::Lazy;

use input_event::Event;

use crate::CaptureEvent;

use super::{
error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
Capture as LanMouseInputCapture, CaptureHandle, Position,
Expand All @@ -56,7 +58,7 @@ enum LibeiNotifyEvent {
pub struct LibeiInputCapture<'a> {
input_capture: Pin<Box<InputCapture<'a>>>,
capture_task: JoinHandle<Result<(), CaptureError>>,
event_rx: Receiver<(CaptureHandle, Event)>,
event_rx: Receiver<(CaptureHandle, CaptureEvent)>,
notify_capture: Sender<LibeiNotifyEvent>,
notify_release: Arc<Notify>,
cancellation_token: CancellationToken,
Expand Down Expand Up @@ -201,7 +203,7 @@ async fn connect_to_eis(
async fn libei_event_handler(
mut ei_event_stream: EiConvertEventStream,
context: ei::Context,
event_tx: Sender<(CaptureHandle, Event)>,
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
release_session: Arc<Notify>,
current_client: Rc<Cell<Option<CaptureHandle>>>,
) -> Result<(), CaptureError> {
Expand Down Expand Up @@ -258,7 +260,7 @@ async fn do_capture(
mut capture_event: Receiver<LibeiNotifyEvent>,
notify_release: Arc<Notify>,
session: Option<(Session<'_, InputCapture<'_>>, BitFlags<Capabilities>)>,
event_tx: Sender<(CaptureHandle, Event)>,
event_tx: Sender<(CaptureHandle, CaptureEvent)>,
cancellation_token: CancellationToken,
) -> Result<(), CaptureError> {
let mut session = session.map(|s| s.0);
Expand Down Expand Up @@ -354,7 +356,7 @@ async fn do_capture(
async fn do_capture_session(
input_capture: &InputCapture<'_>,
session: &mut Session<'_, InputCapture<'_>>,
event_tx: &Sender<(CaptureHandle, Event)>,
event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
active_clients: &[(CaptureHandle, Position)],
next_barrier_id: &mut u32,
notify_release: &Notify,
Expand Down Expand Up @@ -423,7 +425,7 @@ async fn do_capture_session(
current_client.replace(Some(client));

// client entered => send event
event_tx.send((client, Event::Enter())).await.expect("no channel");
event_tx.send((client, CaptureEvent::Begin)).await.expect("no channel");

tokio::select! {
_ = notify_release.notified() => { /* capture release */
Expand Down Expand Up @@ -554,7 +556,7 @@ async fn handle_ei_event(
ei_event: EiEvent,
current_client: Option<CaptureHandle>,
context: &ei::Context,
event_tx: &Sender<(CaptureHandle, Event)>,
event_tx: &Sender<(CaptureHandle, CaptureEvent)>,
release_session: &Notify,
) -> Result<(), CaptureError> {
match ei_event {
Expand All @@ -575,7 +577,7 @@ async fn handle_ei_event(
_ => {
if let Some(handle) = current_client {
for event in Event::from_ei_event(ei_event) {
event_tx.send((handle, event)).await.expect("no channel");
event_tx.send((handle, CaptureEvent::Input(event))).await.expect("no channel");
}
}
}
Expand Down Expand Up @@ -627,7 +629,7 @@ impl<'a> Drop for LibeiInputCapture<'a> {
}

impl<'a> Stream for LibeiInputCapture<'a> {
type Item = Result<(CaptureHandle, Event), 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) {
Expand Down
36 changes: 18 additions & 18 deletions input-capture/src/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use wayland_client::{

use input_event::{Event, KeyboardEvent, PointerEvent};

use crate::CaptureError;
use crate::{CaptureError, CaptureEvent};

use super::{
error::{LayerShellCaptureCreationError, WaylandBindError},
Expand Down Expand Up @@ -108,7 +108,7 @@ struct State {
wayland_fd: RawFd,
read_guard: Option<ReadEventsGuard>,
qh: QueueHandle<Self>,
pending_events: VecDeque<(CaptureHandle, Event)>,
pending_events: VecDeque<(CaptureHandle, CaptureEvent)>,
output_info: Vec<(WlOutput, OutputInfo)>,
scroll_discrete_pending: bool,
}
Expand Down Expand Up @@ -585,7 +585,7 @@ impl Capture for WaylandInputCapture {
}

impl Stream for WaylandInputCapture {
type Item = Result<(CaptureHandle, Event), CaptureError>;
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() {
Expand Down Expand Up @@ -694,7 +694,7 @@ impl Dispatch<WlPointer, ()> for State {
.iter()
.find(|(w, _c)| w.surface == surface)
.unwrap();
app.pending_events.push_back((*client, Event::Enter()));
app.pending_events.push_back((*client, CaptureEvent::Begin));
}
wl_pointer::Event::Leave { .. } => {
/* There are rare cases, where when a window is opened in
Expand All @@ -718,11 +718,11 @@ impl Dispatch<WlPointer, ()> for State {
let (_, client) = app.focused.as_ref().unwrap();
app.pending_events.push_back((
*client,
Event::Pointer(PointerEvent::Button {
CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time,
button,
state: u32::from(state),
}),
})),
));
}
wl_pointer::Event::Axis { time, axis, value } => {
Expand All @@ -735,11 +735,11 @@ impl Dispatch<WlPointer, ()> for State {
} else {
app.pending_events.push_back((
*client,
Event::Pointer(PointerEvent::Axis {
CaptureEvent::Input(Event::Pointer(PointerEvent::Axis {
time,
axis: u32::from(axis) as u8,
value,
}),
})),
));
}
}
Expand All @@ -748,10 +748,10 @@ impl Dispatch<WlPointer, ()> for State {
app.scroll_discrete_pending = true;
app.pending_events.push_back((
*client,
Event::Pointer(PointerEvent::AxisDiscrete120 {
CaptureEvent::Input(Event::Pointer(PointerEvent::AxisDiscrete120 {
axis: u32::from(axis) as u8,
value: value120,
}),
})),
));
}
wl_pointer::Event::Frame {} => {
Expand Down Expand Up @@ -787,11 +787,11 @@ impl Dispatch<WlKeyboard, ()> for State {
if let Some(client) = client {
app.pending_events.push_back((
*client,
Event::Keyboard(KeyboardEvent::Key {
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key {
time,
key,
state: u32::from(state) as u8,
}),
})),
));
}
}
Expand All @@ -805,12 +805,12 @@ impl Dispatch<WlKeyboard, ()> for State {
if let Some(client) = client {
app.pending_events.push_back((
*client,
Event::Keyboard(KeyboardEvent::Modifiers {
mods_depressed,
mods_latched,
mods_locked,
CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Modifiers {
depressed: mods_depressed,
latched: mods_latched,
locked: mods_locked,
group,
}),
})),
));
}
}
Expand Down Expand Up @@ -840,7 +840,7 @@ impl Dispatch<ZwpRelativePointerV1, ()> for State {
let time = (((utime_hi as u64) << 32 | utime_lo as u64) / 1000) as u32;
app.pending_events.push_back((
*client,
Event::Pointer(PointerEvent::Motion { time, dx, dy }),
CaptureEvent::Input(Event::Pointer(PointerEvent::Motion { time, dx, dy })),
));
}
}
Expand Down
13 changes: 5 additions & 8 deletions input-capture/src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ use std::task::Poll;
use async_trait::async_trait;
use futures_core::Stream;

use crate::CaptureError;

use super::Capture;
use input_event::Event;

use super::error::X11InputCaptureCreationError;
use super::{CaptureHandle, Position};
use super::{
error::X11InputCaptureCreationError, Capture, CaptureError, CaptureEvent, CaptureHandle,
Position,
};

pub struct X11InputCapture {}

Expand Down Expand Up @@ -39,7 +36,7 @@ impl Capture for X11InputCapture {
}

impl Stream for X11InputCapture {
type Item = Result<(CaptureHandle, Event), CaptureError>;
type Item = Result<(CaptureHandle, CaptureEvent), CaptureError>;

fn poll_next(
self: std::pin::Pin<&mut Self>,
Expand Down
6 changes: 3 additions & 3 deletions input-emulation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ impl InputEmulation {
}

let event = Event::Keyboard(KeyboardEvent::Modifiers {
mods_depressed: 0,
mods_latched: 0,
mods_locked: 0,
depressed: 0,
latched: 0,
locked: 0,
group: 0,
});
self.emulation.consume(event, handle).await?;
Expand Down
Loading

0 comments on commit e9de367

Please sign in to comment.