libacpi(3)
π libacpi(3) π
okay bestie, welcome to libacpi β it's a Library Functions Manual situation and it is the general purpose library for ACPI. she's iconic, she's tiny, she's literally gonna tell you your battery percentage. let's get into it π¦
β¨ NAME β¨
libacpi β general purpose library for ACPI. that's it, that's the girl.
πΈ SYNOPSIS πΈ
#include <libacpi.h>
π DESCRIPTION π
so the libacpi library routines give you a way to read ACPI values in a comfortable way. comfortable!! like a lil weighted blanket for your sysfs. that includes:
- battery information
- fan states
- thermal zones
- AC states
a program using these routines must be linked with the -lacpi option. don't forget it, she will not link herself π
to initialize this functionality you have to first allocate space for a global_t structure:
global_t *global = malloc (sizeof (global_t));
now use the check_acpi_support() function to check if the system actually supports ACPI. in case it does, the function will return SUCCESS, or NOT_SUPPORTED on failure. vibe check complete β¨
now you can use the following functions to initialize the values you get from the ACPI interface:
init_acpi_batt(global);
init_acpi_acadapt(global);
init_acpi_fan(global);
init_acpi_thermal(global);
to know if some of the features is not supported, it is a good idea to store the return values in some variable. lowkey essential, ngl.
if you want to read non-static values the ACPI interface provides (for example battery percentage), use the following functions to do this:
read_acpi_batt(int num)
read_acpi_zone(int num, global_t *globals);
read_acpi_fan(int num)
read_acpi_acstate(global_t *globals);
since you can have more than one battery, fan and thermal zone, num is the number you want to read, for example in a loop:
thermal_t *tp;
....
for(i=0; i<global->thermal_count; i++){
read_acpi_zone(i, global);
tp = &thermals[i];
...
}
π§ Structures
struct fan_tβ fan datastruct battery_tβ information found about batterystruct thermal_tβ information about thermal zonestruct adapter_tβ information about ac adapterstruct global_tβ global acpi structure
π¦ Functions
int init_acpi_batt (global_t *globals)
int init_acpi_acadapt (global_t *globals)
int init_acpi_thermal (global_t *globals)
int init_acpi_fan (global_t *globals)
int check_acpi_support (void)
int read_acpi_batt (const int num)
void read_acpi_acstate (global_t *globals)
int read_acpi_zone (const int num, global_t *globals)
int read_acpi_fan (const int num)
π Variables
battery_t batteries [MAX_ITEMS]
thermal_t thermals [MAX_ITEMS]
fan_t fans [MAX_ITEMS]
π Enumerations
enum { ITEM_EXCEED = -5, DISABLED = -4, NOT_PRESENT = -3, ALLOC_ERR =
-2, NOT_SUPPORTED = -1, SUCCESS }
enum power_state_t { P_AC, P_BATT, P_ERR }
enum thermal_state_t { T_CRIT, T_HOT, T_PASS, T_ACT, T_OK, T_ERR }
enum charge_state_t { C_CHARGE, C_DISCHARGE, C_CHARGED, C_NOINFO, C_ERR
}
enum batt_state_t { B_HIGH, B_MED, B_LOW, B_CRIT, B_HARD_CRIT, B_ERR }
enum thermal_mode_t { CO_ACT, CO_PASS, CO_CRIT, CO_ERR }
enum fan_state_t { F_ON, F_OFF, F_ERR }
π Detailed Description π
now follows a more detailed description of functions, structures and enums. grab a snack, we're going deep π«Ά
Definition in file libacpi.h.
π Function Documentation π
int check_acpi_support (void)
checks if the system does support ACPI or not. the ultimate "do you even go here" moment.
Returns:
SUCCESSif the system supports ACPI or,NOT_SUPPORTED.
int init_acpi_acadapt (global_t * globals)
finds existing ac adapter and fills the adapt (in globa struct) structure with the paths of the important files to parse. (yes the original says "globa", she's quirky like that, we keep it π)
Parameters:
globalspointer to global acpi structure
Returns:
SUCCESSif everything is ok,NOT_SUPPORTEDif no information can be found,ALLOC_ERRon allocation errors.
int init_acpi_batt (global_t * globals)
finds existing batteries and fills the corresponding batteries structures with the paths of the important files to parse.
Parameters:
globalspointer to global acpi structure
Returns:
SUCCESSif everything is ok,NOT_SUPPORTEDif no information can be found,ALLOC_ERRon allocation errors andITEM_EXCEEDif there are more batteries found thanMAX_ITEMS, this should never happen.
int init_acpi_fan (global_t * globals)
finds existing fans and fills corresponding fan structures with the paths of the important files to parse for fan information.
Parameters:
globalspointer to global acpi structure
Returns:
SUCCESSif everything is ok,NOT_SUPPORTEDif no information can be found,ALLOC_ERRon allocation errors andITEM_EXCEEDif there are more fans found thanMAX_ITEMS, this should never happen.
int init_acpi_thermal (global_t * globals)
finds existing thermal zones and fills corresponding thermal structures with the paths of the important files to parse for thermal information.
Parameters:
globalspointer to global acpi structure
Returns:
SUCCESSif everything is ok,NOT_SUPPORTEDif no information can be found,ALLOC_ERRon allocation errors andITEM_EXCEEDif there are more thermal zones found thanMAX_ITEMS, this should never happen.
void read_acpi_acstate (global_t * globals)
looks up if the ac adapter is plugged in or not and sets the values in a struct. are you charging or are you free? π
Parameters:
globalspointer to the global acpi structure
Referenced by init_acpi_acadapt().
int read_acpi_batt (const int num)
gathers all information of a given battery and filling a struct with it.
Parameters:
numnumber of battery
Returns:
SUCCESSif everything is ok orITEM_EXCEEDif there is not batterynum.
int read_acpi_fan (const int num)
gathers all information about given fan and sets the corresponding values in a struct.
Parameters:
numnumber for the fan to read
Returns:
SUCCESSif everything is ok,ITEM_EXCEEDif there is not fannumorNOT_SUPPORTEDif the values can't be read. this should not happen if the init function returnedSUCCESSif the ACPI implementation.
int read_acpi_zone (const int num, global_t * globals)
gathers all information of a given thermal zone and sets the corresponding values in a struct.
Parameters:
numzoneglobalspointer to global acpi struct, needed if there is just one zone
Returns:
SUCCESSif everything is ok,ITEM_EXCEEDif there is not thermal zonenumorNOT_SUPPORTEDif the values can't be read. this should not happen if the init function returnedSUCCESSif the ACPI implementation.
π Enumeration Type Documentation π
possible function return values
Enumerator:
ITEM_EXCEEDβ maximum item count reachedDISABLEDβ features is disabledNOT_PRESENTβ something is not presentALLOC_ERRβ an error occurred while allocating spaceNOT_SUPPORTEDβ a feature is not supportedSUCCESSβ everything was fine
enum batt_state_t
Enumerator:
B_HIGHβ remaining battery life is highB_MEDβ remaining battery life is mediumB_LOWβ remaining battery life is lowB_CRITβ remaining battery life is criticalB_HARD_CRITβ remaining battery life is hard critical, you have a few minutes to chargeB_ERRβ some error occurred while reading the battery state
bestie if you see B_HARD_CRIT go find a charger. like now. πͺ«π
enum charge_state_t
Enumerator:
C_CHARGEβ battery is currently chargingC_DISCHARGEβ battery is currently dischargingC_CHARGEDβ battery is chargedC_NOINFOβ hardware doesn't give information about the stateC_ERRβ some error occurred while reading the charge state
enum fan_state_t
fan states
Enumerator:
F_ONβ fan is onF_OFFβ fan is offF_ERRβ some error occurred with this fan
enum power_state_t
Enumerator:
P_ACβ if computer runs on ACP_BATTβ if computer runs on batteryP_ERRβ no information can be found
enum thermal_mode_t
Enumerator:
CO_ACTβ fans will be turned after the temperature passes a critical pointCO_PASSβ devices will be put in a lower power state after a critical pointCO_CRITβ system goes into suspend to disk if possible after a critical temperatureCO_ERRβ some error occurred while reading the cooling mode
enum thermal_state_t
Enumerator:
T_CRITβ zone reports critical temperature, will cause system to go toS4T_HOTβ zone reports high temperature, will cause system to shutdown immediatelyT_PASSβ zone is on passive coolingT_ACTβ zone is on active cooling, more power consumptionT_OKβ zone is okT_ERRβ some error occurred while reading the state of the zone
β Variable Documentation β
battery_t batteries[MAX_ITEMS]
array for existing batteries, loop until globals->battery_count
fan_t fans[MAX_ITEMS]
array for existing fans, loop until globals->fan_count
thermal_t thermals[MAX_ITEMS]
array for existing thermal zones, loop until globals->thermal_count
π₯ Additional information π₯
the package ships a test-libacpi.c file which should be pretty self explanatory. also have a look into libacpi.h file and the doxygen documentation in the doc/ directory. iykyk πβ¨
π Homepage π
see http://www.ngolde.de/libacpi.html
π«Ά Author π«Ά
write to nico (at) ngolde (dot) de for suggestions, hardware donations (very welcome, my notebook doesn't support all features and I could need a second battery), questions, fixes, bug reports, thanks, etc. :) β yes that's a direct quote and yes he's literally asking for a second battery, respect the hustle π
that's the whole page bestie, go read your battery like a main character π
LIBACPI Β· libacpi(3) β mwah, no notes, periodt π
β¨
π¦ where this page comes from
- debian bookworm: libacpi-dev 0.2-7 Β· MIT
- debian trixie: libacpi-dev 0.2-7 Β· MIT
- ubuntu jammy: libacpi-dev 0.2-7 Β· MIT
- ubuntu noble: libacpi-dev 0.2-7build1 Β· MIT
- ubuntu questing: libacpi-dev 0.2-7build1
- ubuntu resolute: libacpi-dev 0.2-7build2 Β· MIT
- ubuntu stonking: libacpi-dev 0.2-7build2 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms