diff --git a/src/commands/battery.rs b/src/commands/battery.rs index b9edce3..9fbdfd9 100644 --- a/src/commands/battery.rs +++ b/src/commands/battery.rs @@ -15,27 +15,41 @@ trait Lapctl { } fn try_call_daemon(command: &BatteryCommands) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - match command { + let res = match command { BatteryCommands::Limit { percent } => { - proxy.set_battery_limit(*percent as u32).await.is_ok() + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_battery_limit(*percent as u32), + ) + .await } - _ => false, - } + _ => return false, + }; + + matches!(res, Ok(Ok(_))) }) } diff --git a/src/commands/cooling.rs b/src/commands/cooling.rs index 8777b95..f75146f 100644 --- a/src/commands/cooling.rs +++ b/src/commands/cooling.rs @@ -15,32 +15,55 @@ trait Lapctl { } fn try_call_daemon(command: &CoolingCommands) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + debug!("Internal daemon call detected. Skipping D-Bus self-call."); + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - match command { - CoolingCommands::Performance => proxy - .set_cooling_profile("performance".to_string()) + let res = match command { + CoolingCommands::Performance => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_cooling_profile("performance".to_string()), + ) .await - .is_ok(), - CoolingCommands::Balanced => proxy - .set_cooling_profile("balanced".to_string()) + } + CoolingCommands::Balanced => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_cooling_profile("balanced".to_string()), + ) .await - .is_ok(), - CoolingCommands::Quiet => proxy.set_cooling_profile("quiet".to_string()).await.is_ok(), - } + } + CoolingCommands::Quiet => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_cooling_profile("quiet".to_string()), + ) + .await + } + }; + + matches!(res, Ok(Ok(_))) }) } diff --git a/src/commands/gpu.rs b/src/commands/gpu.rs index 424b769..52622b1 100644 --- a/src/commands/gpu.rs +++ b/src/commands/gpu.rs @@ -40,24 +40,36 @@ trait Lapctl { } fn try_call_daemon(cmd: &GpuCommands) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - match cmd { + let res = match cmd { GpuCommands::Integrated { no_reboot } => { - proxy.switch_gpu_integrated(*no_reboot).await.is_ok() + tokio::time::timeout( + std::time::Duration::from_secs(5), + proxy.switch_gpu_integrated(*no_reboot), + ) + .await } GpuCommands::Hybrid { rtd3, @@ -65,10 +77,11 @@ fn try_call_daemon(cmd: &GpuCommands) -> bool { no_reboot, } => { let rtd3_val = rtd3.map(|v| v as i32).unwrap_or(-1); - proxy - .switch_gpu_hybrid(rtd3_val, *use_nvidia_current, *no_reboot) - .await - .is_ok() + tokio::time::timeout( + std::time::Duration::from_secs(5), + proxy.switch_gpu_hybrid(rtd3_val, *use_nvidia_current, *no_reboot), + ) + .await } GpuCommands::Nvidia { dm, @@ -80,20 +93,23 @@ fn try_call_daemon(cmd: &GpuCommands) -> bool { } => { let dm_val = dm.clone().unwrap_or_default(); let coolbits_val = coolbits.map(|v| v as i32).unwrap_or(-1); - proxy - .switch_gpu_nvidia( + tokio::time::timeout( + std::time::Duration::from_secs(5), + proxy.switch_gpu_nvidia( dm_val, *force_comp, coolbits_val, *use_nvidia_current, *wayland, *no_reboot, - ) - .await - .is_ok() + ), + ) + .await } - _ => false, - } + _ => return false, + }; + + matches!(res, Ok(Ok(_))) }) } diff --git a/src/commands/inhibit.rs b/src/commands/inhibit.rs index bbbcbbb..4679cc3 100644 --- a/src/commands/inhibit.rs +++ b/src/commands/inhibit.rs @@ -18,22 +18,36 @@ trait Lapctl { } fn try_call_daemon_inhibit(active: bool, why: String, who: String) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - proxy.set_system_inhibition(active, why, who).await.is_ok() + let res = tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_system_inhibition(active, why, who), + ) + .await; + + matches!(res, Ok(Ok(_))) }) } diff --git a/src/commands/power.rs b/src/commands/power.rs index 71b6cf5..c33e265 100644 --- a/src/commands/power.rs +++ b/src/commands/power.rs @@ -16,36 +16,61 @@ trait Lapctl { } fn try_call_daemon(command: &PowerCommands) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - match command { - PowerCommands::Performance => proxy - .set_power_profile("performance".to_string()) + let res = match command { + PowerCommands::Performance => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_power_profile("performance".to_string()), + ) .await - .is_ok(), - PowerCommands::Balanced => proxy - .set_power_profile("balanced".to_string()) + } + PowerCommands::Balanced => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_power_profile("balanced".to_string()), + ) .await - .is_ok(), - PowerCommands::BatterySave => proxy - .set_power_profile("battery-save".to_string()) + } + PowerCommands::BatterySave => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_power_profile("battery-save".to_string()), + ) .await - .is_ok(), - PowerCommands::LimitTdp { watts } => proxy.set_tdp_limit(*watts).await.is_ok(), - } + } + PowerCommands::LimitTdp { watts } => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_tdp_limit(*watts), + ) + .await + } + }; + + matches!(res, Ok(Ok(_))) }) } diff --git a/src/commands/touchpad.rs b/src/commands/touchpad.rs index aa8a44d..13ac4bd 100644 --- a/src/commands/touchpad.rs +++ b/src/commands/touchpad.rs @@ -15,25 +15,47 @@ trait Lapctl { } fn try_call_daemon(command: &TouchpadCommands) -> bool { + if std::env::var("LAPCTL_DAEMON_INTERNAL").is_ok() { + return false; + } + let rt = match tokio::runtime::Runtime::new() { Ok(rt) => rt, Err(_) => return false, }; rt.block_on(async { - let connection = match Connection::system().await { - Ok(conn) => conn, - Err(_) => return false, - }; + let connection = + match tokio::time::timeout(std::time::Duration::from_secs(2), Connection::system()) + .await + { + Ok(Ok(conn)) => conn, + _ => return false, + }; + let proxy = match LapctlProxy::new(&connection).await { Ok(p) => p, Err(_) => return false, }; - match command { - TouchpadCommands::Enable => proxy.set_touchpad_inhibition(false).await.is_ok(), - TouchpadCommands::Disable => proxy.set_touchpad_inhibition(true).await.is_ok(), - } + let res = match command { + TouchpadCommands::Enable => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_touchpad_inhibition(false), + ) + .await + } + TouchpadCommands::Disable => { + tokio::time::timeout( + std::time::Duration::from_secs(2), + proxy.set_touchpad_inhibition(true), + ) + .await + } + }; + + matches!(res, Ok(Ok(_))) }) }