From a758913d5226f4b937f653fd09cc45de16511bbf Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Sun, 23 Apr 2023 21:58:49 -0400 Subject: [PATCH] Separate watchers to a module --- src/main.rs | 62 ++----------------- src/watchers.rs | 58 +++++++++++++++++ src/{ => watchers}/kwin_window.js | 0 src/{ => watchers}/kwin_window.rs | 6 +- src/{ => watchers}/wl-protocols/idle.xml | 0 ...oreign-toplevel-management-unstable-v1.xml | 0 src/{ => watchers}/wl_bindings.rs | 8 +-- src/{ => watchers}/wl_connection.rs | 0 src/{ => watchers}/wl_foreign_toplevel.rs | 23 +++---- src/{ => watchers}/wl_kwin_idle.rs | 5 +- src/{ => watchers}/x11_connection.rs | 0 src/{ => watchers}/x11_screensaver_idle.rs | 4 +- src/{ => watchers}/x11_window.rs | 4 +- 13 files changed, 85 insertions(+), 85 deletions(-) create mode 100644 src/watchers.rs rename src/{ => watchers}/kwin_window.js (100%) rename src/{ => watchers}/kwin_window.rs (98%) rename src/{ => watchers}/wl-protocols/idle.xml (100%) rename src/{ => watchers}/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml (100%) rename src/{ => watchers}/wl_bindings.rs (72%) rename src/{ => watchers}/wl_connection.rs (100%) rename src/{ => watchers}/wl_foreign_toplevel.rs (95%) rename src/{ => watchers}/wl_kwin_idle.rs (98%) rename src/{ => watchers}/x11_connection.rs (100%) rename src/{ => watchers}/x11_screensaver_idle.rs (96%) rename src/{ => watchers}/x11_window.rs (93%) diff --git a/src/main.rs b/src/main.rs index 0f1b124..fbe14be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,15 +4,8 @@ extern crate log; mod config; -mod kwin_window; mod report_client; -mod wl_bindings; -mod wl_connection; -mod wl_foreign_toplevel; -mod wl_kwin_idle; -mod x11_connection; -mod x11_screensaver_idle; -mod x11_window; +mod watchers; use config::Config; use fern::colors::{Color, ColoredLevelConfig}; @@ -20,55 +13,10 @@ use report_client::ReportClient; use std::env; use std::{error::Error, str::FromStr, sync::Arc, thread}; +use crate::watchers::ConstructorFilter; + type BoxedError = Box; -trait Watcher: Send { - fn new() -> Result - where - Self: Sized; - fn watch(&mut self, client: &Arc); -} - -type BoxedWatcher = Box; - -type WatcherConstructor = (&'static str, fn() -> Result); -type WatcherConstructors = [WatcherConstructor]; - -trait WatchersFilter { - fn filter_first_supported(&self) -> Option; -} - -impl WatchersFilter for WatcherConstructors { - fn filter_first_supported(&self) -> Option { - self.iter().find_map(|(name, watcher)| match watcher() { - Ok(watcher) => Some(watcher), - Err(e) => { - info!("{name} cannot run: {e}"); - None - } - }) - } -} - -macro_rules! watcher { - ($watcher_struct:ty) => { - (stringify!($watcher_struct), || { - Ok(Box::new(<$watcher_struct>::new()?)) - }) - }; -} - -const IDLE_WATCHERS: &[WatcherConstructor] = &[ - watcher!(wl_kwin_idle::IdleWatcher), - watcher!(x11_screensaver_idle::IdleWatcher), -]; - -const ACTIVE_WINDOW_WATCHERS: &[WatcherConstructor] = &[ - watcher!(wl_foreign_toplevel::WindowWatcher), - watcher!(kwin_window::WindowWatcher), - watcher!(x11_window::WindowWatcher), -]; - fn setup_logger() -> Result<(), fern::InitError> { let log_setting = env::var("AWATCHER_LOG").unwrap_or("info".to_string()); @@ -128,7 +76,7 @@ fn main() -> Result<(), BoxedError> { let mut thread_handlers = Vec::new(); - let idle_watcher = IDLE_WATCHERS.filter_first_supported(); + let idle_watcher = watchers::IDLE.filter_first_supported(); if let Some(mut watcher) = idle_watcher { let thread_client = Arc::clone(&client); let idle_handler = thread::spawn(move || watcher.watch(&thread_client)); @@ -137,7 +85,7 @@ fn main() -> Result<(), BoxedError> { warn!("No supported idle handler is found"); } - let window_watcher = ACTIVE_WINDOW_WATCHERS.filter_first_supported(); + let window_watcher = watchers::ACTIVE_WINDOW.filter_first_supported(); if let Some(mut watcher) = window_watcher { let thread_client = Arc::clone(&client); let active_window_handler = thread::spawn(move || watcher.watch(&thread_client)); diff --git a/src/watchers.rs b/src/watchers.rs new file mode 100644 index 0000000..36b86d0 --- /dev/null +++ b/src/watchers.rs @@ -0,0 +1,58 @@ +mod kwin_window; +mod wl_bindings; +mod wl_connection; +mod wl_foreign_toplevel; +mod wl_kwin_idle; +mod x11_connection; +mod x11_screensaver_idle; +mod x11_window; + +use crate::{report_client::ReportClient, BoxedError}; +use std::sync::Arc; + +pub trait Watcher: Send { + fn new() -> Result + where + Self: Sized; + fn watch(&mut self, client: &Arc); +} + +type BoxedWatcher = Box; + +type WatcherConstructor = (&'static str, fn() -> Result); +type WatcherConstructors = [WatcherConstructor]; + +pub trait ConstructorFilter { + fn filter_first_supported(&self) -> Option; +} + +impl ConstructorFilter for WatcherConstructors { + fn filter_first_supported(&self) -> Option { + self.iter().find_map(|(name, watcher)| match watcher() { + Ok(watcher) => Some(watcher), + Err(e) => { + info!("{name} cannot run: {e}"); + None + } + }) + } +} + +macro_rules! watcher { + ($watcher_struct:ty) => { + (stringify!($watcher_struct), || { + Ok(Box::new(<$watcher_struct>::new()?)) + }) + }; +} + +pub const IDLE: &WatcherConstructors = &[ + watcher!(wl_kwin_idle::IdleWatcher), + watcher!(x11_screensaver_idle::IdleWatcher), +]; + +pub const ACTIVE_WINDOW: &WatcherConstructors = &[ + watcher!(wl_foreign_toplevel::WindowWatcher), + watcher!(kwin_window::WindowWatcher), + watcher!(x11_window::WindowWatcher), +]; diff --git a/src/kwin_window.js b/src/watchers/kwin_window.js similarity index 100% rename from src/kwin_window.js rename to src/watchers/kwin_window.js diff --git a/src/kwin_window.rs b/src/watchers/kwin_window.rs similarity index 98% rename from src/kwin_window.rs rename to src/watchers/kwin_window.rs index 20b505b..973c733 100644 --- a/src/kwin_window.rs +++ b/src/watchers/kwin_window.rs @@ -1,12 +1,10 @@ -use crate::Watcher; - /* * This uses a hack with KWin scripts in order to receive the active window. * For the moment of writing, KWin doesn't implement the appropriate protocols to get a top level window. * Inspired by https://github.com/k0kubun/xremap/ */ -use super::report_client::ReportClient; -use super::BoxedError; +use super::{BoxedError, Watcher}; +use crate::report_client::ReportClient; use std::env::temp_dir; use std::path::Path; use std::sync::{mpsc::channel, Arc, Mutex}; diff --git a/src/wl-protocols/idle.xml b/src/watchers/wl-protocols/idle.xml similarity index 100% rename from src/wl-protocols/idle.xml rename to src/watchers/wl-protocols/idle.xml diff --git a/src/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml b/src/watchers/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml similarity index 100% rename from src/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml rename to src/watchers/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml diff --git a/src/wl_bindings.rs b/src/watchers/wl_bindings.rs similarity index 72% rename from src/wl_bindings.rs rename to src/watchers/wl_bindings.rs index 4c4db44..4371279 100644 --- a/src/wl_bindings.rs +++ b/src/watchers/wl_bindings.rs @@ -14,11 +14,11 @@ pub mod idle { pub mod __interfaces { use wayland_client::protocol::__interfaces::*; - wayland_scanner::generate_interfaces!("src/wl-protocols/idle.xml"); + wayland_scanner::generate_interfaces!("src/watchers/wl-protocols/idle.xml"); } use self::__interfaces::*; - wayland_scanner::generate_client_code!("src/wl-protocols/idle.xml"); + wayland_scanner::generate_client_code!("src/watchers/wl-protocols/idle.xml"); } pub mod wlr_foreign_toplevel { @@ -33,9 +33,9 @@ pub mod wlr_foreign_toplevel { pub mod __interfaces { use wayland_client::protocol::__interfaces::*; - wayland_scanner::generate_interfaces!("src/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml"); + wayland_scanner::generate_interfaces!("src/watchers/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml"); } use self::__interfaces::*; - wayland_scanner::generate_client_code!("src/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml"); + wayland_scanner::generate_client_code!("src/watchers/wl-protocols/wlr-foreign-toplevel-management-unstable-v1.xml"); } diff --git a/src/wl_connection.rs b/src/watchers/wl_connection.rs similarity index 100% rename from src/wl_connection.rs rename to src/watchers/wl_connection.rs diff --git a/src/wl_foreign_toplevel.rs b/src/watchers/wl_foreign_toplevel.rs similarity index 95% rename from src/wl_foreign_toplevel.rs rename to src/watchers/wl_foreign_toplevel.rs index 085e1ac..7864846 100644 --- a/src/wl_foreign_toplevel.rs +++ b/src/watchers/wl_foreign_toplevel.rs @@ -1,22 +1,19 @@ -use std::collections::HashMap; -use std::{sync::Arc, thread}; - -use crate::{wl_connection::subscribe_state, Watcher}; - -use super::report_client::ReportClient; -use super::wl_bindings; +use super::wl_bindings::wlr_foreign_toplevel::zwlr_foreign_toplevel_handle_v1::{ + Event as HandleEvent, State as HandleState, ZwlrForeignToplevelHandleV1, +}; +use super::wl_bindings::wlr_foreign_toplevel::zwlr_foreign_toplevel_manager_v1::{ + Event as ManagerEvent, ZwlrForeignToplevelManagerV1, EVT_TOPLEVEL_OPCODE, +}; use super::wl_connection::WlEventConnection; use super::BoxedError; +use super::{wl_connection::subscribe_state, Watcher}; +use crate::report_client::ReportClient; +use std::collections::HashMap; +use std::{sync::Arc, thread}; use wayland_client::{ event_created_child, globals::GlobalListContents, protocol::wl_registry, Connection, Dispatch, Proxy, QueueHandle, }; -use wl_bindings::wlr_foreign_toplevel::zwlr_foreign_toplevel_handle_v1::{ - Event as HandleEvent, State as HandleState, ZwlrForeignToplevelHandleV1, -}; -use wl_bindings::wlr_foreign_toplevel::zwlr_foreign_toplevel_manager_v1::{ - Event as ManagerEvent, ZwlrForeignToplevelManagerV1, EVT_TOPLEVEL_OPCODE, -}; struct WindowData { app_id: String, diff --git a/src/wl_kwin_idle.rs b/src/watchers/wl_kwin_idle.rs similarity index 98% rename from src/wl_kwin_idle.rs rename to src/watchers/wl_kwin_idle.rs index eb89b96..58096ac 100644 --- a/src/wl_kwin_idle.rs +++ b/src/watchers/wl_kwin_idle.rs @@ -1,9 +1,8 @@ -use crate::Watcher; - -use super::report_client::ReportClient; use super::wl_bindings; use super::wl_connection::{subscribe_state, WlEventConnection}; use super::BoxedError; +use super::Watcher; +use crate::report_client::ReportClient; use chrono::{DateTime, Duration, Utc}; use std::{sync::Arc, thread}; use wayland_client::{ diff --git a/src/x11_connection.rs b/src/watchers/x11_connection.rs similarity index 100% rename from src/x11_connection.rs rename to src/watchers/x11_connection.rs diff --git a/src/x11_screensaver_idle.rs b/src/watchers/x11_screensaver_idle.rs similarity index 96% rename from src/x11_screensaver_idle.rs rename to src/watchers/x11_screensaver_idle.rs index 5f0560b..dbd6c12 100644 --- a/src/x11_screensaver_idle.rs +++ b/src/watchers/x11_screensaver_idle.rs @@ -1,9 +1,9 @@ use std::{sync::Arc, thread}; +use super::{x11_connection::X11Connection, BoxedError, Watcher}; +use crate::report_client::ReportClient; use chrono::{Duration, Utc}; -use crate::{report_client::ReportClient, x11_connection::X11Connection, BoxedError, Watcher}; - pub struct IdleWatcher { connection: X11Connection, } diff --git a/src/x11_window.rs b/src/watchers/x11_window.rs similarity index 93% rename from src/x11_window.rs rename to src/watchers/x11_window.rs index 6957c76..f425af3 100644 --- a/src/x11_window.rs +++ b/src/watchers/x11_window.rs @@ -1,7 +1,7 @@ +use super::{x11_connection::X11Connection, BoxedError, Watcher}; +use crate::report_client::ReportClient; use std::thread; -use crate::{report_client::ReportClient, x11_connection::X11Connection, BoxedError, Watcher}; - pub struct WindowWatcher { connection: X11Connection, last_title: String,