Return support for KDE < 6

This commit is contained in:
Demmie 2024-03-06 22:00:18 -05:00
parent 86e4de5a70
commit a21b917b26
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
4 changed files with 75 additions and 6 deletions

4
Cargo.lock generated
View File

@ -447,7 +447,7 @@ dependencies = [
[[package]]
name = "awatcher"
version = "0.2.4"
version = "0.2.5"
dependencies = [
"anyhow",
"aw-datastore",
@ -3811,7 +3811,7 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "watchers"
version = "0.2.4"
version = "0.2.5"
dependencies = [
"anyhow",
"async-trait",

View File

@ -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"

View File

@ -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);
}

View File

@ -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<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 {