Files
mcde/programs/dtsystemsettings/main.c
T
lotherkandClaude Opus 5.5 ab79f53571 mcde: System Settings > Displays -- arrange, resolution, rotation, main display, mirror
The monitors as rectangles to drag, docked to each other; per monitor on
or off, resolution, rotation, main display (top bar and front panel);
mirroring.  RandR 1.3, set as xrandr(1) does.  Apply asks to keep the
settings and goes back by itself after 15 seconds.  MCDE remembers the
monitor (EDID id), not the socket, also while it is unplugged:
~/.dt/displays, applied at login by Xsession.d/0071.displays.

Tested: make check (test_displays, 31 cases); Xorg with the dummy
driver: -apply-displays, an unknown mode and all-off refused; in the
pane 1024x768 applied and back after 16 s, kept with Keep and written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:36:27 +00:00

699 lines
19 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();
if (MakeWifi(box) != NULL)
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box,
XmNpositionIndex, 3, NULL);
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", "");
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL);
(void) MakeZoneChooser(box);
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);
else if (strcmp(p->id, "appearance") == 0)
w = MakeAppearance(paneArea);
else if (strcmp(p->id, "backup") == 0)
w = MakeBackup(paneArea);
else if (strcmp(p->id, "sharing") == 0)
w = MakeSharing(paneArea);
else if (strcmp(p->id, "startup") == 0)
w = MakeStartup(paneArea);
else if (strcmp(p->id, "desktop") == 0)
w = MakeDesktop(paneArea);
else if (strcmp(p->id, "screensaver") == 0)
w = MakeScreenSaver(paneArea);
else if (strcmp(p->id, "fonts") == 0)
w = MakeFonts(paneArea);
else if (strcmp(p->id, "keyboard") == 0)
w = MakeKeyboard(paneArea);
else if (strcmp(p->id, "mouse") == 0)
w = MakeMouse(paneArea);
else if (strcmp(p->id, "displays") == 0)
w = MakeDisplays(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 if (strcmp(argv[i], "-apply-displays") == 0)
return ApplyDisplaysCommand(XtDisplay(shell));
else if (strcmp(argv[i], "-set-timezone") == 0 && i + 1 < argc)
return SetTimezoneCommand(argv[i + 1]);
else if (strcmp(argv[i], "-join-wifi") == 0 && i + 1 < argc)
return JoinWifiCommand(argv[i + 1],
i + 2 < argc ? argv[i + 2] : NULL);
else {
fprintf(stderr, "usage: dtsystemsettings [-pane ID | "
"-apply-update | -apply-displays | -set-timezone ZONE | -join-wifi IF "
"[SSID-HEX]]\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;
}