diff --git a/README.md b/README.md index 101515b..9d97f48 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ host = "127.0.0.1" idle-timeout-seconds=180 poll-time-idle-seconds=4 poll-time-window-seconds=1 +disable-idle-watcher=false +disable-window-watcher=false [[awatcher.filters]] # match only "navigator" diff --git a/src/config.rs b/src/config.rs index 92e49b8..0268d91 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,6 +15,8 @@ pub struct RunnerConfig { pub config_file: PathBuf, #[cfg(feature = "bundle")] pub no_tray: bool, + pub disable_idle_watcher: bool, + pub disable_window_watcher: bool, } pub fn setup_logger(verbosity: LevelFilter) -> Result<(), fern::InitError> { @@ -70,6 +72,12 @@ pub fn from_cli() -> anyhow::Result { arg!(--"no-server" "Don't send data to the ActivityWatch server") .value_parser(value_parser!(bool)) .action(ArgAction::SetTrue), + arg!(--"disable-idle-watcher" "Don't watch for idle activity") + .value_parser(value_parser!(bool)) + .action(ArgAction::SetTrue), + arg!(--"disable-window-watcher" "Don't watch for window activity") + .value_parser(value_parser!(bool)) + .action(ArgAction::SetTrue), #[cfg(feature = "bundle")] arg!(--"no-tray" "Don't use the bundled tray, run only server and watchers in the background") .value_parser(value_parser!(bool)) @@ -106,6 +114,8 @@ pub fn from_cli() -> anyhow::Result { config_file: config.config_file, #[cfg(feature = "bundle")] no_tray: *matches.get_one("no-tray").unwrap(), + disable_idle_watcher: *matches.get_one("disable-idle-watcher").unwrap(), + disable_window_watcher: *matches.get_one("disable-window-watcher").unwrap(), }) } @@ -143,6 +153,17 @@ fn merge_cli(config: &mut FileConfig, matches: &ArgMatches) { get_arg_value("port", matches, &mut config.server.port); #[cfg(not(feature = "bundle"))] get_arg_value("host", matches, &mut config.server.host); + + get_arg_value( + "disable-idle-watcher", + matches, + &mut config.client.disable_idle_watcher, + ); + get_arg_value( + "disable-window-watcher", + matches, + &mut config.client.disable_window_watcher, + ); } fn get_arg_value(id: &str, matches: &ArgMatches, config_value: &mut T) diff --git a/src/main.rs b/src/main.rs index 0d274d2..c0b2e04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,9 @@ extern crate log; mod bundle; mod config; -use std::error::Error; +use std::pin::Pin; use std::sync::Arc; +use std::{error::Error, future::Future}; use tokio::signal::unix::{signal, SignalKind}; #[cfg(feature = "bundle")] use tokio::sync::mpsc; @@ -22,8 +23,20 @@ async fn main() -> anyhow::Result<(), Box> { let no_tray = config.no_tray; #[cfg(feature = "bundle")] let config_file = config.config_file; + let disable_idle_watcher = config.disable_idle_watcher; + let disable_window_watcher = config.disable_window_watcher; let config = config.watchers_config; + #[cfg(not(feature = "bundle"))] + if disable_idle_watcher && disable_window_watcher { + error!("Both watchers are disabled"); + return Err("At least one watcher must be enabled".into()); + } + #[cfg(feature = "bundle")] + if disable_idle_watcher && disable_window_watcher { + warn!("Both watchers are disabled"); + } + if config.no_server { warn!( "Not sending to server {}:{}", @@ -58,8 +71,22 @@ async fn main() -> anyhow::Result<(), Box> { let client = Arc::new(ReportClient::new(config).await?); - let idle_future = run_first_supported(Arc::clone(&client), &WatcherType::Idle); - let active_window_future = run_first_supported(Arc::clone(&client), &WatcherType::ActiveWindow); + let idle_future: Pin + Send>> = if disable_idle_watcher { + Box::pin(std::future::pending()) + } else { + Box::pin(run_first_supported(Arc::clone(&client), &WatcherType::Idle)) + }; + + let active_window_future: Pin + Send>> = if disable_window_watcher + { + Box::pin(std::future::pending::()) + } else { + Box::pin(run_first_supported( + Arc::clone(&client), + &WatcherType::ActiveWindow, + )) + }; + let sigterm = async { signal(SignalKind::terminate()).unwrap().recv().await; warn!("Caught SIGTERM, shutting down..."); diff --git a/watchers/src/config/file_config.rs b/watchers/src/config/file_config.rs index 9a9f0ef..c1bf467 100644 --- a/watchers/src/config/file_config.rs +++ b/watchers/src/config/file_config.rs @@ -69,6 +69,10 @@ pub struct ClientConfig { pub poll_time_window_seconds: u32, #[serde(default)] pub filters: Vec, + #[serde(default)] + pub disable_idle_watcher: bool, + #[serde(default)] + pub disable_window_watcher: bool, } impl ClientConfig { @@ -168,6 +172,8 @@ host = "http://address.com" idle-timeout-seconds=14 poll-time-idle-seconds=13 poll-time-window-seconds=12 +disable-idle-watcher=false +disable-window-watcher=false # Add as many filters as needed. # There should be at least 1 match field, and at least 1 replace field.