diff --git a/watchers/src/watchers/kwin_window.js b/watchers/src/watchers/kwin_window.js index 54f9307..b30f913 100644 --- a/watchers/src/watchers/kwin_window.js +++ b/watchers/src/watchers/kwin_window.js @@ -12,7 +12,7 @@ function send(client) { ); } -workspace.windowActivated.connect(function(client){ +let handler = function(client){ if (client === null) { return; } @@ -26,4 +26,12 @@ workspace.windowActivated.connect(function(client){ } send(client); -}); +}; + +let activationEvent = workspace.windowActivated ? workspace.windowActivated : workspace.clientActivated; +if (workspace.windowActivated) { + workspace.windowActivated.connect(handler); +} else { + // KDE version < 6 + workspace.clientActivated.connect(handler); +} diff --git a/watchers/src/watchers/kwin_window.rs b/watchers/src/watchers/kwin_window.rs index 5b228ae..44ea883 100644 --- a/watchers/src/watchers/kwin_window.rs +++ b/watchers/src/watchers/kwin_window.rs @@ -92,10 +92,16 @@ impl KWinScript { async fn start(&self, script_number: i32) -> anyhow::Result<()> { debug!("Starting KWin script {script_number}"); + + let path = if self.get_major_version().await < 6 { + format!("/{script_number}") + } else { + format!("/Scripting/Script{script_number}") + }; self.dbus_connection .call_method( Some("org.kde.KWin"), - format!("/Scripting/Script{script_number}"), + path, Some("org.kde.kwin.Script"), "run", &(), @@ -104,6 +110,61 @@ impl KWinScript { .with_context(|| "Error on starting the script")?; Ok(()) } + + async fn get_major_version(&self) -> i8 { + if let Ok(version) = Self::get_major_version_from_env() { + debug!("KWin version from KDE_SESSION_VERSION: {version}"); + + version + } else { + self.get_major_version_from_dbus() + .await + .unwrap_or_else(|e| { + error!("Failed to get KWin version: {e}"); + 5 + }) + } + } + + fn get_major_version_from_env() -> anyhow::Result { + env::var("KDE_SESSION_VERSION")? + .parse::() + .map_err(std::convert::Into::into) + } + + async fn get_major_version_from_dbus(&self) -> anyhow::Result { + let support_information: String = self + .dbus_connection + .call_method( + Some("org.kde.KWin"), + "/KWin", + Some("org.kde.KWin"), + "supportInformation", + &(), + ) + .await? + .body::()?; + + // find a string like "KWin version: 5.27.8" and extract the version number from it: + let version = support_information + .lines() + .find(|line| line.starts_with("KWin version: ")) + .ok_or(anyhow!("KWin version not found"))? + .split_whitespace() + .last() + .ok_or(anyhow!("KWin version is invalid"))?; + + // Extract the major version number from the version number like "5.27.8": + let major_version = version + .split('.') + .next() + .ok_or(anyhow!("KWin version is invalid: {version}"))? + .parse::()?; + + debug!("KWin version from DBus: {version}, major version: {major_version}"); + + Ok(major_version) + } } impl Drop for KWinScript {