Skip to content

Commit

Permalink
Implement get_battery_power_draw() for FreeBSD.
Browse files Browse the repository at this point in the history
I'm using the acpi_battery(4) interface, via sysctl.

It should report battery draw in mW (for milli Watts), so I'm
dividing by 1000 before printing in the buffer.
  • Loading branch information
madpilot78 committed Oct 13, 2022
1 parent 43a1043 commit c669cd2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/freebsd.cc
Expand Up @@ -460,6 +460,27 @@ int get_battery_perct(const char *) {
return batcapacity;
}

void get_battery_power_draw(char *buffer, unsigned int n, const char *bat) {
int rate = 0;
double ret = 0;

/*
* hw.acpi.battery.rate returns battery discharge rate in mW,
* or -1 (according to docs, but also 0 in practice) when not discharging.
*
* ref. acpi_battery(4)
*/
if (GETSYSCTL("hw.acpi.battery.rate", rate)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.rate\"\n");
}

if (rate > 0) {
ret = (double)rate/(double)1000;
}

snprintf(buffer, n, "%.1f", ret);
}

double get_battery_perct_bar(struct text_object *obj) {
int batperct = get_battery_perct(obj->data.s);
return batperct;
Expand Down

0 comments on commit c669cd2

Please sign in to comment.