Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"not charging" battery status #177

Open
MichaPau opened this issue Aug 4, 2024 · 1 comment
Open

"not charging" battery status #177

MichaPau opened this issue Aug 4, 2024 · 1 comment

Comments

@MichaPau
Copy link

MichaPau commented Aug 4, 2024

When the battery is not charging (because plugged in and fully charged) there is no output for the battery.

macchina -d shows
Readout "Battery" failed with message: Got an unexpected value "not charging" reading battery status

which is normal because there is no matching branch for that status in
libmacchina/src/linux/mod.rs -> LinuxBatteryReadout::status

Would be a nice enhancement to have..
but perhaps not possible for all os systems so it was left out intentionally...

@trollLemon
Copy link

I saw this issue, and I had a similar thought about a battery crate a while ago (svartalf/rust-battery#100).

Turns out on linux, the file to read the battery state from will have "not_charging" if the battery is plugged in but not charging.

I think it would be easy for the linux module of libmacchina, since we would only have to add a branch to the match statement for the battery status, but each OS battery status function would need to have a different implementation of the "not charging" status were to show on every supported OS.

i.e it will be a bit tricky on windows:

    fn status(&self) -> Result<BatteryState, ReadoutError> {
        let power_state = WindowsBatteryReadout::get_power_status()?;

        match power_state.ACLineStatus {
            0 => Ok(BatteryState::Discharging),
            1 => Ok(BatteryState::Charging),
            a => Err(ReadoutError::Other(format!(
                "Unexpected value for ac_line_status from win32 api: {a}"
            ))),
        }
    }

...

impl WindowsBatteryReadout {
    fn get_power_status() -> Result<SYSTEM_POWER_STATUS, ReadoutError> {
        let mut power_state = SYSTEM_POWER_STATUS::default();

        if unsafe { GetSystemPowerStatus(&mut power_state) }.as_bool() {
            return Ok(power_state);
        }

        Err(ReadoutError::Other(String::from(
            "Call to GetSystemPowerStatus failed.",
        )))
    }
}

I was thinking we could try reading from the BatteryFlag byte from the SYSTEM_POWER_STATUS struct, along with the ACLineStatus; we can see if its charging with the current code with ACLineStatus, but theoretically we can also check if the battery is plugged in and not charging if ACLineStatus is 1, and check if BatteryFlag is 0 (battery is not charging and is in between high and low capacity). We could also switch ACLineStatus altogether, since BatteryFlag has a charging value we can match against (Windows API for reference https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-system_power_status).

I’d love to hear any thoughts or suggestions on this approach, as part of me thinks there is an easier way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants