use crate::bind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PowerState {
Unknown,
OnBattery,
NoBattery,
Charging,
Charged,
}
impl From<bind::SDL_PowerState> for PowerState {
fn from(raw: bind::SDL_PowerState) -> Self {
match raw {
bind::SDL_POWERSTATE_UNKNOWN => PowerState::Unknown,
bind::SDL_POWERSTATE_ON_BATTERY => PowerState::OnBattery,
bind::SDL_POWERSTATE_NO_BATTERY => PowerState::NoBattery,
bind::SDL_POWERSTATE_CHARGING => PowerState::Charging,
bind::SDL_POWERSTATE_CHARGED => PowerState::Charged,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PowerInfo {
pub state: PowerState,
pub remaining_seconds: Option<u32>,
pub remaining_ratio: Option<u32>,
}
impl PowerInfo {
#[must_use]
pub fn now() -> Self {
let mut remaining_seconds = 0;
let mut remaining_ratio = 0;
let state = unsafe { bind::SDL_GetPowerInfo(&mut remaining_seconds, &mut remaining_ratio) };
Self {
state: state.into(),
remaining_seconds: (0 <= remaining_seconds).then(|| remaining_seconds as _),
remaining_ratio: (0 <= remaining_ratio).then(|| remaining_ratio as _),
}
}
}