Files
mcde/programs/dtsystemsettings/main.c
T
lotherkandClaude Opus 5.5 8ab668c689 mcde: Accounts and Software Update, in dtsystemsettings and McdeSys
McdeSys: mcde_sys_users/user_add/user_delete (users.c, POSIX getpwent and
useradd/userdel), user_set_password and user_set_admin per system (NetBSD:
pw_gensalt+crypt for usermod -p, wheel; a group member removed by rewriting
the group's line as user.c does, since usermod -G only appends -- measured;
Linux: chpasswd on stdin, gpasswd, sudo), mcde_sys_update_check/apply
(NetBSD: /etc/makronix/update, RELEASE, sysupgrade, pkg_add -uu; Linux:
apt-get).  A secret goes through a pipe, never on a command line.
Tests: test_sysinfo refuses a bad login and, not as root, adding (PASS on
Linux and NetBSD); as root in the NetBSD VM add/admin on/off/password/delete
pass and /etc/group is as before.

dtsystemsettings: Accounts (list, Add/Delete/Password/Administrator,
Unlock... through su -m root in a terminal, password fields show stars),
Software Update (running, available, new packages, source, Check Now,
Update Now... runs -apply-update as root in a terminal; the check runs
after the pane shows: apt took 21 s here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 12:38:21 +00:00

666 lines
18 KiB
C

/*
* main.c -- dtsystemsettings: the settings of the computer in one window
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The window shows every pane as an icon, in four bands (panes.c): Personal,
* Hardware, Internet & Network, System. A click opens the pane in the same
* window, "Show All" goes back. Panes drawn here take what they show and
* change from McdeSys (include/Mcde/McdeSys.h) and never run a system tool;
* the others start the program that has them (CDE's style manager, the
* printer tool). dtsystemsettings -pane ID opens a pane at once (the menus
* of the top bar do that).
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/Separator.h>
#include <Xm/ToggleB.h>
#include <Dt/Dt.h>
#include <Mcde/McdeSys.h>
#include "panes.h"
#include "ui.h"
XtAppContext app;
static Widget shell, title, showAll, overview, paneArea, current;
static const Pane *shown;
static XtIntervalId clockTimer;
static const char *fallback[] = {
"Dtsystemsettings*overview*category.marginHeight: 6",
"Dtsystemsettings*paneArea.marginWidth: 20",
"Dtsystemsettings*paneArea.marginHeight: 16",
NULL
};
/* ---- small helpers ------------------------------------------------------ */
XmRenderTable
PaneFont(Widget w, int size, Boolean bold)
{
XmRendition r;
XmRenderTable rt;
Arg a[4];
int n = 0;
XtSetArg(a[n], XmNfontType, XmFONT_IS_XFT); n++;
XtSetArg(a[n], XmNfontName, "Hack"); n++;
XtSetArg(a[n], XmNfontSize, size); n++;
if (bold) {
XtSetArg(a[n], XmNfontStyle, "Bold"); n++;
}
r = XmRenditionCreate(w, XmFONTLIST_DEFAULT_TAG, a, n);
rt = XmRenderTableAddRenditions(NULL, &r, 1, XmMERGE_REPLACE);
XmRenditionFree(r);
return rt;
}
Widget
Text(Widget parent, const char *name, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
Widget w = XtVaCreateManagedWidget(name, xmLabelWidgetClass, parent,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XmStringFree(s);
return w;
}
void
SetText(Widget w, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(w, XmNlabelString, s, NULL);
XmStringFree(s);
}
Widget
Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer data)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass,
parent, XmNlabelString, s, NULL);
XmStringFree(s);
if (cb != NULL)
XtAddCallback(b, XmNactivateCallback, cb, data);
return b;
}
/* a button in a pane, its own width (a column would stretch it across) */
Widget
PaneButton(Widget parent, const char *label, XtCallbackProc cb, XtPointer data)
{
Widget row = XtVaCreateManagedWidget("buttonRow", xmRowColumnWidgetClass,
parent, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
return Button(row, label, cb, data);
}
/* a column of KEY VALUE lines; the keys right and bold */
Widget
Table(Widget parent, const char *const *rows, int n, Widget *values)
{
Widget rc = XtVaCreateManagedWidget("table", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_COLUMN,
XmNnumColumns, 2, XmNisAligned, False, XmNspacing, 3, NULL);
XmRenderTable bold = PaneFont(parent, 10, True);
int i;
for (i = 0; i < n; i++) {
Widget k = Text(rc, "key", rows[2 * i]);
XtVaSetValues(k, XmNalignment, XmALIGNMENT_END,
XmNrenderTable, bold, NULL);
}
for (i = 0; i < n; i++) {
Widget v = Text(rc, "value", rows[2 * i + 1]);
if (values != NULL)
values[i] = v;
}
return rc;
}
Pixmap
Icon(Widget w, const char *file)
{
char path[512];
Pixel fg, bg;
XtVaGetValues(w, XmNforeground, &fg, XmNbackground, &bg, NULL);
snprintf(path, sizeof path, "%s/appconfig/icons/C/%s",
CDE_INSTALLATION_TOP, file);
return XmGetPixmap(XtScreen(w), path, fg, bg);
}
/* start a command line in a session of its own, no zombie: the child
* forks the program and ends at once */
void
Spawn(const char *command)
{
pid_t pid = fork();
if (pid == 0) {
if (fork() == 0) {
setsid();
execl("/bin/sh", "sh", "-c", command, (char *) NULL);
_exit(127);
}
_exit(0);
}
if (pid > 0)
while (waitpid(pid, NULL, 0) < 0 && errno == EINTR)
;
}
static void
MessageClosed(Widget w, XtPointer data, XtPointer call)
{
(void) data;
(void) call;
XtDestroyWidget(XtParent(w));
}
void
Message(Widget parent, const char *text, int error)
{
XmString m = XmStringCreateLocalized((char *) text);
Arg a[2];
Widget d;
XtSetArg(a[0], XmNmessageString, m);
d = error ? XmCreateErrorDialog(parent, "message", a, 1) :
XmCreateInformationDialog(parent, "message", a, 1);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, MessageClosed, NULL);
XtManageChild(d);
XmStringFree(m);
}
/* su -m keeps DISPLAY and XAUTHORITY: the program as root shows on this
* display; su(1) asks for the password in the terminal */
void
AsRoot(const char *title, const char *command, char *buf, size_t len)
{
snprintf(buf, len, "dtterm -title '%s' -geometry 70x12 -e su -m root "
"-c '%s'", title, command);
}
/* ---- the sound pane ----------------------------------------------------- */
static Widget soundState, soundScale, soundMute;
static void
SoundRead(void)
{
int vol = 0, muted = 0;
if (mcde_sys_volume_get(&vol, &muted) != MCDE_SYS_OK) {
SetText(soundState, "There is no sound output.");
XtSetSensitive(soundScale, False);
XtSetSensitive(soundMute, False);
return;
}
SetText(soundState, "The volume of the sound output:");
XmScaleSetValue(soundScale, vol > 100 ? 100 : vol);
XmToggleButtonSetState(soundMute, muted, False);
}
static void
SoundVolume(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) mcde_sys_volume_set(((XmScaleCallbackStruct *) call)->value);
}
static void
SoundMute(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) mcde_sys_volume_mute(((XmToggleButtonCallbackStruct *) call)->set);
}
static Widget
MakeSound(Widget parent)
{
Widget box = XtVaCreateWidget("sound", xmRowColumnWidgetClass, parent,
XmNspacing, 10, NULL);
soundState = Text(box, "state", "");
soundScale = XtVaCreateManagedWidget("volume", xmScaleWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNminimum, 0, XmNmaximum, 100,
XmNshowValue, True, XmNscaleWidth, 320, NULL);
XtAddCallback(soundScale, XmNvalueChangedCallback, SoundVolume, NULL);
soundMute = XtVaCreateManagedWidget("Mute", xmToggleButtonWidgetClass,
box, NULL);
XtAddCallback(soundMute, XmNvalueChangedCallback, SoundMute, NULL);
SoundRead();
return box;
}
/* ---- the network pane --------------------------------------------------- */
#define NET_ROWS 8
static Widget netValues[NET_ROWS], netKeys[NET_ROWS];
static void
NetworkRead(void)
{
McdeSysInterface ifs[32];
int n = mcde_sys_interfaces(ifs, 32), i, row = 0;
char key[64], value[160];
for (i = 0; i < n && row < NET_ROWS; i++) {
const McdeSysInterface *it = &ifs[i];
if (it->kind == MCDE_NET_OTHER)
continue;
snprintf(key, sizeof key, "%s (%s)", it->kind == MCDE_NET_WIFI ?
"Wi-Fi" : "Ethernet", it->name);
if (!it->up)
snprintf(value, sizeof value, "%s",
it->kind == MCDE_NET_WIFI ? "not connected" :
"no cable");
else if (it->ssid[0] != '\0')
snprintf(value, sizeof value, "%s, address %s", it->ssid,
it->address);
else
snprintf(value, sizeof value, "address %s", it->address);
SetText(netKeys[row], key);
SetText(netValues[row], value);
XtManageChild(netKeys[row]);
XtManageChild(netValues[row]);
row++;
}
if (row == 0) {
SetText(netKeys[0], "Network");
SetText(netValues[0], "no network card");
XtManageChild(netKeys[0]);
XtManageChild(netValues[0]);
row = 1;
}
for (; row < NET_ROWS; row++) {
XtUnmanageChild(netKeys[row]);
XtUnmanageChild(netValues[row]);
}
}
static void
NetworkRefresh(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
NetworkRead();
}
static Widget
MakeNetwork(Widget parent)
{
Widget box = XtVaCreateWidget("network", xmRowColumnWidgetClass, parent,
XmNspacing, 10, NULL);
const char *rows[2 * NET_ROWS];
Widget table;
WidgetList kids;
int i;
Text(box, "intro", "The network cards of this computer and their "
"addresses:");
for (i = 0; i < NET_ROWS; i++) {
rows[2 * i] = "";
rows[2 * i + 1] = "";
}
table = Table(box, rows, NET_ROWS, netValues);
XtVaGetValues(table, XmNchildren, &kids, NULL);
for (i = 0; i < NET_ROWS; i++)
netKeys[i] = kids[i];
PaneButton(box, "Refresh", NetworkRefresh, NULL);
NetworkRead();
return box;
}
/* ---- the Bluetooth pane ------------------------------------------------- */
static Widget btState, btButton;
static McdeSysBluetooth bt;
static void
BluetoothRead(void)
{
char text[96];
(void) mcde_sys_bluetooth(&bt);
if (!bt.present) {
SetText(btState, "This computer has no Bluetooth controller.");
SetText(btButton, "Turn Bluetooth On");
XtSetSensitive(btButton, False);
return;
}
snprintf(text, sizeof text, "Bluetooth is %s (%s).", bt.on ? "on" :
"off", bt.name);
SetText(btState, text);
SetText(btButton, bt.on ? "Turn Bluetooth Off" : "Turn Bluetooth On");
XtSetSensitive(btButton, True);
}
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)
SetText(btState, mcde_sys_error());
else
BluetoothRead();
}
static Widget
MakeBluetooth(Widget parent)
{
Widget box = XtVaCreateWidget("bluetooth", xmRowColumnWidgetClass,
parent, XmNspacing, 10, NULL);
btState = Text(box, "state", "");
btButton = PaneButton(box, "Turn Bluetooth Off", BluetoothPower, NULL);
BluetoothRead();
return box;
}
/* ---- the date and time pane --------------------------------------------- */
static Widget dtTime, dtDate, dtZone;
static void
ClockTick(XtPointer data, XtIntervalId *id)
{
char buf[96];
time_t now = time(NULL);
struct tm tm;
(void) data;
(void) id;
clockTimer = 0;
if (dtTime == NULL)
return;
localtime_r(&now, &tm);
strftime(buf, sizeof buf, "%H:%M:%S", &tm);
SetText(dtTime, buf);
strftime(buf, sizeof buf, "%A, %e %B %Y", &tm);
SetText(dtDate, buf);
strftime(buf, sizeof buf, "Time zone: %Z (UTC%z)", &tm);
SetText(dtZone, buf);
clockTimer = XtAppAddTimeOut(app, 1000, ClockTick, NULL);
}
static void
ClockGone(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
dtTime = dtDate = dtZone = NULL;
if (clockTimer != 0)
XtRemoveTimeOut(clockTimer);
clockTimer = 0;
}
static Widget
MakeDateTime(Widget parent)
{
Widget box = XtVaCreateWidget("datetime", xmRowColumnWidgetClass,
parent, XmNspacing, 8, NULL);
dtTime = Text(box, "time", "");
XtVaSetValues(dtTime, XmNrenderTable, PaneFont(box, 28, True), NULL);
dtDate = Text(box, "date", "");
dtZone = Text(box, "zone", "");
XtAddCallback(box, XmNdestroyCallback, ClockGone, NULL);
ClockTick(NULL, NULL);
return box;
}
/* ---- the about pane ----------------------------------------------------- */
static Widget
MakeAbout(Widget parent)
{
Widget box = XtVaCreateWidget("about", xmRowColumnWidgetClass, parent,
XmNspacing, 8, NULL);
McdeSysInfo i;
char version[128], cpu[200], mem[32], disk[200];
const char *rows[18];
(void) mcde_sys_info(&i);
snprintf(version, sizeof version, "%s %s", i.system, i.version);
snprintf(cpu, sizeof cpu, "%s (%d)", i.cpu, i.cpus);
snprintf(mem, sizeof mem, "%llu MB", i.memory >> 20);
snprintf(disk, sizeof disk, "%s, %llu of %llu MB free", i.disk,
i.diskFree >> 20, i.diskSize >> 20);
rows[0] = "System"; rows[1] = version;
rows[2] = "Kernel"; rows[3] = i.kernel;
rows[4] = "Computer"; rows[5] = i.model;
rows[6] = "Processor"; rows[7] = cpu;
rows[8] = "Memory"; rows[9] = mem;
rows[10] = "Startup Disk"; rows[11] = disk;
rows[12] = "Host Name"; rows[13] = i.hostname;
rows[14] = "Desktop"; rows[15] = DtVERSION_STRING;
rows[16] = "System Layer"; rows[17] = mcde_sys_backend();
Table(box, rows, 9, NULL);
return box;
}
/* ---- the window --------------------------------------------------------- */
static void
ShowOverview(void)
{
if (current != NULL) {
XtDestroyWidget(current);
current = NULL;
}
shown = NULL;
XtUnmanageChild(paneArea);
XtManageChild(overview);
SetText(title, "System Settings");
XtVaSetValues(shell, XmNtitle, "System Settings", NULL);
XtSetSensitive(showAll, False);
}
static void
ShowPane(const Pane *p)
{
Widget w = NULL;
if (p == NULL || p->kind == PANE_LATER)
return;
if (p->kind == PANE_COMMAND) {
Spawn(p->command);
return;
}
if (current != NULL)
XtDestroyWidget(current);
if (strcmp(p->id, "sound") == 0)
w = MakeSound(paneArea);
else if (strcmp(p->id, "network") == 0)
w = MakeNetwork(paneArea);
else if (strcmp(p->id, "bluetooth") == 0)
w = MakeBluetooth(paneArea);
else if (strcmp(p->id, "datetime") == 0)
w = MakeDateTime(paneArea);
else if (strcmp(p->id, "about") == 0)
w = MakeAbout(paneArea);
else if (strcmp(p->id, "accounts") == 0)
w = MakeAccounts(paneArea);
else if (strcmp(p->id, "update") == 0)
w = MakeUpdate(paneArea);
if (w == NULL)
return;
current = w;
shown = p;
XtManageChild(w);
XtUnmanageChild(overview);
XtManageChild(paneArea);
SetText(title, p->label);
XtVaSetValues(shell, XmNtitle, p->label, NULL);
XtSetSensitive(showAll, True);
}
static void
PaneClicked(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
ShowPane((const Pane *) data);
}
static void
ShowAllClicked(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
ShowOverview();
}
/* the overview: one band a category, its panes as icons with their names */
static Widget
MakeOverview(Widget parent)
{
Widget box = XtVaCreateWidget("overview", xmRowColumnWidgetClass, parent,
XmNspacing, 4, NULL);
XmRenderTable bold = PaneFont(box, 11, True);
int c, i;
for (c = 0; c < CAT_COUNT; c++) {
Widget band, inner, row, l;
band = XtVaCreateManagedWidget("category", xmFrameWidgetClass,
box, XmNshadowType, XmSHADOW_ETCHED_IN, NULL);
inner = XtVaCreateManagedWidget("inner", xmRowColumnWidgetClass,
band, XmNspacing, 2, NULL);
l = Text(inner, "name", categoryNames[c]);
XtVaSetValues(l, XmNrenderTable, bold, NULL);
row = XtVaCreateManagedWidget("panes", xmRowColumnWidgetClass,
inner, XmNorientation, XmHORIZONTAL, XmNspacing, 8, NULL);
for (i = 0; i < paneCount; i++) {
const Pane *p = &panes[i];
Widget item, b;
if (p->category != c)
continue;
/* the icon in a field of one size, the name under it:
* the CDE icons are 16 to 37 pixels high, the names
* stand on one line all the same */
item = XtVaCreateManagedWidget("item",
xmRowColumnWidgetClass, row, XmNspacing, 0,
XmNmarginWidth, 0, XmNmarginHeight, 0,
XmNentryAlignment, XmALIGNMENT_CENTER, NULL);
b = XtVaCreateManagedWidget(p->id, xmPushButtonWidgetClass,
item, XmNlabelType, XmPIXMAP,
XmNlabelPixmap, Icon(row, p->icon),
XmNshadowThickness, 0, XmNrecomputeSize, False,
XmNwidth, 112, XmNheight, 44, NULL);
l = Text(item, "label", p->label);
XtVaSetValues(l, XmNalignment, XmALIGNMENT_CENTER, NULL);
if (p->kind == PANE_LATER) {
XtSetSensitive(b, False);
XtSetSensitive(l, False);
} else
XtAddCallback(b, XmNactivateCallback, PaneClicked,
(XtPointer) p);
}
}
return box;
}
int
main(int argc, char **argv)
{
Widget form, bar, sep;
const char *start = NULL;
int i;
XtSetLanguageProc(NULL, NULL, NULL);
shell = XtVaAppInitialize(&app, "Dtsystemsettings", NULL, 0, &argc,
argv, (String *) fallback, XmNtitle, "System Settings", NULL);
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-pane") == 0 && i + 1 < argc)
start = argv[++i];
else if (strcmp(argv[i], "-apply-update") == 0)
return ApplyUpdate();
else {
fprintf(stderr, "usage: dtsystemsettings [-pane ID | "
"-apply-update]\n");
return 1;
}
}
/* a CDE program: its menus would go to the top bar, and it is one */
(void) DtInitialize(XtDisplay(shell), shell, argv[0], "Dtsystemsettings");
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
XmNmarginWidth, 8, XmNmarginHeight, 8, NULL);
bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form,
XmNorientation, XmHORIZONTAL, XmNspacing, 12,
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
showAll = Button(bar, "Show All", ShowAllClicked, NULL);
title = Text(bar, "title", "System Settings");
XtVaSetValues(title, XmNrenderTable, PaneFont(bar, 12, True), NULL);
sep = XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, form,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
NULL);
overview = MakeOverview(form);
XtVaSetValues(overview, XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, sep, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNbottomAttachment,
XmATTACH_FORM, NULL);
paneArea = XtVaCreateWidget("paneArea", xmRowColumnWidgetClass, form,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, sep,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
XtManageChild(overview);
XtSetSensitive(showAll, False);
XtRealizeWidget(shell);
if (start != NULL) {
const Pane *p = PaneFind(start);
if (p == NULL)
fprintf(stderr, "dtsystemsettings: no pane \"%s\"\n", start);
else
ShowPane(p);
}
XtAppMainLoop(app);
return 0;
}