mcde: the battery in the bar

McdeSys gets mcde_sys_battery: all batteries as one, charging, mains,
minutes left.  NetBSD reads the envsys(4) dictionary of /dev/sysmon as
envstat(8) does (acpibat "present", "charge", "charging", "discharge
rate"; acpiacad "connected"), Linux /sys/class/power_supply (energy_* or
charge_*, status, Mains/USB online).  The sources are in
docs/reference/battery.txt.

dtbar shows a fourth icon, only when there is a battery: full, half, low
(red), charging; its menu says the charge, the power source and the time
left.  The status bar is 30 pixels an icon.

Measured: on the Linux host test_sysinfo says "battery 76% ac", as
/sys/class/power_supply/BAT0/capacity and AC/online do; the menu on Xvfb
(build/mcde-shots/battery-menu.png) says "Battery: 76 %", "Power Source:
Power Adapter".  The NetBSD backend compiles and runs in the build VM,
which has no battery (envstat: no drivers registered): only the
no-battery case is measured there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lotherkandClaude Opus 5.5 committed 2026-11-11 07:01:18 +00:00
1 parent 3969c15907
commit 451aac7fe4
13 files changed
+416 -19

No files matched your search

+7 -4
View File
@@ -81,10 +81,13 @@ place of CDE; pkgsrc builds it on any platform from `pkgsrc/`.
Right of the menus, left of the clock (`status.c`): volume (a slider in
the menu -- `menuscale.c` gives XmScale Motif's XmQTmenuSavvy trait with
XmeTraitSet, so a menu takes it; Mute; the wheel over the speaker),
Bluetooth (on/off) and network (the cards, not bridges or containers), all
through McdeSys; icons are CDE icon files (`icons/Mcde*.t.pm`). The status
menu bar has a fixed width: the form sized it before the icons and the bar
squeezed them to 10 pixels. Test sound only against a private PulseAudio
Bluetooth (on/off), network (the cards, not bridges or containers) and,
on a machine that has one, the battery (charge, power source, time left;
NetBSD's envsys(4) dictionary, Linux's /sys/class/power_supply,
`docs/reference/battery.txt` in the Makronix tree), all through McdeSys;
icons are CDE icon files (`icons/Mcde*.t.pm`). The status menu bar has a
fixed width, 30 pixels an icon (`StatusWidth`): the form sized it before
the icons and the bar squeezed them to 10 pixels. Test sound only against a private PulseAudio
(null sink, own socket): the slider changes the volume it reaches.
dtwm places and maximizes windows below the bar (`McdeExcludeBar` in
`programs/dtwm/WmMultiHead.c`); a full-screen window covers it.
+15
View File
@@ -16,6 +16,7 @@
*
* Status: lib/McdeSys implements mcde_sys_backend, mcde_sys_error,
* mcde_sys_info, mcde_sys_power, mcde_sys_interfaces, the sound, Bluetooth,
* battery,
* user and software update functions (common.c, net.c, sound.c, users.c,
* netbsd.c, linux.c); the network setup, host name and time zone setters
* are the contract still.
@@ -145,6 +146,20 @@ typedef struct {
int mcde_sys_bluetooth(McdeSysBluetooth *bt);
int mcde_sys_bluetooth_power(int on);
/*
* The battery: all batteries of the machine as one, and the mains
*/
typedef struct {
int present; /* there is a battery */
int percent; /* its charge, 0..100 */
int charging; /* it is being charged */
int ac; /* the machine runs on mains power */
int minutes; /* until it is empty, -1 when not known or
* not running on it */
} McdeSysBattery;
int mcde_sys_battery(McdeSysBattery *bat);
/*
* Software update: the system and the packages, from the update source the
* system names (NetBSD: the URL in /etc/makronix/update, a release directory
+2 -2
View File
@@ -8,8 +8,8 @@ libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include
libMcdeSys_la_SOURCES = common.c net.c sound.c users.c private.h
if NETBSD
libMcdeSys_la_SOURCES += netbsd.c
# crypt(3), for the password hash (netbsd.c)
libMcdeSys_la_LIBADD = -lcrypt
# crypt(3), for the password hash; proplib(3), for the battery (netbsd.c)
libMcdeSys_la_LIBADD = -lcrypt -lprop
else
libMcdeSys_la_SOURCES += linux.c
endif
+92 -1
View File
@@ -7,7 +7,8 @@
* /proc/cpuinfo for the processor, /sys/class/dmi/id for the machine,
* /proc/self/mounts for the startup disk; power through systemctl(1) where
* the system has it, else shutdown(8); /sys/class/net and iw(8) for Wi-Fi,
* amixer(1) when no sound server runs, bluetoothctl(1) for Bluetooth.
* amixer(1) when no sound server runs, bluetoothctl(1) for Bluetooth,
* /sys/class/power_supply for the battery.
*/
#define _POSIX_C_SOURCE 200809L
@@ -227,6 +228,96 @@ mcde_sys_bluetooth_power(int on)
return mcde_sys_run(argv);
}
/* ---- the battery: /sys/class/power_supply (the kernel's
* Documentation/ABI/testing/sysfs-class-power, docs/reference/battery.txt):
* a supply of type Battery has energy_now/energy_full in uWh and power_now in
* uW, or charge_now/charge_full in uAh and current_now in uA, and status
* Charging, Discharging, Full, Not charging; one of type Mains (or USB, a
* charger at the USB-C port) has online */
static long
Supply(const char *dir, const char *name)
{
char path[512], buf[64];
snprintf(path, sizeof path, "%s/%s", dir, name);
if (!FirstLine(path, buf, sizeof buf))
return -1;
return strtol(buf, NULL, 10);
}
int
mcde_sys_battery(McdeSysBattery *bat)
{
DIR *d;
struct dirent *de;
char dir[512], path[600], kind[32], status[32];
long long now = 0, full = 0, rate = 0;
long v, f, r;
int n = 0, capsum = 0, discharging = 0;
memset(bat, 0, sizeof *bat);
bat->minutes = -1;
if ((d = opendir("/sys/class/power_supply")) == NULL)
return MCDE_SYS_OK;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.')
continue;
snprintf(dir, sizeof dir, "/sys/class/power_supply/%s",
de->d_name);
snprintf(path, sizeof path, "%s/type", dir);
if (!FirstLine(path, kind, sizeof kind))
continue;
if (strcmp(kind, "Mains") == 0 || strcmp(kind, "USB") == 0) {
if (Supply(dir, "online") == 1)
bat->ac = 1;
continue;
}
if (strcmp(kind, "Battery") != 0 || Supply(dir, "present") == 0)
continue;
/* a device's battery (a mouse) has scope Device: not ours */
snprintf(path, sizeof path, "%s/scope", dir);
if (FirstLine(path, status, sizeof status) &&
strcmp(status, "Device") == 0)
continue;
n++;
v = Supply(dir, "energy_now");
f = Supply(dir, "energy_full");
r = Supply(dir, "power_now");
if (v < 0 || f <= 0) {
v = Supply(dir, "charge_now");
f = Supply(dir, "charge_full");
r = Supply(dir, "current_now");
}
if (v >= 0 && f > 0) {
now += v;
full += f;
if (r > 0)
rate += r;
} else if ((v = Supply(dir, "capacity")) >= 0)
capsum += (int) v;
snprintf(path, sizeof path, "%s/status", dir);
if (FirstLine(path, status, sizeof status)) {
if (strcmp(status, "Charging") == 0)
bat->charging = 1;
else if (strcmp(status, "Discharging") == 0)
discharging = 1;
}
}
closedir(d);
bat->present = n > 0;
if (full > 0)
bat->percent = (int) ((now * 100 + full / 2) / full);
else if (n > 0)
bat->percent = capsum / n;
if (bat->percent > 100)
bat->percent = 100;
if (bat->charging)
bat->ac = 1;
if (discharging && rate > 0 && full > 0)
bat->minutes = (int) (now * 60 / rate);
return MCDE_SYS_OK;
}
/* ---- users: the group sudo administers (Debian; wheel where there is no
* sudo group); chpasswd(8) takes the password on its standard input,
* gpasswd(1) adds to and removes from a group */
+104 -1
View File
@@ -8,11 +8,12 @@
* (based on NetBSD ...)") for the system, statvfs(2)'s f_mntfromname for the
* startup disk, shutdown(8) and zzz(8) for power, the SIOCG80211NWID ioctl
* for Wi-Fi, mixerctl(1) when no sound server runs, btconfig(8) for
* Bluetooth.
* Bluetooth, the envsys(4) dictionary for the battery.
*/
#define _NETBSD_SOURCE
#include <fcntl.h>
#include <grp.h>
#include <paths.h>
#include <pwd.h>
@@ -26,8 +27,10 @@
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/sysctl.h>
#include <sys/envsys.h>
#include <net/if.h>
#include <net80211/ieee80211_ioctl.h>
#include <prop/proplib.h>
#include <Mcde/McdeSys.h>
@@ -209,6 +212,106 @@ mcde_sys_bluetooth_power(int on)
return mcde_sys_run(argv);
}
/* ---- the battery: the envsys(4) dictionary of /dev/sysmon, read as
* envstat(8) reads it (src/usr.sbin/envstat/envstat.c:527): acpibat(4)
* publishes "present", "charge" (uWh, max-value the last full charge),
* "charging" and "discharge rate" (uW); acpiacad(4) "connected"
* (src/sys/dev/acpi/acpi_bat.c:710, acpi_acad.c:262;
* docs/reference/battery.txt). A sensor in state "invalid" says nothing. */
static int
Sensor(prop_array_t sensors, const char *desc, int64_t *cur, int64_t *max)
{
prop_object_iterator_t it;
prop_object_t s, o;
int found = 0;
if ((it = prop_array_iterator(sensors)) == NULL)
return 0;
while ((s = prop_object_iterator_next(it)) != NULL) {
o = prop_dictionary_get(s, "description");
if (o == NULL || !prop_string_equals_string(o, desc))
continue;
o = prop_dictionary_get(s, "state");
if (o != NULL && prop_string_equals_string(o, "invalid"))
break;
if ((o = prop_dictionary_get(s, "cur-value")) == NULL)
break;
*cur = prop_number_signed_value(o);
if (max != NULL) {
o = prop_dictionary_get(s, "max-value");
*max = o != NULL ? prop_number_signed_value(o) : 0;
}
found = 1;
break;
}
prop_object_iterator_release(it);
return found;
}
int
mcde_sys_battery(McdeSysBattery *bat)
{
prop_dictionary_t dict;
prop_object_iterator_t it;
prop_object_t key;
prop_array_t sensors;
const char *dev;
int64_t cur, max, now = 0, full = 0, rate = 0;
int fd, n = 0, discharging = 0;
memset(bat, 0, sizeof *bat);
bat->minutes = -1;
if ((fd = open(_PATH_SYSMON, O_RDONLY)) == -1)
return MCDE_SYS_OK;
if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict) != 0) {
close(fd);
return MCDE_SYS_OK;
}
close(fd);
if ((it = prop_dictionary_iterator(dict)) == NULL) {
prop_object_release(dict);
return MCDE_SYS_OK;
}
while ((key = prop_object_iterator_next(it)) != NULL) {
dev = prop_dictionary_keysym_value(key);
sensors = prop_dictionary_get_keysym(dict, key);
if (prop_object_type(sensors) != PROP_TYPE_ARRAY)
continue;
if (strncmp(dev, "acpiacad", 8) == 0) {
if (Sensor(sensors, "connected", &cur, NULL) && cur)
bat->ac = 1;
continue;
}
if (strncmp(dev, "acpibat", 7) != 0 ||
!Sensor(sensors, "present", &cur, NULL) || !cur)
continue;
n++;
if (Sensor(sensors, "charge", &cur, &max) && max > 0) {
now += cur;
full += max;
}
if (Sensor(sensors, "charging", &cur, NULL) && cur)
bat->charging = 1;
else if (Sensor(sensors, "discharge rate", &cur, NULL) &&
cur > 0) {
discharging = 1;
rate += cur;
}
}
prop_object_iterator_release(it);
prop_object_release(dict);
bat->present = n > 0;
if (full > 0)
bat->percent = (int) ((now * 100 + full / 2) / full);
if (bat->percent > 100)
bat->percent = 100;
if (bat->charging)
bat->ac = 1;
if (discharging && !bat->ac && rate > 0)
bat->minutes = (int) (now * 60 / rate);
return MCDE_SYS_OK;
}
/* ---- users: wheel administers (su(1) takes only wheel); the password is
* hashed here, as passwd(1) does, with pw_gensalt(3) and crypt(3), and only
* the hash goes to usermod(8) -- the password itself is on no command line */
+18
View File
@@ -69,6 +69,24 @@ main(void)
printf("bluetooth %s%s%s\n", bt.present ? bt.name : "none",
bt.present ? " " : "", bt.present ?
(bt.on ? "on" : "off") : "");
{
McdeSysBattery b;
if (mcde_sys_battery(&b) == MCDE_SYS_OK) {
if (b.present)
printf("battery %d%%%s%s, %d min\n",
b.percent, b.charging ?
" charging" : "", b.ac ? " ac" : "",
b.minutes);
else
printf("battery none%s\n",
b.ac ? " ac" : "");
if (b.percent < 0 || b.percent > 100) {
printf("FAIL battery out of range\n");
failures++;
}
}
}
}
{
McdeSysUser users[64], bad;
+4
View File
@@ -2124,6 +2124,10 @@ dt/appconfig/icons/C/IcMxbdy.t_m.bm
dt/appconfig/icons/C/MakronixDtlogo.m.pm
dt/appconfig/icons/C/MakronixDtlogo.pm
dt/appconfig/icons/C/MakronixDtlogo.t.pm
dt/appconfig/icons/C/McdeBatteryCharging.t.pm
dt/appconfig/icons/C/McdeBatteryFull.t.pm
dt/appconfig/icons/C/McdeBatteryHalf.t.pm
dt/appconfig/icons/C/McdeBatteryLow.t.pm
dt/appconfig/icons/C/McdeBluetooth.m.pm
dt/appconfig/icons/C/McdeBluetoothOff.t.pm
dt/appconfig/icons/C/McdeBluetoothOn.t.pm
+3 -1
View File
@@ -17,7 +17,9 @@ iconsdir = $(CDE_INSTALLATION_TOP)/appconfig/icons/C
dist_icons_DATA = icons/McdeVolumeHigh.t.pm icons/McdeVolumeLow.t.pm \
icons/McdeVolumeMuted.t.pm icons/McdeBluetoothOn.t.pm \
icons/McdeBluetoothOff.t.pm icons/McdeWifi.t.pm icons/McdeWired.t.pm \
icons/McdeNetworkOff.t.pm icons/McdeWired.m.pm icons/McdeBluetooth.m.pm
icons/McdeNetworkOff.t.pm icons/McdeWired.m.pm icons/McdeBluetooth.m.pm \
icons/McdeBatteryFull.t.pm icons/McdeBatteryHalf.t.pm \
icons/McdeBatteryLow.t.pm icons/McdeBatteryCharging.t.pm
check_PROGRAMS = test_items
test_items_SOURCES = test_items.c items.c items.h
@@ -0,0 +1,22 @@
/* XPM */
/* MCDE: dtbar, McdeBatteryCharging (programs/dtbar/status.c): charging */
static char *McdeBatteryCharging[] = {
"16 16 2 1",
" c None s background",
". c #000000",
" ",
" ",
" ",
" ",
".............. ",
". . . ",
". . ...",
". .... ...",
". . ...",
". . ...",
". . . ",
".............. ",
" ",
" ",
" ",
" "};
+22
View File
@@ -0,0 +1,22 @@
/* XPM */
/* MCDE: dtbar, McdeBatteryFull (programs/dtbar/status.c): charged */
static char *McdeBatteryFull[] = {
"16 16 2 1",
" c None s background",
". c #000000",
" ",
" ",
" ",
" ",
".............. ",
". . ",
". .......... ...",
". .......... ...",
". .......... ...",
". .......... ...",
". . ",
".............. ",
" ",
" ",
" ",
" "};
+22
View File
@@ -0,0 +1,22 @@
/* XPM */
/* MCDE: dtbar, McdeBatteryHalf (programs/dtbar/status.c): half */
static char *McdeBatteryHalf[] = {
"16 16 2 1",
" c None s background",
". c #000000",
" ",
" ",
" ",
" ",
".............. ",
". . ",
". ..... ...",
". ..... ...",
". ..... ...",
". ..... ...",
". . ",
".............. ",
" ",
" ",
" ",
" "};
+23
View File
@@ -0,0 +1,23 @@
/* XPM */
/* MCDE: dtbar, McdeBatteryLow (programs/dtbar/status.c): almost empty */
static char *McdeBatteryLow[] = {
"16 16 3 1",
" c None s background",
". c #000000",
"r c #cc0000",
" ",
" ",
" ",
" ",
".............. ",
". . ",
". rr ...",
". rr ...",
". rr ...",
". rr ...",
". . ",
".............. ",
" ",
" ",
" ",
" "};
+82 -10
View File
@@ -1,11 +1,13 @@
/*
* status.c -- dtbar: volume, Bluetooth and network at the right of the bar
* status.c -- dtbar: volume, Bluetooth, network and battery at the right of
* the bar
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* Three small menus with an icon each, left of the clock. What they show and
* change goes through McdeSys (the volume of the default output, the first
* Bluetooth controller, the network interfaces): dtbar itself never runs a
* Small menus with an icon each, left of the clock -- the battery only on a
* machine that has one. What they show and change goes through McdeSys (the
* volume of the default output, the first Bluetooth controller, the network
* interfaces, the batteries): dtbar itself never runs a
* system tool. The icons follow the state every ten seconds; a menu reads
* the state anew when it opens. The wheel over the speaker turns the volume.
*
@@ -78,8 +80,10 @@ SetIcon(Widget cascade, Pixmap *cache, const char *name, const char **shown)
static Widget volCascade, volLabel, volScale, volMute;
static Widget btCascade, btLabel, btPower;
static Widget netCascade, netLabels[NET_SLOTS];
static Widget batCascade, batLabel, batSource, batTime, statusBar;
static Pixmap pmHigh, pmLow, pmMuted, pmBtOn, pmBtOff, pmWifi, pmWired, pmNetOff;
static const char *volShown, *btShown, *netShown;
static Pixmap pmBatFull, pmBatHalf, pmBatLow, pmBatCharging;
static const char *volShown, *btShown, *netShown, *batShown;
static int volume = -1, muted;
static McdeSysBluetooth bt;
@@ -286,6 +290,63 @@ ReadNetwork(void)
SetIcon(netCascade, &pmNetOff, "McdeNetworkOff", &netShown);
}
/* ---- battery ------------------------------------------------------------ */
/* the bar is as wide as its icons (MakeStatus) */
static void
StatusWidth(void)
{
int icons = XtIsManaged(batCascade) ? 4 : 3;
XtVaSetValues(statusBar, XmNwidth, icons * 30 + 12, NULL);
}
static void
ReadBattery(void)
{
McdeSysBattery b;
char text[64];
(void) mcde_sys_battery(&b);
if (!b.present) {
if (XtIsManaged(batCascade)) {
XtUnmanageChild(batCascade);
StatusWidth();
}
return;
}
if (b.charging)
snprintf(text, sizeof text, "Battery: %d%%, charging",
b.percent);
else if (b.ac && b.percent >= 99)
snprintf(text, sizeof text, "Battery: charged");
else
snprintf(text, sizeof text, "Battery: %d%%", b.percent);
Label(batLabel, text);
Label(batSource, b.ac ? "Power Source: Power Adapter" :
"Power Source: Battery");
if (b.minutes >= 0) {
snprintf(text, sizeof text, "%d:%02d remaining",
b.minutes / 60, b.minutes % 60);
Label(batTime, text);
XtManageChild(batTime);
} else
XtUnmanageChild(batTime);
if (b.charging)
SetIcon(batCascade, &pmBatCharging, "McdeBatteryCharging",
&batShown);
else if (b.percent >= 67)
SetIcon(batCascade, &pmBatFull, "McdeBatteryFull", &batShown);
else if (b.percent >= 20)
SetIcon(batCascade, &pmBatHalf, "McdeBatteryHalf", &batShown);
else
SetIcon(batCascade, &pmBatLow, "McdeBatteryLow", &batShown);
if (!XtIsManaged(batCascade)) {
XtManageChild(batCascade);
StatusWidth();
}
}
/* ---- the menus ---------------------------------------------------------- */
static void
@@ -297,6 +358,7 @@ Opening(Widget w, XtPointer data, XtPointer call)
case 0: ReadVolume(); break;
case 1: ReadBluetooth(); break;
case 2: ReadNetwork(); break;
case 3: ReadBattery(); break;
}
}
@@ -343,13 +405,14 @@ Poll(XtPointer data, XtIntervalId *id)
ReadVolume();
ReadBluetooth();
ReadNetwork();
ReadBattery();
XtAppAddTimeOut((XtAppContext) data, 10000, Poll, data);
}
Widget
MakeStatus(Widget parent)
{
Widget bar = XmCreateMenuBar(parent, "status", NULL, 0);
Widget bar = statusBar = XmCreateMenuBar(parent, "status", NULL, 0);
Widget m;
int i;
@@ -394,10 +457,19 @@ MakeStatus(Widget parent)
XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, m, NULL);
Settings(m, "Network Settings...", "dtsystemsettings -pane network");
/* three icons wide, fixed: the form gave the menu bar the width it had
* before the icons were measured, and the bar squeezed its buttons to
* 10 pixels (measured, MCDE_GLOBAL_MENU_DEBUG) */
XtVaSetValues(bar, XmNwidth, 3 * 30 + 12, XmNresizeWidth, False, NULL);
/* hidden until a battery is found (ReadBattery) */
batCascade = Menu(bar, "battery", &m, 3, &pmBatFull, "McdeBatteryFull",
&batShown);
batLabel = Item(m, xmLabelGadgetClass, "Battery");
batSource = Item(m, xmLabelGadgetClass, "Power Source");
batTime = Item(m, xmLabelGadgetClass, "");
XtUnmanageChild(batCascade);
/* as wide as its icons, fixed: the form gave the menu bar the width it
* had before the icons were measured, and the bar squeezed its buttons
* to 10 pixels (measured, MCDE_GLOBAL_MENU_DEBUG) */
XtVaSetValues(bar, XmNresizeWidth, False, NULL);
StatusWidth();
XtManageChild(bar);
return bar;
}