diff --git a/AGENTS.md b/AGENTS.md index 516e9887..8c6e8966 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index ea2ec8b0..ae6c306d 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -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 diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index 65a5834a..118d34e5 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -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 diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 4b8e5e18..5f7516c0 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -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 */ diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index 35c0b969..d4697804 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -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 #include #include #include @@ -26,8 +27,10 @@ #include #include #include +#include #include #include +#include #include @@ -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 */ diff --git a/lib/McdeSys/test_sysinfo.c b/lib/McdeSys/test_sysinfo.c index 7729e911..1892a53e 100644 --- a/lib/McdeSys/test_sysinfo.c +++ b/lib/McdeSys/test_sysinfo.c @@ -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; diff --git a/pkgsrc/PLIST b/pkgsrc/PLIST index a5ab3c07..59704486 100644 --- a/pkgsrc/PLIST +++ b/pkgsrc/PLIST @@ -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 diff --git a/programs/dtbar/Makefile.am b/programs/dtbar/Makefile.am index 980acfbb..e55a1f37 100644 --- a/programs/dtbar/Makefile.am +++ b/programs/dtbar/Makefile.am @@ -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 diff --git a/programs/dtbar/icons/McdeBatteryCharging.t.pm b/programs/dtbar/icons/McdeBatteryCharging.t.pm new file mode 100644 index 00000000..5674b23d --- /dev/null +++ b/programs/dtbar/icons/McdeBatteryCharging.t.pm @@ -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", +" ", +" ", +" ", +" ", +".............. ", +". . . ", +". . ...", +". .... ...", +". . ...", +". . ...", +". . . ", +".............. ", +" ", +" ", +" ", +" "}; diff --git a/programs/dtbar/icons/McdeBatteryFull.t.pm b/programs/dtbar/icons/McdeBatteryFull.t.pm new file mode 100644 index 00000000..afe9c53f --- /dev/null +++ b/programs/dtbar/icons/McdeBatteryFull.t.pm @@ -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", +" ", +" ", +" ", +" ", +".............. ", +". . ", +". .......... ...", +". .......... ...", +". .......... ...", +". .......... ...", +". . ", +".............. ", +" ", +" ", +" ", +" "}; diff --git a/programs/dtbar/icons/McdeBatteryHalf.t.pm b/programs/dtbar/icons/McdeBatteryHalf.t.pm new file mode 100644 index 00000000..14c219ef --- /dev/null +++ b/programs/dtbar/icons/McdeBatteryHalf.t.pm @@ -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", +" ", +" ", +" ", +" ", +".............. ", +". . ", +". ..... ...", +". ..... ...", +". ..... ...", +". ..... ...", +". . ", +".............. ", +" ", +" ", +" ", +" "}; diff --git a/programs/dtbar/icons/McdeBatteryLow.t.pm b/programs/dtbar/icons/McdeBatteryLow.t.pm new file mode 100644 index 00000000..bcd72c46 --- /dev/null +++ b/programs/dtbar/icons/McdeBatteryLow.t.pm @@ -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 ...", +". . ", +".............. ", +" ", +" ", +" ", +" "}; diff --git a/programs/dtbar/status.c b/programs/dtbar/status.c index 285af66b..81e6f22c 100644 --- a/programs/dtbar/status.c +++ b/programs/dtbar/status.c @@ -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; }