Files
mcde/programs/dtsystemsettings/main.c
T
lotherk cbf1edf406 MCDE dtsystemsettings: Sharing, name at the top and services beside
their details (t0158)

The pane follows the reference's shape: "Computer Name:" with the field and
Edit..., the line "Computers on your local network can access this computer
at NAME.local", then the services at the left and the chosen one at the
right -- its switch, its state and the address others use (ssh,
smb://ADDR/, ADDR:/folder, afp://ADDR/ from mcde_sys_interfaces).  The
folders shared by SMB, NFS and AFP appear under the file sharing services
(Add.../Remove), and Unlock... is unchanged.

mcde_sys_hostname_set was declared and never written; it is now: the name
must be letters, digits, '.' and '-' (checked in common.c), NetBSD writes
/etc/myname and runs hostname(1), Linux /etc/hostname and hostnamectl (else
hostname(1)).  "Edit..." runs it as root (dtsystemsettings -set-hostname
NAME in the su terminal, like the time zone and the clock).

Measured under Xvfb: the pane shows the name (kitt), the .local line, the
five services, and for "File Sharing by SMB" its state "On -- others reach
it at smb://192.168.1.116/" and the Shared Folders list with Add.../Remove
(docs/screenshots/leopard/System-Preferences-Sharing-mcde.png); selecting
another service switches the page and hides the folders (Remote Login).

Open: the reference's On column as check marks (the switch is on the detail
page), per-user rights for a folder (Samba valid users/read list/write list
and the netatalk counterpart -- McdeSys would need a struct instead of the
path list), Printer, Screen and Internet Sharing entries, AppleTalk options
under "Options...", and the change of the host name was not run as root
here (no root; it writes /etc/myname and the running name).
2026-11-14 08:37:26 +00:00

