1use crate::bind;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum PowerState {
9 Unknown,
11 OnBattery,
13 NoBattery,
15 Charging,
17 Charged,
19}
20
21impl From<bind::SDL_PowerState> for PowerState {
22 fn from(raw: bind::SDL_PowerState) -> Self {
23 match raw {
24 bind::SDL_POWERSTATE_UNKNOWN => PowerState::Unknown,
25 bind::SDL_POWERSTATE_ON_BATTERY => PowerState::OnBattery,
26 bind::SDL_POWERSTATE_NO_BATTERY => PowerState::NoBattery,
27 bind::SDL_POWERSTATE_CHARGING => PowerState::Charging,
28 bind::SDL_POWERSTATE_CHARGED => PowerState::Charged,
29 _ => unreachable!(),
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct PowerInfo {
37 pub state: PowerState,
39 pub remaining_seconds: Option<u32>,
41 pub remaining_ratio: Option<u32>,
43}
44
45impl PowerInfo {
46 #[must_use]
48 pub fn now() -> Self {
49 let mut remaining_seconds = 0;
50 let mut remaining_ratio = 0;
51 let state = unsafe { bind::SDL_GetPowerInfo(&mut remaining_seconds, &mut remaining_ratio) };
52 Self {
53 state: state.into(),
54 remaining_seconds: (0 <= remaining_seconds).then(|| remaining_seconds as _),
55 remaining_ratio: (0 <= remaining_ratio).then(|| remaining_ratio as _),
56 }
57 }
58}