From 31c1b1ce4493f702338c97190335bb4de754ba12 Mon Sep 17 00:00:00 2001 From: cyber-sushi Date: Sat, 18 May 2024 01:47:49 +0200 Subject: [PATCH] Use Server enum instead of Option to match compositor --- src/active_client.rs | 113 +++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/active_client.rs b/src/active_client.rs index 597fac0..4f9d0e2 100644 --- a/src/active_client.rs +++ b/src/active_client.rs @@ -1,66 +1,73 @@ use crate::Config; +use crate::udev_monitor::{Client, Server}; use serde_json; use swayipc_async::Connection; use std::{collections::HashMap, process::Command}; use x11rb::protocol::xproto::{get_property, get_input_focus, Atom, AtomEnum}; -pub async fn get_active_window(current_desktop: &Option, config: &HashMap) -> String { - let active_client = current_desktop.clone().unwrap_or(String::from("default")); - match active_client.as_str() { - "Hyprland" => { - let query = Command::new("hyprctl").args(["activewindow", "-j"]).output().unwrap(); - if let Ok(reply) = serde_json::from_str::(std::str::from_utf8(query.stdout.as_slice()).unwrap()) { - let active_window = reply["class"].to_string().replace("\"", ""); - if config.contains_key(&active_window) { - active_window - } else { - String::from("default") - } - } else { - String::from("default") - } - }, - "sway" => { - let mut connection = Connection::new().await.unwrap(); - let active_window = match connection.get_tree().await.unwrap().find_focused(|window| window.focused) { - Some(window) => { - match window.app_id { - Some(id) => id, - None => window.window_properties.unwrap().class.unwrap() +pub async fn get_active_window(server: &Server, config: &HashMap) -> Client { + match server { + Server::Connected(server) => { + let server_str = server.as_str(); + match server_str { + "Hyprland" => { + let query = Command::new("hyprctl").args(["activewindow", "-j"]).output().unwrap(); + if let Ok(reply) = serde_json::from_str::(std::str::from_utf8(query.stdout.as_slice()).unwrap()) { + let active_window = Client::Class(reply["class"].to_string().replace("\"", "")); + if config.contains_key(&active_window) { + active_window + } else { + Client::Default + } + } else { + Client::Default } }, - None => String::from("default") - }; - if config.contains_key(&active_window) { - active_window - } else { - String::from("default") + "sway" => { + let mut connection = Connection::new().await.unwrap(); + let active_window = match connection.get_tree().await.unwrap().find_focused(|window| window.focused) { + Some(window) => { + match window.app_id { + Some(id) => Client::Class(id), + None => Client::Class(window.window_properties.unwrap().class.unwrap()) + } + }, + None => Client::Default + }; + if config.contains_key(&active_window) { + active_window + } else { + Client::Default + } + }, + "x11" => { + let connection = x11rb::connect(None).unwrap().0; + let focused_window = get_input_focus(&connection) + .unwrap().reply().unwrap().focus; + let (wm_class, string): (Atom, Atom) = (AtomEnum::WM_CLASS.into(), AtomEnum::STRING.into()); + let class = get_property(&connection, false, focused_window, wm_class, string, 0, u32::MAX) + .unwrap().reply().unwrap().value; + if let Some(middle) = class.iter().position(|&byte| byte == 0) { + let class = class.split_at(middle).1; + let mut class = &class[1..]; + if class.last() == Some(&0) { + class = &class[..class.len() -1]; + } + let active_window = Client::Class(std::str::from_utf8(class).unwrap().to_string()); + if config.contains_key(&active_window) { + active_window + } else { + Client::Default + } + } else { + Client::Default + } + }, + _ => Client::Default } }, - "x11" => { - let connection = x11rb::connect(None).unwrap().0; - let focused_window = get_input_focus(&connection) - .unwrap().reply().unwrap().focus; - let (wm_class, string): (Atom, Atom) = (AtomEnum::WM_CLASS.into(), AtomEnum::STRING.into()); - let class = get_property(&connection, false, focused_window, wm_class, string, 0, u32::MAX) - .unwrap().reply().unwrap().value; - if let Some(middle) = class.iter().position(|&byte| byte == 0) { - let class = class.split_at(middle).1; - let mut class = &class[1..]; - if class.last() == Some(&0) { - class = &class[..class.len() -1]; - } - let active_window = std::str::from_utf8(class).unwrap().to_string(); - if config.contains_key(&active_window) { - active_window - } else { - String::from("default") - } - } else { - String::from("default") - } - }, - _ => String::from("default") + Server::Unsupported => Client::Default, + Server::Failed => Client::Default, } }