After discovering the the shell script wrote by Sofian to get the remaining battery life time in a laptop, I try my own implementation in pure C. I also try to add extra informations provided by the ACPI.

Where are the information

A kick look with the sysctl(8) tool, the informations are located in the hw.acpi.battery.

% sysctrl hw.acpi.battery

Running the previous command, the returned values are :

Argument Description
life battery percent capacity remaining
state battery current status (flags)
time battery remaining time in minutes (Not available in my computer)
Ok, so what's the battery states, I mean in *real* life.
ValueStateDescription
1 Discharging We are using the battery
2 Charging We are charging the battery
4 Critical something really weird, but what
7 Not present / max another unknown state

Notes about status :

  • If the battery is full and we are using the AC power the status value is 0
  • If we disconnect the AC power, the status value is 2
  • If the battery is not full, and we connect the AC power the status is 2

Sysctrl(3) and battery

A short lookup in the FreeBSD code lets me know all the informations I'm looking for are returned as integer values.

Not all the sysctrl uses basic C data types as shown in the sample code provided with the sysctl(3) man page.

The code

You can get the code for this sample application here To compile it just write

%make battery.c -o battery

Screenshots

I use my code to display the battery status in my wmii status bar This screenshots shows what the application looks like when the AC power is plugged and when only the battery is active.