Files
mcde/programs/dtbar/status.c
T
lotherkandClaude Opus 5.5 451aac7fe4 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>
2026-11-11 07:01:18 +00:00

509 lines
14 KiB
C

/*
* 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.
*
* 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.
*
* The menus keep their entries and change only their labels: a pulldown that
* gets new children while Motif posts it closes again.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/CascadeB.h>
#include <Xm/CascadeBG.h>
#include <Xm/LabelG.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/SeparatoG.h>
#include <Xm/ToggleBG.h>
#include <Mcde/McdeSys.h>
#include "dtbar.h"
/* ---- the icons: 16x16 icon files of CDE's kind (icons/Mcde*.t.pm, in
* appconfig/icons/C), loaded by XmGetPixmap: Motif then knows their size --
* a pixmap made here was sized 10 pixels wide by the menu bar under dtwm */
static Pixmap
Icon(Widget w, const char *name)
{
char path[512];
Pixel fg, bg;
Pixmap pm;
/* "None" in the file becomes the bar's background (XmGetPixmap); W is
* a gadget, with no screen of its own (XtScreen would crash) */
XtVaGetValues(XtIsWidget(w) ? w : XtParent(w), XmNforeground, &fg,
XmNbackground, &bg, NULL);
snprintf(path, sizeof path, "%s/appconfig/icons/C/%s.t.pm",
CDE_INSTALLATION_TOP, name);
pm = XmGetPixmap(XtScreenOfObject(w), path, fg, bg);
if (pm == XmUNSPECIFIED_PIXMAP)
fprintf(stderr, "dtbar: no icon %s\n", path);
return pm;
}
static void
SetIcon(Widget cascade, Pixmap *cache, const char *name, const char **shown)
{
if (*shown != NULL && strcmp(*shown, name) == 0)
return;
if (*cache == None)
*cache = Icon(cascade, name);
XtVaSetValues(cascade, XmNlabelType, XmPIXMAP, XmNlabelPixmap, *cache,
NULL);
*shown = name;
}
/* ---- the widgets -------------------------------------------------------- */
#define NET_SLOTS 6
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 Pixmap pmBatFull, pmBatHalf, pmBatLow, pmBatCharging;
static const char *volShown, *btShown, *netShown, *batShown;
static int volume = -1, muted;
static McdeSysBluetooth bt;
static void
Label(Widget w, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(w, XmNlabelString, s, NULL);
XmStringFree(s);
}
static Widget
Item(Widget menu, WidgetClass class, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
Widget w = XtVaCreateManagedWidget("item", class, menu,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XmStringFree(s);
return w;
}
/* ---- volume ------------------------------------------------------------- */
static void
VolumeIcon(void)
{
if (volume < 0 || muted || volume == 0)
SetIcon(volCascade, &pmMuted, "McdeVolumeMuted", &volShown);
else if (volume < 50)
SetIcon(volCascade, &pmLow, "McdeVolumeLow", &volShown);
else
SetIcon(volCascade, &pmHigh, "McdeVolumeHigh", &volShown);
}
static void
ReadVolume(void)
{
char text[64];
if (mcde_sys_volume_get(&volume, &muted) != MCDE_SYS_OK) {
volume = -1;
Label(volLabel, "No sound output");
XtSetSensitive(volScale, False);
XtSetSensitive(volMute, False);
} else {
snprintf(text, sizeof text, "Volume: %d %%", volume);
Label(volLabel, text);
XtSetSensitive(volScale, True);
XtSetSensitive(volMute, True);
XmScaleSetValue(volScale, volume > 100 ? 100 : volume);
XmToggleButtonGadgetSetState(volMute, muted, False);
}
VolumeIcon();
}
static void
VolumeChanged(Widget w, XtPointer data, XtPointer call)
{
XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct *) call;
char text[64];
(void) w;
(void) data;
if (mcde_sys_volume_set(cbs->value) == MCDE_SYS_OK) {
volume = cbs->value;
snprintf(text, sizeof text, "Volume: %d %%", volume);
Label(volLabel, text);
VolumeIcon();
}
}
static void
MuteChanged(Widget w, XtPointer data, XtPointer call)
{
XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct *) call;
(void) w;
(void) data;
if (mcde_sys_volume_mute(cbs->set) == MCDE_SYS_OK)
muted = cbs->set;
VolumeIcon();
}
/* the wheel over the speaker: five steps a click */
static void
Wheel(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
{
int v;
(void) w;
(void) data;
(void) cont;
if (ev->type != ButtonPress ||
(ev->xbutton.button != Button4 && ev->xbutton.button != Button5))
return;
/* over the speaker only (the cascades are gadgets of the bar) */
{
Position x = 0;
Dimension width = 0;
XtVaGetValues(volCascade, XmNx, &x, XmNwidth, &width, NULL);
if (ev->xbutton.x < x || ev->xbutton.x >= x + (int) width)
return;
}
if (volume < 0)
ReadVolume();
if (volume < 0)
return;
v = volume + (ev->xbutton.button == Button4 ? 5 : -5);
if (v < 0)
v = 0;
if (v > 100)
v = 100;
if (mcde_sys_volume_set(v) == MCDE_SYS_OK)
volume = v;
VolumeIcon();
}
/* ---- Bluetooth ---------------------------------------------------------- */
static void
ReadBluetooth(void)
{
(void) mcde_sys_bluetooth(&bt);
if (!bt.present) {
Label(btLabel, "No Bluetooth controller");
Label(btPower, "Turn Bluetooth On");
XtSetSensitive(btPower, False);
} else {
Label(btLabel, bt.on ? "Bluetooth: On" : "Bluetooth: Off");
Label(btPower, bt.on ? "Turn Bluetooth Off" :
"Turn Bluetooth On");
XtSetSensitive(btPower, True);
}
if (bt.present && bt.on)
SetIcon(btCascade, &pmBtOn, "McdeBluetoothOn", &btShown);
else
SetIcon(btCascade, &pmBtOff, "McdeBluetoothOff", &btShown);
}
static void
BluetoothPower(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (mcde_sys_bluetooth_power(!bt.on) != MCDE_SYS_OK)
ShowMessage("Bluetooth", mcde_sys_error(), 1);
ReadBluetooth();
}
/* ---- network ------------------------------------------------------------ */
static void
ReadNetwork(void)
{
McdeSysInterface ifs[32];
int n = mcde_sys_interfaces(ifs, 32), i, slot = 0;
int wifiUp = 0, wiredUp = 0;
char text[160];
for (i = 0; i < n && slot < NET_SLOTS; i++) {
const McdeSysInterface *it = &ifs[i];
/* the network cards, not bridges, tunnels and containers */
if (it->kind == MCDE_NET_OTHER)
continue;
if (it->kind == MCDE_NET_WIFI) {
if (it->up)
snprintf(text, sizeof text, "Wi-Fi (%s): %s%s%s",
it->name, it->ssid[0] != '\0' ? it->ssid : "",
it->ssid[0] != '\0' ? ", " : "", it->address);
else
snprintf(text, sizeof text,
"Wi-Fi (%s): not connected", it->name);
wifiUp |= it->up;
} else {
if (it->up)
snprintf(text, sizeof text, "Ethernet (%s): %s",
it->name, it->address);
else
snprintf(text, sizeof text,
"Ethernet (%s): no cable", it->name);
wiredUp |= it->up;
}
Label(netLabels[slot], text);
XtManageChild(netLabels[slot]);
slot++;
}
if (slot == 0) {
Label(netLabels[0], "No network");
XtManageChild(netLabels[0]);
slot = 1;
}
for (; slot < NET_SLOTS; slot++)
XtUnmanageChild(netLabels[slot]);
if (wiredUp)
SetIcon(netCascade, &pmWired, "McdeWired", &netShown);
else if (wifiUp)
SetIcon(netCascade, &pmWifi, "McdeWifi", &netShown);
else
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
Opening(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
switch ((int) (size_t) data) {
case 0: ReadVolume(); break;
case 1: ReadBluetooth(); break;
case 2: ReadNetwork(); break;
case 3: ReadBattery(); break;
}
}
/* the cascade gets its first icon when it is made: Motif sizes a label in
* Initialize, and a pixmap set later left the button 10 pixels wide */
static Widget
Menu(Widget bar, const char *name, Widget *pulldown, int which,
Pixmap *cache, const char *icon, const char **shown)
{
Widget c;
*pulldown = XmCreatePulldownMenu(bar, (char *) name, NULL, 0);
*cache = Icon(bar, icon);
*shown = icon;
c = XtVaCreateManagedWidget(name, xmCascadeButtonWidgetClass, bar,
XmNsubMenuId, *pulldown, XmNlabelType, XmPIXMAP,
XmNlabelPixmap, *cache, NULL);
XtAddCallback(c, XmNcascadingCallback, Opening,
(XtPointer) (size_t) which);
return c;
}
static void
OpenSettings(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
RunCommand((const char *) data);
}
/* "... Settings...": the pane of dtsystemsettings */
static void
Settings(Widget menu, const char *label, const char *command)
{
Widget b = Item(menu, xmPushButtonGadgetClass, label);
XtAddCallback(b, XmNactivateCallback, OpenSettings, (XtPointer) command);
}
static void
Poll(XtPointer data, XtIntervalId *id)
{
(void) id;
ReadVolume();
ReadBluetooth();
ReadNetwork();
ReadBattery();
XtAppAddTimeOut((XtAppContext) data, 10000, Poll, data);
}
Widget
MakeStatus(Widget parent)
{
Widget bar = statusBar = XmCreateMenuBar(parent, "status", NULL, 0);
Widget m;
int i;
XtVaSetValues(bar, XmNshadowThickness, 0, XmNmarginHeight, 0, NULL);
volCascade = Menu(bar, "volume", &m, 0, &pmMuted, "McdeVolumeMuted",
&volShown);
volLabel = Item(m, xmLabelGadgetClass, "Volume");
{
Arg a[6];
Cardinal n = 0;
/* a slider Motif takes as a menu entry (menuscale.c) */
XtSetArg(a[n], XmNorientation, XmHORIZONTAL); n++;
XtSetArg(a[n], XmNminimum, 0); n++;
XtSetArg(a[n], XmNmaximum, 100); n++;
XtSetArg(a[n], XmNshowValue, False); n++;
XtSetArg(a[n], XmNscaleWidth, 180); n++;
volScale = MenuScale(m, "scale", a, n);
}
XtAddCallback(volScale, XmNvalueChangedCallback, VolumeChanged, NULL);
XtAddCallback(volScale, XmNdragCallback, VolumeChanged, NULL);
volMute = Item(m, xmToggleButtonGadgetClass, "Mute");
XtVaSetValues(volMute, XmNvisibleWhenOff, True, NULL);
XtAddCallback(volMute, XmNvalueChangedCallback, MuteChanged, NULL);
XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, m, NULL);
Settings(m, "Sound Settings...", "dtsystemsettings -pane sound");
XtAddEventHandler(bar, ButtonPressMask, False, Wheel, NULL);
btCascade = Menu(bar, "bluetooth", &m, 1, &pmBtOff, "McdeBluetoothOff",
&btShown);
btLabel = Item(m, xmLabelGadgetClass, "Bluetooth");
btPower = Item(m, xmPushButtonGadgetClass, "Turn Bluetooth Off");
XtAddCallback(btPower, XmNactivateCallback, BluetoothPower, NULL);
XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, m, NULL);
Settings(m, "Bluetooth Settings...", "dtsystemsettings -pane bluetooth");
netCascade = Menu(bar, "network", &m, 2, &pmNetOff, "McdeNetworkOff",
&netShown);
for (i = 0; i < NET_SLOTS; i++)
netLabels[i] = Item(m, xmLabelGadgetClass, "");
XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, m, NULL);
Settings(m, "Network Settings...", "dtsystemsettings -pane network");
/* 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;
}
void
StartStatus(XtAppContext app)
{
Poll((XtPointer) app, NULL);
if (getenv("MCDE_GLOBAL_MENU_DEBUG")) {
fprintf(stderr, "dtbar: volume %d muted %d (%s)\n", volume, muted,
volume < 0 ? mcde_sys_error() : "ok");
{
Dimension h = 0;
XtVaGetValues(XtParent(volCascade), XmNheight, &h, NULL);
fprintf(stderr, "dtbar: status bar height %u\n", h);
}
Widget c[3];
int i;
c[0] = volCascade;
c[1] = btCascade;
c[2] = netCascade;
for (i = 0; i < 3; i++) {
Dimension w = 0, h = 0;
Pixmap pm = None;
unsigned char type = 0;
XtVaGetValues(c[i], XmNwidth, &w, XmNheight, &h,
XmNlabelPixmap, &pm, XmNlabelType, &type, NULL);
fprintf(stderr, "dtbar: %s %ux%u pixmap 0x%lx type %d "
"managed %d\n", XtName(c[i]), w, h,
(unsigned long) pm, type, XtIsManaged(c[i]));
}
}
}