Skip to content

Commit

Permalink
refactor producer and consumer (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber authored Mar 21, 2024
1 parent af02ccc commit a491c0e
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 55 deletions.
2 changes: 0 additions & 2 deletions src/backend.rs

This file was deleted.

20 changes: 0 additions & 20 deletions src/backend/consumer.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/backend/producer.rs

This file was deleted.

36 changes: 28 additions & 8 deletions src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ use async_trait::async_trait;
use std::future;

use crate::{
backend::consumer,
client::{ClientEvent, ClientHandle},
event::Event,
};
use anyhow::Result;

#[cfg(windows)]
pub mod windows;

#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
pub mod x11;

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
pub mod wlroots;

#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
pub mod xdg_desktop_portal;

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
pub mod libei;

#[cfg(target_os = "macos")]
pub mod macos;

/// fallback consumer
pub mod dummy;

#[async_trait]
pub trait EventConsumer: Send {
async fn consume(&mut self, event: Event, client_handle: ClientHandle);
Expand All @@ -23,13 +43,13 @@ pub trait EventConsumer: Send {

pub async fn create() -> Box<dyn EventConsumer> {
#[cfg(windows)]
match consumer::windows::WindowsConsumer::new() {
match windows::WindowsConsumer::new() {
Ok(c) => return Box::new(c),
Err(e) => log::warn!("windows event consumer unavailable: {e}"),
}

#[cfg(target_os = "macos")]
match consumer::macos::MacOSConsumer::new() {
match macos::MacOSConsumer::new() {
Ok(c) => {
log::info!("using macos event consumer");
return Box::new(c);
Expand All @@ -38,7 +58,7 @@ pub async fn create() -> Box<dyn EventConsumer> {
}

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
match consumer::wlroots::WlrootsConsumer::new() {
match wlroots::WlrootsConsumer::new() {
Ok(c) => {
log::info!("using wlroots event consumer");
return Box::new(c);
Expand All @@ -47,7 +67,7 @@ pub async fn create() -> Box<dyn EventConsumer> {
}

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
match consumer::libei::LibeiConsumer::new().await {
match libei::LibeiConsumer::new().await {
Ok(c) => {
log::info!("using libei event consumer");
return Box::new(c);
Expand All @@ -56,7 +76,7 @@ pub async fn create() -> Box<dyn EventConsumer> {
}

#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
match consumer::xdg_desktop_portal::DesktopPortalConsumer::new().await {
match xdg_desktop_portal::DesktopPortalConsumer::new().await {
Ok(c) => {
log::info!("using xdg-remote-desktop-portal event consumer");
return Box::new(c);
Expand All @@ -65,7 +85,7 @@ pub async fn create() -> Box<dyn EventConsumer> {
}

#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
match consumer::x11::X11Consumer::new() {
match x11::X11Consumer::new() {
Ok(c) => {
log::info!("using x11 event consumer");
return Box::new(c);
Expand All @@ -74,5 +94,5 @@ pub async fn create() -> Box<dyn EventConsumer> {
}

log::error!("falling back to dummy event consumer");
Box::new(consumer::dummy::DummyConsumer::new())
Box::new(dummy::DummyConsumer::new())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ pub mod server;
pub mod consumer;
pub mod producer;

pub mod backend;
pub mod frontend;
pub mod scancode;
31 changes: 24 additions & 7 deletions src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,44 @@ use std::io;

use futures_core::Stream;

use crate::backend::producer;
use crate::{
client::{ClientEvent, ClientHandle},
event::Event,
};

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
pub mod libei;

#[cfg(target_os = "macos")]
pub mod macos;

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
pub mod wayland;

#[cfg(windows)]
pub mod windows;

#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
pub mod x11;

/// fallback event producer
pub mod dummy;

pub async fn create() -> Box<dyn EventProducer> {
#[cfg(target_os = "macos")]
match producer::macos::MacOSProducer::new() {
match macos::MacOSProducer::new() {
Ok(p) => return Box::new(p),
Err(e) => log::info!("macos event producer not available: {e}"),
}

#[cfg(windows)]
match producer::windows::WindowsProducer::new() {
match windows::WindowsProducer::new() {
Ok(p) => return Box::new(p),
Err(e) => log::info!("windows event producer not available: {e}"),
}

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
match producer::libei::LibeiProducer::new().await {
match libei::LibeiProducer::new().await {
Ok(p) => {
log::info!("using libei event producer");
return Box::new(p);
Expand All @@ -31,7 +48,7 @@ pub async fn create() -> Box<dyn EventProducer> {
}

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
match producer::wayland::WaylandEventProducer::new() {
match wayland::WaylandEventProducer::new() {
Ok(p) => {
log::info!("using layer-shell event producer");
return Box::new(p);
Expand All @@ -40,7 +57,7 @@ pub async fn create() -> Box<dyn EventProducer> {
}

#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
match producer::x11::X11Producer::new() {
match x11::X11Producer::new() {
Ok(p) => {
log::info!("using x11 event producer");
return Box::new(p);
Expand All @@ -49,7 +66,7 @@ pub async fn create() -> Box<dyn EventProducer> {
}

log::error!("falling back to dummy event producer");
Box::new(producer::dummy::DummyProducer::new())
Box::new(dummy::DummyProducer::new())
}

pub trait EventProducer: Stream<Item = io::Result<(ClientHandle, Event)>> + Unpin {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a491c0e

Please sign in to comment.