From 2d9b4eeaab9df25ec693c61e51c374852077e9e3 Mon Sep 17 00:00:00 2001 From: cyber-sushi Date: Mon, 27 May 2024 08:21:38 +0200 Subject: [PATCH] Added per app bindings on KDE/Wayland through kdotool --- src/active_client.rs | 12 ++++++++++++ src/udev_monitor.rs | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/active_client.rs b/src/active_client.rs index 4f9d0e2..ba3a06e 100644 --- a/src/active_client.rs +++ b/src/active_client.rs @@ -40,6 +40,18 @@ pub async fn get_active_window(server: &Server, config: &HashMap Client::Default } }, + "KDE" => { + if let Ok(query) = Command::new("sh").arg("c").arg("kdotool getactivewindow getwindowclassname").output() { + let active_window = Client::Class(std::str::from_utf8(query.stdout.as_slice()).unwrap().trim().to_string()); + if config.contains_key(&active_window) { + active_window + } else { + Client::Default + } + } else { + Client::Default + } + } "x11" => { let connection = x11rb::connect(None).unwrap().0; let focused_window = get_input_focus(&connection) diff --git a/src/udev_monitor.rs b/src/udev_monitor.rs index 22d5fed..0150377 100644 --- a/src/udev_monitor.rs +++ b/src/udev_monitor.rs @@ -146,16 +146,26 @@ fn set_environment() -> Environment { env::set_var("XDG_SESSION_TYPE", "wayland") } - let supported_compositors = vec!["Hyprland", "sway"].into_iter().map(|str| String::from(str)).collect::>(); + let supported_compositors = vec!["Hyprland", "sway", "KDE"].into_iter().map(|str| String::from(str)).collect::>(); let (x11, wayland) = (String::from("x11"), String::from("wayland")); let server: Server = match (env::var("XDG_SESSION_TYPE"), env::var("XDG_CURRENT_DESKTOP")) { (Ok(session), Ok(desktop)) if session == wayland && supported_compositors.contains(&desktop) => { - println!("Running on {}, per application bindings enabled.", desktop); - Server::Connected(desktop) + let server = 'a: { + if desktop == String::from("KDE") { + if let Err(_) = Command::new("kdotool").output() { + println!("Running on KDE but kdotool doesn't seem to be installed.\n\ + Won't be able to change bindings according to the active window.\n"); + break 'a Server::Unsupported; + } + } + println!("Running on {}, per application bindings enabled.", desktop); + Server::Connected(desktop) + }; + server }, (Ok(session), Ok(desktop)) if session == wayland => { println!("Warning: unsupported compositor: {}, won't be able to change bindings according to the active window.\n\ - Currently supported desktops: Hyprland, Sway, X11.\n", desktop); + Currently supported desktops: Hyprland, Sway, Plasma/KWin, X11.\n", desktop); Server::Unsupported }, (Ok(session), _) if session == x11 => {