Skip to main content

hyprshell_exec_lib/
kill.rs

1use crate::util::to_client_address;
2use anyhow::Context;
3use core_lib::ClientId;
4use hyprland::data::Clients;
5use hyprland::dispatch::{Dispatch, DispatchType, WindowIdentifier};
6use hyprland::prelude::HyprData;
7use std::thread;
8use std::time::Duration;
9use tracing::{instrument, warn};
10
11/// Sends a close window request to hyprland and waits for the client to be killed
12///
13/// Returns true if the client was killed successfully, false if close was interrupted
14#[instrument(level = "debug", ret(level = "trace"))]
15pub fn kill_client_blocking(address: ClientId, timeout: Duration) -> anyhow::Result<bool> {
16    match kill_client_blocking_lua(address, timeout) {
17        Err(e) => {
18            warn!("Failed to kill client: {}, trying legacy syntax", e);
19            kill_client_blocking_legacy(address, timeout)
20        }
21        Ok(r) => Ok(r),
22    }
23}
24
25fn kill_client_blocking_lua(address: ClientId, timeout: Duration) -> anyhow::Result<bool> {
26    let disp = hyprland::dispatch_new::Dispatch::WindowKill(Some(
27        hyprland::dispatch_new::WindowIdentifier::Address(to_client_address(address)),
28    ));
29    disp.apply().context("failed to execute dispatch")?;
30    thread::sleep(timeout);
31    let client = Clients::get()
32        .context("get clients failed")?
33        .into_iter()
34        .find(|c| c.address == to_client_address(address));
35
36    Ok(client.is_none())
37}
38
39fn kill_client_blocking_legacy(address: ClientId, timeout: Duration) -> anyhow::Result<bool> {
40    Dispatch::call(DispatchType::CloseWindow(WindowIdentifier::Address(
41        to_client_address(address),
42    )))?;
43    thread::sleep(timeout);
44    let client = Clients::get()
45        .context("get clients failed")?
46        .into_iter()
47        .find(|c| c.address == to_client_address(address));
48
49    Ok(client.is_none())
50}