Skip to content

Commit

Permalink
Return support for KDE < 6
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed Mar 7, 2024
1 parent 86e4de5 commit a21b917
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ image = { version = "0.24.7" }
members = ["watchers"]

[workspace.package]
version = "0.2.4"
version = "0.2.5"

[workspace.dependencies]
anyhow = "1.0.75"
Expand Down
12 changes: 10 additions & 2 deletions watchers/src/watchers/kwin_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function send(client) {
);
}

workspace.windowActivated.connect(function(client){
let handler = function(client){
if (client === null) {
return;
}
Expand All @@ -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);
}
63 changes: 62 additions & 1 deletion watchers/src/watchers/kwin_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
&(),
Expand All @@ -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<i8> {
env::var("KDE_SESSION_VERSION")?
.parse::<i8>()
.map_err(std::convert::Into::into)
}

async fn get_major_version_from_dbus(&self) -> anyhow::Result<i8> {
let support_information: String = self
.dbus_connection
.call_method(
Some("org.kde.KWin"),
"/KWin",
Some("org.kde.KWin"),
"supportInformation",
&(),
)
.await?
.body::<String>()?;

// 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::<i8>()?;

debug!("KWin version from DBus: {version}, major version: {major_version}");

Ok(major_version)
}
}

impl Drop for KWinScript {
Expand Down

0 comments on commit a21b917

Please sign in to comment.