Polish code and fix mistakes

This commit is contained in:
Demmie 2023-04-17 23:46:55 -04:00
parent 9dbc6fce80
commit 65ce731f03
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
5 changed files with 23 additions and 35 deletions

View File

@ -144,11 +144,11 @@ impl ActiveWindowInterface {
} }
} }
pub struct KwinWindowWatcher { pub struct WindowWatcher {
kwin_script: KWinScript, kwin_script: KWinScript,
} }
impl Watcher for KwinWindowWatcher { impl Watcher for WindowWatcher {
fn new() -> Result<Self, BoxedError> { fn new() -> Result<Self, BoxedError> {
let kwin_script = KWinScript::new(Connection::session()?); let kwin_script = KWinScript::new(Connection::session()?);
if kwin_script.is_loaded()? { if kwin_script.is_loaded()? {

View File

@ -4,22 +4,22 @@
extern crate log; extern crate log;
mod config; mod config;
mod kwin_window;
mod report_client; mod report_client;
mod wl_bindings; mod wl_bindings;
mod wl_connection; mod wl_connection;
mod wl_foreign_toplevel; mod wl_foreign_toplevel;
mod wl_kwin_idle; mod wl_kwin_idle;
mod wl_kwin_window;
use config::Config; use config::Config;
use fern::colors::{Color, ColoredLevelConfig}; use fern::colors::{Color, ColoredLevelConfig};
use kwin_window::WindowWatcher as KwinWindowWatcher;
use report_client::ReportClient; use report_client::ReportClient;
use std::env; use std::env;
use std::{error::Error, str::FromStr, sync::Arc, thread}; use std::{error::Error, str::FromStr, sync::Arc, thread};
use wl_kwin_idle::KwinIdleWatcher; use wl_kwin_idle::IdleWatcher as WlKwinIdleWatcher;
use wl_kwin_window::KwinWindowWatcher;
use crate::wl_foreign_toplevel::WlrForeignToplevelWatcher; use crate::wl_foreign_toplevel::WindowWatcher as WlrForeignToplevelWindowWatcher;
type BoxedError = Box<dyn Error>; type BoxedError = Box<dyn Error>;
@ -59,10 +59,10 @@ macro_rules! watcher {
}; };
} }
const IDLE_WATCHERS: &[WatcherConstructor] = &[watcher!(KwinIdleWatcher)]; const IDLE_WATCHERS: &[WatcherConstructor] = &[watcher!(WlKwinIdleWatcher)];
const ACTIVE_WINDOW_WATCHERS: &[WatcherConstructor] = &[ const ACTIVE_WINDOW_WATCHERS: &[WatcherConstructor] = &[
watcher!(WlrForeignToplevelWatcher), watcher!(WlrForeignToplevelWindowWatcher),
watcher!(KwinWindowWatcher), watcher!(KwinWindowWatcher),
]; ];
@ -93,10 +93,10 @@ fn setup_logger() -> Result<(), fern::InitError> {
Ok(()) Ok(())
} }
fn main() { fn main() -> Result<(), BoxedError> {
setup_logger().unwrap(); setup_logger()?;
let client = ReportClient::new(Config::from_cli()); let client = ReportClient::new(Config::from_cli())?;
let client = Arc::new(client); let client = Arc::new(client);
info!( info!(
@ -131,4 +131,5 @@ fn main() {
error!("Thread failed with error"); error!("Thread failed with error");
} }
} }
Ok(())
} }

View File

@ -12,18 +12,12 @@ pub struct ReportClient {
} }
impl ReportClient { impl ReportClient {
pub fn new(config: Config) -> Self { pub fn new(config: Config) -> Result<Self, BoxedError> {
let host = config.host.clone(); let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher");
let port = config.port.to_string(); Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?;
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?;
let client = AwClient::new(&host, &port, "awatcher"); Ok(Self { client, config })
Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus").unwrap();
Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow").unwrap();
Self {
config,
client: AwClient::new(&host, &port, "awatcher"),
}
} }
pub fn ping( pub fn ping(

View File

@ -7,7 +7,6 @@ use super::report_client::ReportClient;
use super::wl_bindings; use super::wl_bindings;
use super::wl_connection::WlEventConnection; use super::wl_connection::WlEventConnection;
use super::BoxedError; use super::BoxedError;
use chrono::{DateTime, Utc};
use wayland_client::{ use wayland_client::{
event_created_child, globals::GlobalListContents, protocol::wl_registry, Connection, Dispatch, event_created_child, globals::GlobalListContents, protocol::wl_registry, Connection, Dispatch,
Proxy, QueueHandle, Proxy, QueueHandle,
@ -27,9 +26,6 @@ struct WindowData {
struct ToplevelState { struct ToplevelState {
windows: HashMap<String, WindowData>, windows: HashMap<String, WindowData>,
current_window_id: Option<String>, current_window_id: Option<String>,
_last_input_time: DateTime<Utc>,
_is_idle: bool,
_is_changed: bool,
client: Arc<ReportClient>, client: Arc<ReportClient>,
} }
@ -38,9 +34,6 @@ impl ToplevelState {
Self { Self {
windows: HashMap::new(), windows: HashMap::new(),
current_window_id: None, current_window_id: None,
_last_input_time: Utc::now(),
_is_idle: false,
_is_changed: false,
client, client,
} }
} }
@ -139,11 +132,11 @@ impl ToplevelState {
} }
} }
pub struct WlrForeignToplevelWatcher { pub struct WindowWatcher {
connection: WlEventConnection<ToplevelState>, connection: WlEventConnection<ToplevelState>,
} }
impl Watcher for WlrForeignToplevelWatcher { impl Watcher for WindowWatcher {
fn new() -> Result<Self, BoxedError> { fn new() -> Result<Self, BoxedError> {
let connection: WlEventConnection<ToplevelState> = WlEventConnection::connect()?; let connection: WlEventConnection<ToplevelState> = WlEventConnection::connect()?;
connection.get_foreign_toplevel_manager()?; connection.get_foreign_toplevel_manager()?;
@ -164,7 +157,7 @@ impl Watcher for WlrForeignToplevelWatcher {
if let Err(e) = self.connection.event_queue.roundtrip(&mut toplevel_state) { if let Err(e) = self.connection.event_queue.roundtrip(&mut toplevel_state) {
error!("Event queue is not processed: {e}"); error!("Event queue is not processed: {e}");
} else if let Err(e) = toplevel_state.send_active_window() { } else if let Err(e) = toplevel_state.send_active_window() {
error!("Error on idle iteration {e}"); error!("Error on iteration: {e}");
} }
thread::sleep(time::Duration::from_secs(u64::from( thread::sleep(time::Duration::from_secs(u64::from(

View File

@ -118,11 +118,11 @@ impl Dispatch<OrgKdeKwinIdleTimeout, ()> for IdleState {
} }
} }
pub struct KwinIdleWatcher { pub struct IdleWatcher {
connection: WlEventConnection<IdleState>, connection: WlEventConnection<IdleState>,
} }
impl Watcher for KwinIdleWatcher { impl Watcher for IdleWatcher {
fn new() -> Result<Self, BoxedError> { fn new() -> Result<Self, BoxedError> {
let connection: WlEventConnection<IdleState> = WlEventConnection::connect()?; let connection: WlEventConnection<IdleState> = WlEventConnection::connect()?;
connection.get_kwin_idle()?; connection.get_kwin_idle()?;
@ -147,7 +147,7 @@ impl Watcher for KwinIdleWatcher {
if let Err(e) = self.connection.event_queue.roundtrip(&mut idle_state) { if let Err(e) = self.connection.event_queue.roundtrip(&mut idle_state) {
error!("Event queue is not processed: {e}"); error!("Event queue is not processed: {e}");
} else if let Err(e) = idle_state.send_ping() { } else if let Err(e) = idle_state.send_ping() {
error!("Error on idle iteration {e}"); error!("Error on idle iteration: {e}");
} }
thread::sleep(time::Duration::from_secs(u64::from( thread::sleep(time::Duration::from_secs(u64::from(
client.config.poll_time_idle, client.config.poll_time_idle,