openbsd-ports/x11/i3status/patches/patch-src_print_battery_info_c
2019-07-10 18:40:58 +00:00

81 lines
2.8 KiB
Plaintext

$OpenBSD: patch-src_print_battery_info_c,v 1.18 2019/07/10 18:40:58 jasper Exp $
From 3a51673c05142b99f8db6a0bd9b8e4b806efeb72 Mon Sep 17 00:00:00 2001
From: Jasper Lievisse Adriaanse <jasper@humppa.nl>
Date: Mon, 8 Jul 2019 17:53:25 +0200
Subject: [PATCH] Extend battery handling on OpenBSD (#351)
Index: src/print_battery_info.c
--- src/print_battery_info.c.orig
+++ src/print_battery_info.c
@@ -31,6 +31,8 @@
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/sensors.h>
#endif
#if defined(__NetBSD__)
@@ -269,11 +271,16 @@ static bool slurp_battery_info(struct battery_info *ba
#elif defined(__OpenBSD__)
/*
* We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
- * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
- * probing acpi(4) devices.
+ * the generic interface on macppc/sparc64/zaurus. Machines that have ACPI
+ * battery sensors gain some extra information.
*/
struct apm_power_info apm_info;
+ struct sensordev sensordev;
+ struct sensor sensor;
+ size_t sdlen, slen;
int apm_fd;
+ int dev, mib[5] = {CTL_HW, HW_SENSORS, 0, 0, 0};
+ int volts = 0;
apm_fd = open("/dev/apm", O_RDONLY);
if (apm_fd < 0) {
@@ -310,6 +317,41 @@ static bool slurp_battery_info(struct battery_info *ba
/* Can't give a meaningful value for remaining minutes if we're charging. */
if (batt_info->status != CS_CHARGING) {
batt_info->seconds_remaining = apm_info.minutes_left * 60;
+ }
+
+ /* If acpibat* are present, check sensors for data not present via APM. */
+ batt_info->present_rate = 0;
+ sdlen = sizeof(sensordev);
+ slen = sizeof(sensor);
+
+ for (dev = 0;; dev++) {
+ mib[2] = dev;
+ if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
+ break;
+ }
+ /* 'path' is the node within the full path */
+ if (BEGINS_WITH(sensordev.xname, "acpibat")) {
+ /* power0 */
+ mib[3] = SENSOR_WATTS;
+ mib[4] = 0;
+ if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
+ /* try current0 */
+ mib[3] = SENSOR_AMPS;
+ if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1)
+ continue;
+ volts = sensor.value;
+
+ /* we also need current voltage to convert amps to watts */
+ mib[3] = SENSOR_VOLTS_DC;
+ mib[4] = 1;
+ if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1)
+ continue;
+
+ batt_info->present_rate += (((float)volts / 1000.0) * ((float)sensor.value / 1000.0));
+ } else {
+ batt_info->present_rate += sensor.value;
+ }
+ }
}
#elif defined(__NetBSD__)
/*