refactor(src/commands): Refactor battery and touchpad command execution

Refactored command flow to separate daemon invocation, added status handling, and improved error handling.

- New Features: Added `execute_local` helper and `BatteryCommands::Status` handling.
- Enhancements: Simplified daemon checks, removed redundant condition, improved readability and error messages.
- Bug Fixes: Fixed missed daemon return path for status command and ensured proper touchpad inhibition logic.
This commit is contained in:
sankalp committed 2026-03-23 16:16:25 +05:45
1 parent 319ca54300
commit 0dc00ec5c9
7 files changed
+143 -132

No files matched your search

+46 -62
View File
@@ -15,10 +15,6 @@ trait Lapctl {
} }
fn try_call_daemon(command: &BatteryCommands) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -54,20 +50,27 @@ fn try_call_daemon(command: &BatteryCommands) -> bool {
} }
pub fn execute(command: &BatteryCommands) { pub fn execute(command: &BatteryCommands) {
if matches!(command, BatteryCommands::Limit { .. }) && try_call_daemon(command) {
println!("Request handled by lapctld daemon.");
return;
}
match command { match command {
BatteryCommands::Limit { percent } => { BatteryCommands::Limit { percent } => {
if !(&1..=&100).contains(&percent) { if !(&1..=&100).contains(&percent) {
error!("Invalid percentage. Please provide a value between 1 and 100."); error!("Invalid percentage. Please provide a value between 1 and 100.");
std::process::exit(1); std::process::exit(1);
} }
println!("Setting battery charge limit to {}%", percent); println!("Setting battery charge limit to {}%", percent);
}
BatteryCommands::Status => {}
}
if matches!(command, BatteryCommands::Limit { .. }) && try_call_daemon(command) {
return;
}
execute_local(command);
}
pub fn execute_local(command: &BatteryCommands) {
match command {
BatteryCommands::Limit { percent } => {
let mut success_count = 0; let mut success_count = 0;
let sys_class_power = Path::new("/sys/class/power_supply"); let sys_class_power = Path::new("/sys/class/power_supply");
@@ -90,47 +93,34 @@ pub fn execute(command: &BatteryCommands) {
} }
} else { } else {
// Ideapad fallback // Ideapad fallback
let mut ideapad_found = false; let conservation_path = Path::new(
if let Ok(ideapad_entries) = "/sys/bus/platform/devices/ideapad_acpi/conservation_mode",
fs::read_dir("/sys/bus/platform/drivers/ideapad_acpi") );
{ if conservation_path.exists() {
for ideapad_entry in ideapad_entries.flatten() { let val = if *percent < 100 { "1" } else { "0" };
let ideapad_path = ideapad_entry.path(); match fs::write(conservation_path, val) {
let conservation_path = ideapad_path.join("conservation_mode"); Ok(_) => {
if conservation_path.exists() { if *percent < 100 {
ideapad_found = true; println!(
let val = if *percent < 100 { "1" } else { "0" }; "WARNING: Your laptop (Lenovo Ideapad) does NOT support custom charge limits (like {}%).",
match fs::write(&conservation_path, val) { percent
Ok(_) => { );
if *percent < 100 { println!(
println!( "Instead, lapctl has enabled Lenovo's built-in 'Conservation Mode' via the Ideapad ACPI."
"WARNING: Your laptop (Lenovo Ideapad) does NOT support custom charge limits (like {}%).", );
percent println!(
); "This mode is hard-coded into your laptop's firmware to stop charging at ~60%."
println!( );
"Instead, lapctl has enabled Lenovo's built-in 'Conservation Mode' via the Ideapad ACPI." } else {
); println!(
println!( "Disabled Conservation Mode (charging to 100%)."
"This mode is hard-coded into your laptop's firmware to stop charging at ~60%." );
);
} else {
println!(
"Disabled Conservation Mode (charging to 100%)."
);
}
success_count += 1;
}
Err(e) => debug!(
"Failed to write to conservation_mode: {}",
e
),
} }
success_count += 1;
} }
Err(e) => debug!("Failed to write to conservation_mode: {}", e),
} }
} }
if !ideapad_found {
debug!("limit not supported for {} (missing thresholds)", name_str);
}
} }
} }
} }
@@ -145,7 +135,6 @@ pub fn execute(command: &BatteryCommands) {
} }
} }
BatteryCommands::Status => { BatteryCommands::Status => {
// Re-use logic from `lapctl status` to show battery-specific details:
let sys_class_power = Path::new("/sys/class/power_supply"); let sys_class_power = Path::new("/sys/class/power_supply");
if let Ok(entries) = fs::read_dir(sys_class_power) { if let Ok(entries) = fs::read_dir(sys_class_power) {
for entry in entries.flatten() { for entry in entries.flatten() {
@@ -168,21 +157,16 @@ pub fn execute(command: &BatteryCommands) {
println!(" Charge Limit: {}%", limit.trim()); println!(" Charge Limit: {}%", limit.trim());
} else { } else {
// Ideapad fallback display // Ideapad fallback display
if let Ok(ideapad_entries) = let conservation_path = Path::new(
fs::read_dir("/sys/bus/platform/drivers/ideapad_acpi") "/sys/bus/platform/devices/ideapad_acpi/conservation_mode",
);
if conservation_path.exists()
&& let Ok(mode) = fs::read_to_string(conservation_path)
{ {
for ideapad_entry in ideapad_entries.flatten() { if mode.trim() == "1" {
let conservation_path = println!(" Charge Limit: Conservation Mode (~60%)");
ideapad_entry.path().join("conservation_mode"); } else {
if conservation_path.exists() println!(" Charge Limit: 100%");
&& let Ok(mode) = fs::read_to_string(&conservation_path)
{
if mode.trim() == "1" {
println!(" Charge Limit: Conservation Mode (~60%)");
} else {
println!(" Charge Limit: 100%");
}
}
} }
} }
} }
+11 -6
View File
@@ -15,11 +15,6 @@ trait Lapctl {
} }
fn try_call_daemon(command: &CoolingCommands) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -102,11 +97,21 @@ fn set_asus_throttle_policy(mode: &str) -> bool {
} }
pub fn execute(command: &CoolingCommands) { pub fn execute(command: &CoolingCommands) {
let desc = match command {
CoolingCommands::Performance => "Performance",
CoolingCommands::Balanced => "Balanced",
CoolingCommands::Quiet => "Quiet",
};
println!("Setting thermal/cooling profile to {}", desc);
if try_call_daemon(command) { if try_call_daemon(command) {
println!("Request handled by lapctld daemon.");
return; return;
} }
execute_local(command);
}
pub fn execute_local(command: &CoolingCommands) {
let (ideapad_mode, asus_mode, desc) = match command { let (ideapad_mode, asus_mode, desc) = match command {
CoolingCommands::Performance => ("1", "1", "Performance"), CoolingCommands::Performance => ("1", "1", "Performance"),
CoolingCommands::Balanced => ("0", "0", "Balanced"), CoolingCommands::Balanced => ("0", "0", "Balanced"),
+11 -5
View File
@@ -40,10 +40,6 @@ trait Lapctl {
} }
fn try_call_daemon(cmd: &GpuCommands) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -748,11 +744,21 @@ fn run_on_dgpu(command: &[String]) {
} }
pub fn execute(cmd: &GpuCommands) { pub fn execute(cmd: &GpuCommands) {
match cmd {
GpuCommands::Integrated { .. } => println!("Switching to Integrated GPU mode..."),
GpuCommands::Hybrid { .. } => println!("Switching to Hybrid GPU mode..."),
GpuCommands::Nvidia { .. } => println!("Switching to NVIDIA-only GPU mode..."),
_ => {}
}
if try_call_daemon(cmd) { if try_call_daemon(cmd) {
println!("Request handled by lapctld daemon.");
return; return;
} }
execute_local(cmd);
}
pub fn execute_local(cmd: &GpuCommands) {
match cmd { match cmd {
GpuCommands::Query => { GpuCommands::Query => {
let mode = get_current_mode(); let mode = get_current_mode();
+4 -4
View File
@@ -18,10 +18,6 @@ trait Lapctl {
} }
fn try_call_daemon_inhibit(active: bool, why: String, who: String) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -62,6 +58,10 @@ pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool, stop
} }
if daemon && command_args.is_empty() { if daemon && command_args.is_empty() {
println!(
"Attempting to activate system inhibition via lapctld: '{}' by '{}'",
why, who
);
if try_call_daemon_inhibit(true, why.to_string(), who.to_string()) { if try_call_daemon_inhibit(true, why.to_string(), who.to_string()) {
println!("Persistent system inhibition activated via lapctld."); println!("Persistent system inhibition activated via lapctld.");
println!("The system will stay awake until you run: lapctl inhibit --stop"); println!("The system will stay awake until you run: lapctl inhibit --stop");
+12 -6
View File
@@ -20,7 +20,7 @@ const POLKIT_POLICY: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<allow_inactive>auth_admin</allow_inactive> <allow_inactive>auth_admin</allow_inactive>
<allow_active>yes</allow_active> <allow_active>yes</allow_active>
</defaults> </defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/lapctl</annotate> <annotate key="org.freedesktop.policykit.exec.path">{EXE_PATH}</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">false</annotate> <annotate key="org.freedesktop.policykit.exec.allow_gui">false</annotate>
</action> </action>
</policyconfig> </policyconfig>
@@ -66,7 +66,7 @@ After=dbus.service
[Service] [Service]
Type=simple Type=simple
ExecStart=/usr/bin/lapctl daemon ExecStart={EXE_PATH} daemon
Restart=on-failure Restart=on-failure
[Install] [Install]
@@ -76,21 +76,27 @@ WantedBy=multi-user.target
pub fn execute() { pub fn execute() {
assert_root(); assert_root();
println!("Installing lapctl configuration for daemon communication..."); let current_exe = std::env::current_exe().expect("Failed to get current executable path");
let exe_path = current_exe.to_str().expect("Invalid executable path");
println!("Found lapctl at: {}", exe_path);
println!("Installing lapctl configuration with dynamic paths...");
let dbus_policy_path = "/etc/dbus-1/system.d/org.lapctl.conf"; let dbus_policy_path = "/etc/dbus-1/system.d/org.lapctl.conf";
create_file(dbus_policy_path, DBUS_POLICY, false); create_file(dbus_policy_path, DBUS_POLICY, false);
let systemd_content = SYSTEMD_SERVICE.replace("{EXE_PATH}", exe_path);
let service_path = "/etc/systemd/system/lapctld.service"; let service_path = "/etc/systemd/system/lapctld.service";
create_file(service_path, SYSTEMD_SERVICE, false); create_file(service_path, &systemd_content, false);
// Also install the udev rules file (even if empty now) to avoid errors if it was previously there // Also install the udev rules file (even if empty now) to avoid errors if it was previously there
let rules_path = "/etc/udev/rules.d/99-lapctl.rules"; let rules_path = "/etc/udev/rules.d/99-lapctl.rules";
create_file(rules_path, LAPCTL_UDEV_RULES, false); create_file(rules_path, LAPCTL_UDEV_RULES, false);
// Polkit fallbacks for rootless execution without the daemon // Polkit fallbacks for rootless execution without the daemon
let polkit_content = POLKIT_POLICY.replace("{EXE_PATH}", exe_path);
let polkit_policy_path = "/usr/share/polkit-1/actions/org.lapctl.policy"; let polkit_policy_path = "/usr/share/polkit-1/actions/org.lapctl.policy";
create_file(polkit_policy_path, POLKIT_POLICY, false); create_file(polkit_policy_path, &polkit_content, false);
let polkit_rules_path = "/etc/polkit-1/rules.d/10-lapctl.rules"; let polkit_rules_path = "/etc/polkit-1/rules.d/10-lapctl.rules";
create_file(polkit_rules_path, POLKIT_RULES, false); create_file(polkit_rules_path, POLKIT_RULES, false);
@@ -115,5 +121,5 @@ pub fn execute() {
} }
println!("Installation successful! The lapctld daemon is now running."); println!("Installation successful! The lapctld daemon is now running.");
println!("You can now use all lapctl commands without sudo."); println!("You can now use all lapctl commands from any location.");
} }
+13 -7
View File
@@ -16,10 +16,6 @@ trait Lapctl {
} }
fn try_call_daemon(command: &PowerCommands) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -226,11 +222,23 @@ fn set_platform_profile(profile: &str) {
} }
pub fn execute(command: &PowerCommands) { pub fn execute(command: &PowerCommands) {
match command {
PowerCommands::Performance => println!("Setting power profile to Performance"),
PowerCommands::Balanced => println!("Setting power profile to Balanced"),
PowerCommands::BatterySave => println!("Setting power profile to Battery Saver"),
PowerCommands::LimitTdp { watts } => {
println!("Setting CPU Package TDP Limit to {} Watts...", watts)
}
}
if try_call_daemon(command) { if try_call_daemon(command) {
println!("Request handled by lapctld daemon.");
return; return;
} }
execute_local(command);
}
pub fn execute_local(command: &PowerCommands) {
match command { match command {
PowerCommands::Performance => { PowerCommands::Performance => {
println!("Setting power profile to Performance"); println!("Setting power profile to Performance");
@@ -242,8 +250,6 @@ pub fn execute(command: &PowerCommands) {
PowerCommands::Balanced => { PowerCommands::Balanced => {
println!("Setting power profile to Balanced"); println!("Setting power profile to Balanced");
set_platform_profile("balanced"); set_platform_profile("balanced");
// Schedutil is the modern balanced default. Fallback isn't critical since cpufreq
// usually rejects invalid profiles, and standard Intel/AMD systems use EPP now anyway.
set_cpu_governor("schedutil"); set_cpu_governor("schedutil");
set_energy_performance_preference("balance_performance"); set_energy_performance_preference("balance_performance");
println!("Operation completed successfully."); println!("Operation completed successfully.");
+46 -42
View File
@@ -1,5 +1,5 @@
use crate::cli::TouchpadCommands; use crate::cli::TouchpadCommands;
use log::{error, info}; use log::{debug, error, info};
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use zbus::Connection; use zbus::Connection;
@@ -15,10 +15,6 @@ trait Lapctl {
} }
fn try_call_daemon(command: &TouchpadCommands) -> bool { 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() { let rt = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
Err(_) => return false, Err(_) => return false,
@@ -60,56 +56,64 @@ fn try_call_daemon(command: &TouchpadCommands) -> bool {
} }
pub fn execute(command: &TouchpadCommands) { pub fn execute(command: &TouchpadCommands) {
match command {
TouchpadCommands::Enable => println!("Enabling touchpad..."),
TouchpadCommands::Disable => println!("Disabling touchpad..."),
}
if try_call_daemon(command) { if try_call_daemon(command) {
println!("Request handled by lapctld daemon.");
return; return;
} }
let sys_class_input = Path::new("/sys/class/input"); execute_local(command);
if !sys_class_input.exists() { }
pub fn execute_local(command: &TouchpadCommands) {
match command {
TouchpadCommands::Enable => set_touchpad_inhibition(false),
TouchpadCommands::Disable => set_touchpad_inhibition(true),
}
}
fn set_touchpad_inhibition(inhibited: bool) {
let dev_input = Path::new("/sys/class/input");
if !dev_input.exists() {
error!("Could not find /sys/class/input. Is this a Linux system?"); error!("Could not find /sys/class/input. Is this a Linux system?");
return; return;
} }
let mut found_touchpads = Vec::new(); if let Ok(entries) = fs::read_dir(dev_input) {
let mut found = false;
if let Ok(entries) = fs::read_dir(sys_class_input) {
for entry in entries.flatten() { for entry in entries.flatten() {
let path = entry.path(); let name = entry.file_name();
let name_path = path.join("name"); let name_str = name.to_string_lossy();
let inhibited_path = path.join("inhibited");
if name_path.exists() if name_str.starts_with("input") {
&& inhibited_path.exists() let name_file = entry.path().join("name");
&& let Ok(name) = fs::read_to_string(&name_path) if let Ok(content) = fs::read_to_string(name_file) {
&& name.to_lowercase().contains("touchpad") let content_lower = content.to_lowercase();
{ if content_lower.contains("touchpad") || content_lower.contains("trackpad") {
found_touchpads.push((name.trim().to_string(), inhibited_path)); found = true;
} let inhibited_file = entry.path().join("inhibited");
} if inhibited_file.exists() {
} let val = if inhibited { "1" } else { "0" };
match fs::write(&inhibited_file, val) {
if found_touchpads.is_empty() { Ok(_) => info!("Set inhibit={} for {}", inhibited, name_str),
error!("No touchpad devices found with 'inhibited' support in /sys/class/input."); Err(e) => {
return; debug!("Failed to write to {}: {}", inhibited_file.display(), e)
} }
}
match command { }
TouchpadCommands::Enable => { }
for (name, path) in found_touchpads {
match fs::write(&path, "0") {
Ok(_) => info!("Enabled touchpad: {}", name),
Err(e) => error!("Failed to enable touchpad {}: {}", name, e),
} }
} }
} }
TouchpadCommands::Disable => { if found {
for (name, path) in found_touchpads { println!("Operation completed successfully.");
match fs::write(&path, "1") { } else {
Ok(_) => info!("Disabled touchpad: {}", name), error!("No touchpad device found in /sys/class/input.");
Err(e) => error!("Failed to disable touchpad {}: {}", name, e),
}
}
} }
} else {
error!("Failed to read directory /sys/class/input.");
} }
} }