1332 lines
38 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/DrawingA.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 <Xm/TextF.h>
#include <math.h>
#include <stdlib.h>
#include <Dt/Dt.h>
#include <Mcde/McdeSys.h>
#include <dirent.h>
#include <sys/stat.h>
#include <Mcde/Sound.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);
}
/* a path that may stand in single quotes on a command line */
static int
PlainPath(const char *p)
{
return p != NULL && p[0] == '/' &&
strspn(p, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789/._-") == strlen(p);
}
/*
* A terminal in which su(1) asks for root's password and runs COMMAND as root.
* Not "su -m": that keeps the user's whole environment (su(1): "Leave the
* environment unmodified"), so root looked COMMAND up in the user's PATH --
* a ~/bin/dtsystemsettings would run as root -- and wrote ~/.dt files into the
* user's home (review 2026-11-13, tasksd t0079). Plain su sets HOME and
* SHELL to root's and keeps the rest; the command sets PATH itself, and
* XAUTHORITY is named so root still reaches this display with the user's
* cookie (DISPLAY is kept by su). On NetBSD su's -c is a login class; the
* words after the login go to the shell, so "su root -c CMD" runs CMD there
* as on Linux. A command that does not fit is not run at all.
*/
void
AsRoot(const char *title, const char *command, char *buf, size_t len)
{
char xauth[300] = "";
const char *x = getenv("XAUTHORITY"), *home = getenv("HOME");
int n;
if (PlainPath(x))
snprintf(xauth, sizeof xauth, "XAUTHORITY=%s; export XAUTHORITY; ",
x);
else if (PlainPath(home) && strlen(home) < 200)
snprintf(xauth, sizeof xauth, "XAUTHORITY=%s/.Xauthority; "
"export XAUTHORITY; ", home);
n = snprintf(buf, len, "dtterm -title '%s' -geometry 70x12 -e su root "
"-c 'PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin:"
CDE_INSTALLATION_TOP "/bin; export PATH; %s%s'", title, xauth,
command);
if (n < 0 || (size_t) n >= len)
buf[0] = '\0';
}
/* ---- 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 void
SoundPreview(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
(void) McdeSoundEvent(XtDisplay(w), (const char *) data);
}
static void
SoundTheme(Widget w, XtPointer data, XtPointer call)
{
(void) call;
(void) McdeSoundSetTheme(XtDisplay(w), (const char *) data);
}
/* the themes are directories next to the current one */
static void
MakeThemes(Widget box)
{
char root[512], path[768], *slash;
DIR *d;
struct dirent *e;
Widget row;
(void) McdeSoundInit();
snprintf(root, sizeof root, "%s", McdeSoundThemeDir());
if ((slash = strrchr(root, '/')) == NULL)
return;
*slash = '\0';
if ((d = opendir(root)) == NULL)
return;
XtVaCreateManagedWidget("themeLabel", xmLabelWidgetClass, box,
XmNlabelString, XmStringCreateLocalized("Sound theme:"), NULL);
row = XtVaCreateManagedWidget("themes", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNspacing, 4, NULL);
while ((e = readdir(d)) != NULL) {
struct stat st;
Widget b;
char *dir;
if (e->d_name[0] == '.')
continue;
snprintf(path, sizeof path, "%s/%s", root, e->d_name);
if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
continue;
b = XtVaCreateManagedWidget(e->d_name, xmPushButtonWidgetClass,
row, NULL);
dir = strdup(path);
XtAddCallback(b, XmNactivateCallback, SoundTheme,
(XtPointer) dir);
}
closedir(d);
}
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);
XtVaCreateManagedWidget("eventsLabel", xmLabelWidgetClass, box,
XmNlabelString,
XmStringCreateLocalized("Desktop events (click to hear):"), NULL);
{
Widget row;
int i, n;
(void) McdeSoundInit();
n = McdeSoundCount();
row = XtVaCreateManagedWidget("events", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNpacking,
XmPACK_COLUMN, XmNnumColumns, 4, XmNspacing, 4, NULL);
for (i = 0; i < n; i++) {
const char *name = McdeSoundEventName(i);
Widget b;
if (name == NULL)
continue;
b = XtVaCreateManagedWidget(name,
xmPushButtonWidgetClass, row, NULL);
XtAddCallback(b, XmNactivateCallback, SoundPreview,
(XtPointer) name);
}
}
MakeThemes(box);
SoundRead();
return box;
}
/* ---- the network pane: network.c ---------------------------------------- */
/* ---- 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 Widget dtClockArea;
static int dtClockMinute = -1;
static XtIntervalId clockTimer;
static void DtDrawClock(Widget w);
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);
if (dtClockArea != NULL && tm.tm_min != dtClockMinute)
DtDrawClock(dtClockArea);
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 dtTabRow, dtTabPages, dtTabPage[3];
static int dtTabCount;
static Widget dtCalLabel, dtCalGrid[42], dtTimeField;
static Widget dtNtpToggle, dtNtpServer;
static Widget dtClockShow, dtClock24, dtClockSecs, dtClockWeekday, dtClockDate;
static int dtMonth, dtYear;
static time_t dtDay; /* the day chosen, at 00:00 local */
static Boolean dtNtpOn;
static void DtOpenLanguage(Widget w, XtPointer data, XtPointer call);
static int
LeapYear(int y)
{
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
static int
DaysInMonth(int y, int m)
{
static const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
return days[m] + (m == 1 && LeapYear(y) ? 1 : 0);
}
/* the month of dtDay at the top of the calendar, the chosen day marked */
static void
DtDrawCalendar(void)
{
struct tm tm;
char buf[96];
int i, first, days;
memset(&tm, 0, sizeof tm);
tm.tm_year = dtYear - 1900;
tm.tm_mon = dtMonth;
tm.tm_mday = 1;
tm.tm_isdst = -1;
(void) mktime(&tm); /* normalizes, gives the weekday */
first = tm.tm_wday;
strftime(buf, sizeof buf, "%B %Y", &tm);
SetText(dtCalLabel, buf);
days = DaysInMonth(dtYear, dtMonth);
{
struct tm chosen;
localtime_r(&dtDay, &chosen);
for (i = 0; i < 42; i++) {
int d = i - first + 1;
XmString s;
s = XmStringCreateLocalized(d >= 1 && d <= days ?
(snprintf(buf, sizeof buf, "%d", d), buf) : "");
XtVaSetValues(dtCalGrid[i], XmNlabelString, s,
XmNset, d == chosen.tm_mday &&
dtMonth == chosen.tm_mon &&
dtYear == chosen.tm_year ? XmSET : XmUNSET, NULL);
XmStringFree(s);
XtSetSensitive(dtCalGrid[i], d >= 1 && d <= days);
}
}
}
static void
DtDayChosen(Widget w, XtPointer data, XtPointer call)
{
struct tm tm;
int day = (int) (long) data;
char buf[32];
if (!XmToggleButtonGetState(w))
return; /* only the one pressed counts */
localtime_r(&dtDay, &tm);
tm.tm_mday = day;
tm.tm_isdst = -1;
dtDay = mktime(&tm);
(void)snprintf(buf, sizeof buf, "%02d:%02d:%02d", tm.tm_hour,
tm.tm_min, tm.tm_sec);
XmTextFieldSetString(dtTimeField, buf);
DtDrawCalendar();
}
static void
DtMonthStep(Widget w, XtPointer data, XtPointer call)
{
int step = (int) (long) data;
(void) w;
(void) call;
dtMonth += step;
while (dtMonth < 0) {
dtMonth += 12;
dtYear--;
}
while (dtMonth > 11) {
dtMonth -= 12;
dtYear++;
}
DtDrawCalendar();
}
static void
DtNow(Widget w, XtPointer data, XtPointer call)
{
struct tm tm;
char buf[32];
(void) w;
(void) data;
(void) call;
dtDay = time(NULL);
localtime_r(&dtDay, &tm);
dtYear = tm.tm_year + 1900;
dtMonth = tm.tm_mon;
(void)snprintf(buf, sizeof buf, "%02d:%02d:%02d", tm.tm_hour,
tm.tm_min, tm.tm_sec);
XmTextFieldSetString(dtTimeField, buf);
DtDrawCalendar();
}
/* the analog clock: a circle, twelve marks, two hands; drawn on Expose and
* when the minute changes, no timer of its own (the pane's 1 s clock asks) */
static void
DtDrawClock(Widget w)
{
Display *dpy = XtDisplay(w);
Window win = XtWindow(w);
Dimension width = 100, height = 100;
GC gc;
time_t now = time(NULL);
struct tm tm;
int cx, cy, r, i;
if (win == None)
return;
XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL);
cx = (int) width / 2;
cy = (int) height / 2;
r = ((int) width < (int) height ? (int) width : (int) height) / 2 - 4;
gc = XCreateGC(dpy, win, 0, NULL);
XClearWindow(dpy, win);
XDrawArc(dpy, win, gc, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
for (i = 0; i < 12; i++) {
double a = (double) i * 3.14159265358979 / 6.0;
int x1 = cx + (int) ((r - 4) * sin(a));
int y1 = cy - (int) ((r - 4) * cos(a));
int x2 = cx + (int) ((r - 1) * sin(a));
int y2 = cy - (int) ((r - 1) * cos(a));
XDrawLine(dpy, win, gc, x1, y1, x2, y2);
}
localtime_r(&now, &tm);
{
double m = (double) tm.tm_min * 3.14159265358979 / 30.0;
double h = ((double) (tm.tm_hour % 12) + tm.tm_min / 60.0) *
3.14159265358979 / 6.0;
XDrawLine(dpy, win, gc, cx, cy,
cx + (int) ((r - 8) * sin(m)), cy - (int) ((r - 8) * cos(m)));
XDrawLine(dpy, win, gc, cx, cy,
cx + (int) ((r / 2) * sin(h)), cy - (int) ((r / 2) * cos(h)));
}
XFillArc(dpy, win, gc, cx - 2, cy - 2, 5, 5, 0, 360 * 64);
XFreeGC(dpy, gc);
dtClockMinute = tm.tm_min;
}
static void
DtClockExpose(Widget w, XtPointer client, XtPointer call)
{
XmDrawingAreaCallbackStruct *c = (XmDrawingAreaCallbackStruct *) call;
(void) client;
if (c != NULL && c->event != NULL && c->event->type == Expose)
DtDrawClock(w);
}
/* "dtsystemsettings -set-time EPOCH" in the root terminal */
static void
DtSetTime(Widget w, XtPointer data, XtPointer call)
{
char *text = XmTextFieldGetString(dtTimeField);
char cmd[256], line[512];
struct tm tm;
int h = 0, m = 0, s = 0;
time_t when;
(void) w;
(void) data;
(void) call;
if (text == NULL || sscanf(text, "%d:%d:%d", &h, &m, &s) != 3 ||
h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 60) {
SetText(dtZone, "The time has to read HH:MM:SS.");
XtFree(text);
return;
}
XtFree(text);
localtime_r(&dtDay, &tm);
tm.tm_hour = h;
tm.tm_min = m;
tm.tm_sec = s;
tm.tm_isdst = -1;
when = mktime(&tm);
(void)snprintf(cmd, sizeof cmd, "dtsystemsettings -set-time %lld",
(long long) when);
AsRoot("Set Date and Time", cmd, line, sizeof line);
if (line[0] != '\0')
Spawn(line);
SetText(dtZone, "The administrator's password is asked for in the "
"window that opened.");
}
/* a host name that may stand on the command line of su -c */
static int
PlainHost(const char *h)
{
return h != NULL && h[0] != '\0' &&
strspn(h, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789.-") == strlen(h);
}
static void
DtApplyNtp(Widget w, XtPointer data, XtPointer call)
{
char *server;
char cmd[256], line[512];
(void) w;
(void) data;
(void) call;
server = XmTextFieldGetString(dtNtpServer);
if (!PlainHost(server)) {
SetText(dtZone, "That is not a time server name.");
XtFree(server);
return;
}
(void)snprintf(cmd, sizeof cmd, "dtsystemsettings -set-ntp %s %s",
dtNtpOn ? "on" : "off", server);
XtFree(server);
AsRoot("Time Service", cmd, line, sizeof line);
if (line[0] != '\0')
Spawn(line);
SetText(dtZone, "The administrator's password is asked for in the "
"window that opened.");
}
static void
DtNtpToggled(Widget w, XtPointer data, XtPointer call)
{
XmToggleButtonCallbackStruct *c =
(XmToggleButtonCallbackStruct *) call;
(void) w;
(void) data;
dtNtpOn = c->set ? True : False;
XtSetSensitive(dtCalGrid[0], True);
}
/* the bar's clock: ~/.dt/clock ("show=1", "format=...", read by dtbar) */
static void
DtClockApply(Widget w, XtPointer data, XtPointer call)
{
char path[512], format[128];
const char *home = getenv("HOME");
FILE *fp;
(void) w;
(void) data;
(void) call;
if (home == NULL)
return;
/* %H:%M with seconds and weekday/date as asked; the bar prints one
* line */
format[0] = '\0';
if (XmToggleButtonGetState(dtClockWeekday))
strcat(format, "%a ");
if (XmToggleButtonGetState(dtClockDate))
strcat(format, "%e %b ");
strcat(format, XmToggleButtonGetState(dtClock24) ? "%H:%M" : "%I:%M");
if (XmToggleButtonGetState(dtClockSecs))
strcat(format, ":%S");
if (!XmToggleButtonGetState(dtClock24))
strcat(format, " %p");
(void)snprintf(path, sizeof path, "%s/.dt/clock", home);
if ((fp = fopen(path, "w")) != NULL) {
fprintf(fp, "show=%d\nformat=%s\n",
XmToggleButtonGetState(dtClockShow) ? 1 : 0, format);
fclose(fp);
}
}
static void
DtTabChosen(Widget w, XtPointer data, XtPointer call)
{
XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call;
int which = (int) (long) data, i;
(void) w;
if (!c->set)
return;
for (i = 0; i < dtTabCount; i++)
if (i == which)
XtManageChild(dtTabPage[i]);
else
XtUnmanageChild(dtTabPage[i]);
}
static Widget
DtTab(const char *label, int idx)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget t = XtVaCreateManagedWidget("tab", xmToggleButtonWidgetClass,
dtTabRow, XmNlabelString, s, XmNindicatorOn, False,
XmNshadowThickness, 2, XmNfillOnSelect, True, XmNmarginWidth, 10,
XmNset, idx == 0 ? XmSET : XmUNSET, NULL);
Widget page = XtVaCreateWidget("page", xmRowColumnWidgetClass,
dtTabPages, XmNspacing, 6, XmNmarginWidth, 6, XmNmarginHeight, 6,
NULL);
XmStringFree(s);
XtAddCallback(t, XmNvalueChangedCallback, DtTabChosen,
(XtPointer) (long) idx);
dtTabPage[dtTabCount++] = page;
return page;
}
static Widget
MakeDateTimePage(Widget parent)
{
Widget page = DtTab("Date & Time", dtTabCount);
Widget row, calRow, calCol, grid, side;
int i;
int on = 0;
char server[256];
dtNtpToggle = XtVaCreateManagedWidget("setAutomatically",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *)
"Set date & time automatically:"), NULL);
(void) mcde_sys_ntp_get(&on, server, sizeof server);
dtNtpOn = on != 0;
XtVaSetValues(dtNtpToggle, XmNset, dtNtpOn ? XmSET : XmUNSET, NULL);
XtAddCallback(dtNtpToggle, XmNvalueChangedCallback, DtNtpToggled, NULL);
row = XtVaCreateManagedWidget("ntpRow", xmRowColumnWidgetClass, page,
XmNorientation, XmHORIZONTAL, XmNspacing, 6, NULL);
(void) Text(row, "ntpServerLabel", "Server:");
dtNtpServer = XtVaCreateManagedWidget("ntpServer", xmTextFieldWidgetClass,
row, XmNcolumns, 24, NULL);
XmTextFieldSetString(dtNtpServer, server[0] != '\0' ? server :
"pool.ntp.org");
(void) PaneButton(row, "Apply Time Service", DtApplyNtp, NULL);
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, page, NULL);
/* the calendar at the left, the time at the right, as the reference */
row = XtVaCreateManagedWidget("calAndTime", xmRowColumnWidgetClass, page,
XmNorientation, XmHORIZONTAL, XmNspacing, 16, XmNmarginWidth, 0,
XmNmarginHeight, 0, NULL);
calCol = XtVaCreateManagedWidget("calendarCol", xmRowColumnWidgetClass,
row, XmNspacing, 4, XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
calRow = XtVaCreateManagedWidget("calRow", xmRowColumnWidgetClass, calCol,
XmNorientation, XmHORIZONTAL, XmNspacing, 6, NULL);
(void) Button(calRow, "<", DtMonthStep, (XtPointer) -1L);
dtCalLabel = Text(calRow, "month", "");
XtVaSetValues(dtCalLabel, XmNwidth, 150, NULL);
(void) Button(calRow, ">", DtMonthStep, (XtPointer) 1L);
grid = XtVaCreateManagedWidget("calendar", xmRowColumnWidgetClass,
calCol, XmNnumColumns, 7, XmNorientation, XmHORIZONTAL,
XmNpacking, XmPACK_COLUMN, XmNspacing, 2, NULL);
/* the weekday row is part of the same grid, so that the columns line up */
{
static const char *const names[] = { "S", "M", "T", "W", "T",
"F", "S" };
for (i = 0; i < 7; i++) {
Widget d = Text(grid, "weekday", names[i]);
XtVaSetValues(d, XmNwidth, 26, XmNalignment,
XmALIGNMENT_CENTER, NULL);
}
}
for (i = 0; i < 42; i++) {
dtCalGrid[i] = XtVaCreateManagedWidget("day",
xmToggleButtonWidgetClass, grid,
XmNlabelString, XmStringCreateLocalized((char *) ""),
XmNindicatorOn, False, XmNshadowThickness, 2,
XmNmarginWidth, 6, XmNmarginHeight, 2, NULL);
XtAddCallback(dtCalGrid[i], XmNvalueChangedCallback,
DtDayChosen, (XtPointer) (long) (i + 1));
}
side = XtVaCreateManagedWidget("timeCol", xmRowColumnWidgetClass, row,
XmNspacing, 6, XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
(void) Text(side, "timeLabel", "Time:");
dtTimeField = XtVaCreateManagedWidget("timeField",
xmTextFieldWidgetClass, side, XmNcolumns, 10, NULL);
XmTextFieldSetString(dtTimeField, "00:00:00");
(void) Button(side, "Now", DtNow, (XtPointer) NULL);
{
/* a form of its own: a column would size the area away */
Widget cf = XtVaCreateManagedWidget("clockForm",
xmFormWidgetClass, side, XmNwidth, 110, XmNheight, 110,
XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
dtClockArea = XtVaCreateManagedWidget("clock",
xmDrawingAreaWidgetClass, cf,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
}
XtAddCallback(dtClockArea, XmNexposeCallback, DtClockExpose, NULL);
(void) PaneButton(side, "Set Date and Time", DtSetTime, NULL);
(void) Text(side, "formats", "To set date and time formats, use "
"Language & Region.");
(void) PaneButton(side, "Open Language & Region...", DtOpenLanguage,
NULL);
DtNow(NULL, NULL, NULL);
return page;
}
static Widget
MakeClockPage(Widget parent)
{
Widget page = DtTab("Clock", dtTabCount);
char path[512];
const char *home = getenv("HOME");
FILE *fp;
dtClockShow = XtVaCreateManagedWidget("showClock",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *)
"Show the clock in the bar"), XmNset, XmSET, NULL);
dtClock24 = XtVaCreateManagedWidget("clock24",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *) "24-hour time"),
XmNset, XmSET, NULL);
dtClockSecs = XtVaCreateManagedWidget("clockSeconds",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *) "Show seconds"),
NULL);
dtClockWeekday = XtVaCreateManagedWidget("clockWeekday",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *) "Show the "
"weekday"), XmNset, XmSET, NULL);
dtClockDate = XtVaCreateManagedWidget("clockDate",
xmToggleButtonWidgetClass, page,
XmNlabelString, XmStringCreateLocalized((char *) "Show the date"),
XmNset, XmSET, NULL);
(void) PaneButton(page, "Apply Clock Settings", DtClockApply, NULL);
(void) Text(page, "note", "The bar reads ~/.dt/clock at its next minute.");
/* what the file says now */
if (home != NULL) {
(void)snprintf(path, sizeof path, "%s/.dt/clock", home);
if ((fp = fopen(path, "r")) != NULL) {
char line[256];
while (fgets(line, sizeof line, fp) != NULL) {
if (strncmp(line, "show=0", 6) == 0)
XtVaSetValues(dtClockShow, XmNset,
XmUNSET, NULL);
}
fclose(fp);
}
}
return page;
}
static Widget
MakeDateTime(Widget parent)
{
Widget box = XtVaCreateManagedWidget("datetime", xmRowColumnWidgetClass,
parent, XmNspacing, 8, NULL);
Widget tabsRow, noteRow;
struct tm now;
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);
tabsRow = XtVaCreateManagedWidget("dtTabs", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNspacing, 4, NULL);
dtTabRow = tabsRow;
dtTabPages = XtVaCreateManagedWidget("dtPages",
xmRowColumnWidgetClass, box, XmNspacing, 4, NULL);
(void) MakeDateTimePage(dtTabPages);
{
Widget zone = DtTab("Time Zone", dtTabCount);
Widget clock;
(void) MakeZoneChooser(zone);
clock = MakeClockPage(dtTabPages);
(void) clock;
}
XtManageChild(dtTabPage[0]);
dtDay = time(NULL);
localtime_r(&dtDay, &now);
dtYear = now.tm_year + 1900;
dtMonth = now.tm_mon;
DtDrawCalendar();
XtAddCallback(box, XmNdestroyCallback, ClockGone, NULL);
ClockTick(NULL, NULL);
return box;
}
/* the language pane of this program */
static void
DtOpenLanguage(Widget w, XtPointer data, XtPointer call)
{
char cmd[512];
(void) w;
(void) data;
(void) call;
(void)snprintf(cmd, sizeof cmd, "dtsystemsettings -pane language");
Spawn(cmd);
}
/* ---- 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);
/* calendar, clock and the two tabs need more room than the
* overview's height; this pane brings its own */
XtVaSetValues(shell, XmNheight, 660, NULL);
}
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);
else if (strcmp(p->id, "language") == 0)
w = MakeLanguage(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;
}
/* dtsystemsettings -set-time EPOCH, in the terminal of "Set Date and Time"
* (root); the time as a number, no secret and nothing for a shell to read */
static int
SetTimeCommand(const char *when)
{
char *end = NULL;
long long t = strtoll(when, &end, 10);
int r;
if (end == NULL || *end != '\0' || t <= 0)
r = MCDE_SYS_EINVAL;
else
r = mcde_sys_time_set((time_t) t);
if (r == MCDE_SYS_OK)
printf("The clock is set.\n");
else
printf("The clock was not set: %s\n", mcde_sys_error());
printf("\nPress Return to close this window.\n");
fflush(stdout);
(void) getchar();
return r == MCDE_SYS_OK ? 0 : 1;
}
/* dtsystemsettings -set-hostname NAME, in the terminal of "Edit..." (root) */
static int
SetHostnameCommand(const char *name)
{
int r = mcde_sys_hostname_set(name);
if (r == MCDE_SYS_OK)
printf("The computer's name is %s now.\n", name);
else
printf("The name was not changed: %s\n", mcde_sys_error());
printf("\nPress Return to close this window.\n");
fflush(stdout);
(void) getchar();
return r == MCDE_SYS_OK ? 0 : 1;
}
/* dtsystemsettings -set-ntp on|off SERVER, in the terminal of "Apply Time
* Service" (root) */
static int
SetNtpCommand(int on, const char *server)
{
int r = mcde_sys_ntp_set(on, server);
if (r == MCDE_SYS_OK)
printf("The time service is %s now.\n", on ? "on" : "off");
else
printf("The time service was not changed: %s\n",
mcde_sys_error());
printf("\nPress Return to close this window.\n");
fflush(stdout);
(void) getchar();
return r == MCDE_SYS_OK ? 0 : 1;
}
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], "-set-time") == 0 && i + 1 < argc)
return SetTimeCommand(argv[++i]);
else if (strcmp(argv[i], "-set-hostname") == 0 && i + 1 < argc)
return SetHostnameCommand(argv[++i]);
else if (strcmp(argv[i], "-set-ntp") == 0 && i + 1 < argc) {
int on = strcmp(argv[i + 1], "on") == 0;
++i;
return SetNtpCommand(on,
i + 1 < argc ? 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 if (strcmp(argv[i], "-renew-dhcp") == 0 && i + 1 < argc)
return RenewDhcpCommand(argv[i + 1]);
else {
fprintf(stderr, "usage: dtsystemsettings [-pane ID | "
"-apply-update | -apply-displays | -set-timezone ZONE | "
"-set-time EPOCH | -set-ntp on|off [SERVER] | "
"-set-hostname NAME | "
"-join-wifi IF [SSID-HEX] | -renew-dhcp IF]\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;
}