Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

763 lines
22 KiB
C

/*
* network.c -- dtsystemsettings: the Network pane
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* "Show:" chooses Network Status -- every card with a light and a sentence
* of what it is doing, and "Configure..." -- or one card. A card has the
* tabs TCP/IP (Configure IPv4: Using DHCP, Using DHCP with manual address,
* Manually, Off; address, subnet mask, router, DHCP client ID, Renew DHCP
* Lease; Configure IPv6: Automatically, Manually, Off), DNS (servers and
* search domains, one a line), Wi-Fi (wifi.c, a Wi-Fi card only) and
* Ethernet (its ID, media, MTU). The pane of that generation's System
* Preferences is the model (docs/reference/netconfig.txt in the Makronix
* tree). Where the system keeps the settings McdeSys knows
* (mcde_sys_net_config, mcde_sys_net_configure): dhcpcd.conf on NetBSD,
* NetworkManager on Linux.
*
* Changing them is the administrator's: as a user the fields show what is
* set and the lock offers "Unlock...", which opens this pane as root in a
* terminal after su(1) -- as Accounts does. As root "Apply Now" writes and
* applies them at once. While DHCP gives an address the fields show what
* the card has now, greyed.
*
* 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 <unistd.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
#define MAX_IFS 16
static McdeSysInterface ifs[MAX_IFS];
static int nifs;
static Widget showMenu, showButtons[MAX_IFS + 1], holder;
static int shown; /* 0: Network Status, i: ifs[i - 1] */
static int root;
/* the card's page */
static McdeSysNetConfig cfg;
static Widget v4Buttons[4], v6Buttons[3], v4Menu, v6Menu;
static Widget fAddr, fMask, fRouter, fClient, fAddr6, fPrefix6, renew;
static Widget tDns, tSearch, note, tabRow, tabPages[4];
static int v4, v6, ntabs;
static Widget showRow, showSep; /* the head that stays */
static void Show(int which);
/* W below ABOVE; ABOVE NULL: below the head (the top of the form before
* there is one). The pane is one form: a RowColumn taller than the window
* wrapped into a second column, a form inside the form stayed 1x1
* (measured on Xvfb, 2026-11-13) */
static Widget
Stack(Widget w, Widget above)
{
if (above == NULL)
above = showSep;
XtVaSetValues(w, XmNtopAttachment, above != NULL ? XmATTACH_WIDGET :
XmATTACH_FORM, XmNtopWidget, above, XmNtopOffset, above != NULL ?
8 : 0, XmNleftAttachment, XmATTACH_FORM, NULL);
return w;
}
static const char *const v4Names[] = { "Using DHCP",
"Using DHCP with manual address", "Manually", "Off" };
static const McdeSysIpv4 v4Values[] = { MCDE_IPV4_DHCP,
MCDE_IPV4_DHCP_MANUAL, MCDE_IPV4_MANUAL, MCDE_IPV4_OFF };
static const char *const v6Names[] = { "Automatically", "Manually", "Off" };
static const McdeSysIpv6 v6Values[] = { MCDE_IPV6_AUTO, MCDE_IPV6_MANUAL,
MCDE_IPV6_OFF };
static void
Kind(const McdeSysInterface *it, char *buf, size_t len)
{
snprintf(buf, len, "%s (%s)", it->kind == MCDE_NET_WIFI ? "Wi-Fi" :
"Ethernet", it->name);
}
static void
ReadInterfaces(void)
{
McdeSysInterface all[32];
int n = mcde_sys_interfaces(all, 32), i;
nifs = 0;
for (i = 0; i < n && nifs < MAX_IFS; i++)
if (all[i].kind != MCDE_NET_OTHER)
ifs[nifs++] = all[i];
}
/* ---- Network Status ---------------------------------------------------- */
static void
ConfigureClicked(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
Show((int) (long) data);
}
/* a light: green connected, yellow on but not joined, red no address */
static void
Light(Widget parent, const char *colour)
{
Widget l = Text(parent, "light", "\342\227\217"); /* U+25CF */
XtVaSetValues(l, XtVaTypedArg, XmNforeground, XmRString, colour,
strlen(colour) + 1, XmNrenderTable, PaneFont(parent, 14, True),
NULL);
}
static void
MakeStatus(Widget parent)
{
Widget grid;
char kind[64], text[256];
int i;
grid = Stack(Text(parent, "intro", "The network cards of this "
"computer:"), NULL);
/* filled first, managed then: a container managed empty in a form
* stays 1x1 (Motif) */
grid = Stack(XtVaCreateWidget("cards", xmRowColumnWidgetClass,
parent, XmNspacing, 6, XmNmarginWidth, 0, XmNpacking,
XmPACK_COLUMN, XmNnumColumns, 1, NULL), grid);
for (i = 0; i < nifs; i++) {
const McdeSysInterface *it = &ifs[i];
Widget row = XtVaCreateManagedWidget("row",
xmRowColumnWidgetClass, grid, XmNorientation, XmHORIZONTAL,
XmNmarginWidth, 0, XmNspacing, 8,
XmNentryVerticalAlignment, XmALIGNMENT_CENTER, NULL);
Widget words;
Kind(it, kind, sizeof kind);
if (it->up && it->ssid[0] != '\0') {
Light(row, "green3");
snprintf(text, sizeof text, "%s is connected to \"%s\" "
"and has the IP address %s.", kind, it->ssid,
it->address);
} else if (it->up) {
Light(row, "green3");
snprintf(text, sizeof text, "%s is connected and has the "
"IP address %s.", kind, it->address);
} else if (it->kind == MCDE_NET_WIFI) {
Light(row, "gold");
snprintf(text, sizeof text, "%s is on but is not "
"connected to a network.", kind);
} else {
Light(row, "red3");
snprintf(text, sizeof text, "%s has no IP address: is the "
"cable plugged in?", kind);
}
words = Text(row, "sentence", text);
XtVaSetValues(words, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
Button(row, "Configure...", ConfigureClicked,
(XtPointer) (long) (i + 1));
}
if (nifs == 0)
Text(grid, "none", "This computer has no network card.");
XtManageChild(grid);
}
/* ---- one card: the fields --------------------------------------------- */
static Widget
Field(Widget parent, const char *name, int columns)
{
return XtVaCreateManagedWidget(name, xmTextFieldWidgetClass, parent,
XmNcolumns, columns, XmNmarginHeight, 1, XmNshadowThickness, 1,
XmNhighlightThickness, 1, NULL);
}
static void
SetField(Widget w, const char *text)
{
XmTextFieldSetString(w, (char *) text);
}
static void
GetField(Widget w, char *buf, size_t len)
{
char *s = XmTextFieldGetString(w);
char *p = s, *e;
while (*p == ' ' || *p == '\t')
p++;
e = p + strlen(p);
while (e > p && (e[-1] == ' ' || e[-1] == '\t'))
*--e = '\0';
snprintf(buf, len, "%s", p);
XtFree(s);
}
static void
Editable(Widget w, Boolean on)
{
XtSetSensitive(w, root && on);
}
/* what the fields show and allow for the chosen methods */
static void
UpdateFields(void)
{
char b[64];
McdeSysIpv4 m4 = v4Values[v4];
McdeSysIpv6 m6 = v6Values[v6];
if (m4 == MCDE_IPV4_OFF) {
SetField(fAddr, "");
SetField(fMask, "");
SetField(fRouter, "");
} else if (m4 == MCDE_IPV4_DHCP) {
SetField(fAddr, cfg.cur_address);
SetField(fMask, cfg.cur_netmask);
SetField(fRouter, cfg.cur_router);
} else {
GetField(fAddr, b, sizeof b);
if (b[0] == '\0')
SetField(fAddr, cfg.address[0] != '\0' ? cfg.address :
cfg.cur_address);
GetField(fMask, b, sizeof b);
if (b[0] == '\0')
SetField(fMask, cfg.netmask[0] != '\0' ? cfg.netmask :
cfg.cur_netmask[0] != '\0' ? cfg.cur_netmask :
"255.255.255.0");
GetField(fRouter, b, sizeof b);
if (b[0] == '\0' || m4 == MCDE_IPV4_DHCP_MANUAL)
SetField(fRouter, m4 == MCDE_IPV4_MANUAL &&
cfg.router[0] != '\0' ? cfg.router :
cfg.cur_router);
}
Editable(fAddr, m4 == MCDE_IPV4_MANUAL || m4 == MCDE_IPV4_DHCP_MANUAL);
Editable(fMask, m4 == MCDE_IPV4_MANUAL || m4 == MCDE_IPV4_DHCP_MANUAL);
Editable(fRouter, m4 == MCDE_IPV4_MANUAL);
Editable(fClient, m4 == MCDE_IPV4_DHCP || m4 == MCDE_IPV4_DHCP_MANUAL);
XtSetSensitive(renew, m4 == MCDE_IPV4_DHCP ||
m4 == MCDE_IPV4_DHCP_MANUAL);
if (m6 == MCDE_IPV6_OFF) {
SetField(fAddr6, "");
SetField(fPrefix6, "");
} else if (m6 == MCDE_IPV6_AUTO) {
SetField(fAddr6, cfg.cur_address6);
if (cfg.cur_address6[0] != '\0') {
snprintf(b, sizeof b, "%d", cfg.cur_prefix6);
SetField(fPrefix6, b);
} else
SetField(fPrefix6, "");
} else {
GetField(fAddr6, b, sizeof b);
if (b[0] == '\0')
SetField(fAddr6, cfg.address6);
GetField(fPrefix6, b, sizeof b);
if (b[0] == '\0') {
snprintf(b, sizeof b, "%d", cfg.prefix6 > 0 ?
cfg.prefix6 : 64);
SetField(fPrefix6, b);
}
}
Editable(fAddr6, m6 == MCDE_IPV6_MANUAL);
Editable(fPrefix6, m6 == MCDE_IPV6_MANUAL);
}
static void
V4Chosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
v4 = (int) (long) data;
UpdateFields();
}
static void
V6Chosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
v6 = (int) (long) data;
UpdateFields();
}
static Widget
Choice(Widget parent, const char *name, const char *const *labels, int n,
Widget *buttons, XtCallbackProc cb)
{
Widget pull = XmCreatePulldownMenu(parent, (char *) name, NULL, 0);
Widget menu;
Arg a[1];
int i;
for (i = 0; i < n; i++) {
XmString s = XmStringCreateLocalized((char *) labels[i]);
buttons[i] = XtVaCreateManagedWidget("choice",
xmPushButtonWidgetClass, pull, XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(buttons[i], XmNactivateCallback, cb,
(XtPointer) (long) i);
}
XtSetArg(a[0], XmNsubMenuId, pull);
menu = XmCreateOptionMenu(parent, (char *) name, a, 1);
XtUnmanageChild(XmOptionLabelGadget(menu));
XtManageChild(menu);
return menu;
}
/* keys right-aligned in the first column, the fields in the second */
static Widget
Grid(Widget parent, int rows)
{
return XtVaCreateManagedWidget("grid", xmRowColumnWidgetClass, parent,
XmNorientation, XmVERTICAL, XmNpacking, XmPACK_COLUMN,
XmNnumColumns, 2, XmNisAligned, False, XmNspacing, 2,
XmNmarginWidth, 0, XmNentryVerticalAlignment, XmALIGNMENT_CENTER,
XmNuserData, (XtPointer) (long) rows, NULL);
}
static void
Key(Widget grid, const char *text)
{
Widget k = Text(grid, "key", text);
XtVaSetValues(k, XmNalignment, XmALIGNMENT_END, NULL);
}
static void
RenewClicked(Widget w, XtPointer data, XtPointer call)
{
const char *ifname = ifs[shown - 1].name;
char cmd[512], inner[160];
(void) w;
(void) data;
(void) call;
if (mcde_sys_net_renew(ifname) == MCDE_SYS_OK) {
SetText(note, "The lease is renewed.");
return;
}
if (!root) {
/* asking the DHCP server again is the administrator's on
* NetBSD: in a terminal, after su(1) */
snprintf(inner, sizeof inner, "dtsystemsettings -renew-dhcp %s",
ifname);
AsRoot("Renew DHCP Lease", inner, cmd, sizeof cmd);
Spawn(cmd);
SetText(note, "The password is asked for in the window that "
"opened.");
return;
}
SetText(note, mcde_sys_error());
}
static void
MakeTcpIp(Widget parent)
{
Widget grid;
char b[16];
int i;
grid = Grid(parent, 5);
Key(grid, "Configure IPv4:");
Key(grid, "IP Address:");
Key(grid, "Subnet Mask:");
Key(grid, "Router:");
Key(grid, "DHCP Client ID:");
v4Menu = Choice(grid, "configureIPv4", v4Names, 4, v4Buttons,
V4Chosen);
fAddr = Field(grid, "address", 16);
fMask = Field(grid, "netmask", 16);
fRouter = Field(grid, "router", 16);
fClient = Field(grid, "clientid", 16);
renew = PaneButton(parent, "Renew DHCP Lease", RenewClicked, NULL);
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, parent, NULL);
/* IPv6 in one line: the window has the height of the overview */
grid = XtVaCreateManagedWidget("ipv6", xmRowColumnWidgetClass, parent,
XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, XmNspacing, 4,
XmNentryVerticalAlignment, XmALIGNMENT_CENTER, NULL);
Text(grid, "key", "Configure IPv6:");
v6Menu = Choice(grid, "configureIPv6", v6Names, 3, v6Buttons,
V6Chosen);
fAddr6 = Field(grid, "address6", 22);
Text(grid, "slash", "/");
fPrefix6 = Field(grid, "prefix6", 3);
for (v4 = 0, i = 0; i < 4; i++)
if (v4Values[i] == cfg.ipv4)
v4 = i;
for (v6 = 0, i = 0; i < 3; i++)
if (v6Values[i] == cfg.ipv6)
v6 = i;
XtVaSetValues(v4Menu, XmNmenuHistory, v4Buttons[v4], NULL);
XtVaSetValues(v6Menu, XmNmenuHistory, v6Buttons[v6], NULL);
XtSetSensitive(v4Menu, root);
XtSetSensitive(v6Menu, root);
SetField(fClient, cfg.clientid);
if (cfg.ipv4 == MCDE_IPV4_MANUAL || cfg.ipv4 == MCDE_IPV4_DHCP_MANUAL) {
SetField(fAddr, cfg.address);
SetField(fMask, cfg.netmask);
}
if (cfg.ipv4 == MCDE_IPV4_MANUAL)
SetField(fRouter, cfg.router);
if (cfg.ipv6 == MCDE_IPV6_MANUAL) {
SetField(fAddr6, cfg.address6);
snprintf(b, sizeof b, "%d", cfg.prefix6);
SetField(fPrefix6, b);
}
UpdateFields();
}
/* N strings WIDTH bytes apart from ITEMS, one a line, in a text */
static Widget
Lines(Widget parent, const char *name, const char *items, int n,
size_t width)
{
char text[1024];
size_t at = 0;
Widget w;
Arg a[5];
int i;
text[0] = '\0';
for (i = 0; i < n && at < sizeof text; i++)
at += (size_t) snprintf(text + at, sizeof text - at, "%s%s",
i > 0 ? "\n" : "", items + (size_t) i * width);
XtSetArg(a[0], XmNeditMode, XmMULTI_LINE_EDIT);
XtSetArg(a[1], XmNrows, 3);
XtSetArg(a[2], XmNcolumns, 34);
XtSetArg(a[3], XmNeditable, root ? True : False);
XtSetArg(a[4], XmNscrollHorizontal, False);
w = XmCreateScrolledText(parent, (char *) name, a, 5);
XmTextSetString(w, text);
XtManageChild(w);
return w;
}
static void
MakeDns(Widget parent)
{
char now[256];
size_t at = 0;
int i;
Text(parent, "dnsLabel", "DNS Servers (one a line):");
tDns = Lines(parent, "dns", cfg.dns[0], cfg.ndns, sizeof cfg.dns[0]);
Text(parent, "searchLabel", "Search Domains (one a line):");
tSearch = Lines(parent, "search", cfg.search[0], cfg.nsearch,
sizeof cfg.search[0]);
now[0] = '\0';
for (i = 0; i < cfg.ncur_dns && at < sizeof now; i++)
at += (size_t) snprintf(now + at, sizeof now - at, "%s%s",
i > 0 ? ", " : "", cfg.cur_dns[i]);
{
char text[300];
snprintf(text, sizeof text, "In use now: %s", now[0] != '\0' ?
now : "none");
Text(parent, "dnsNow", text);
}
}
static void
MakeEthernet(Widget parent, const McdeSysInterface *it)
{
char mtu[16];
const char *rows[6];
snprintf(mtu, sizeof mtu, "%d", cfg.mtu);
rows[0] = it->kind == MCDE_NET_WIFI ? "Wi-Fi ID:" : "Ethernet ID:";
rows[1] = cfg.mac[0] != '\0' ? cfg.mac : "unknown";
rows[2] = "Media:";
rows[3] = cfg.media[0] != '\0' ? cfg.media : "unknown";
rows[4] = "MTU:";
rows[5] = cfg.mtu > 0 ? mtu : "unknown";
Table(parent, rows, 3, NULL);
}
/* the tabs: toggle buttons in one radio row, the page of the one set */
static void
TabChosen(Widget w, XtPointer data, XtPointer call)
{
XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct *)
call;
int i, which = (int) (long) data;
(void) w;
if (!cbs->set)
return;
for (i = 0; i < ntabs; i++)
if (i == which)
XtManageChild(tabPages[i]);
else
XtUnmanageChild(tabPages[i]);
}
static Widget
Tab(const char *label, Widget pages)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget t = XtVaCreateManagedWidget("tab", xmToggleButtonWidgetClass,
tabRow, XmNlabelString, s, XmNindicatorOn, False,
XmNshadowThickness, 2, XmNfillOnSelect, True, XmNmarginWidth, 10,
XmNset, ntabs == 0 ? XmSET : XmUNSET, NULL);
Widget page = XtVaCreateWidget("page", xmRowColumnWidgetClass, pages,
XmNspacing, 4, XmNmarginWidth, 10, XmNmarginHeight, 6,
XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM,
NULL);
XmStringFree(s);
XtAddCallback(t, XmNvalueChangedCallback, TabChosen,
(XtPointer) (long) ntabs);
tabPages[ntabs++] = page;
return page;
}
/* the lines of a multi-line text into N strings WIDTH bytes apart */
static int
ReadLines(Widget w, char *dst, size_t width, int max)
{
char *s = XmTextGetString(w), *line, *last = NULL;
int n = 0;
for (line = strtok_r(s, "\n", &last); line != NULL && n < max;
line = strtok_r(NULL, "\n", &last)) {
char *p = line + strspn(line, " \t,"), *e;
e = p + strcspn(p, " \t,");
*e = '\0';
if (*p != '\0')
snprintf(dst + (size_t) n++ * width, width, "%s", p);
}
XtFree(s);
return n;
}
static void
ApplyClicked(Widget w, XtPointer data, XtPointer call)
{
McdeSysNetConfig c = cfg;
char b[16];
(void) w;
(void) data;
(void) call;
c.ipv4 = v4Values[v4];
c.ipv6 = v6Values[v6];
GetField(fAddr, c.address, sizeof c.address);
GetField(fMask, c.netmask, sizeof c.netmask);
GetField(fRouter, c.router, sizeof c.router);
GetField(fClient, c.clientid, sizeof c.clientid);
GetField(fAddr6, c.address6, sizeof c.address6);
GetField(fPrefix6, b, sizeof b);
c.prefix6 = atoi(b);
c.ndns = ReadLines(tDns, c.dns[0], sizeof c.dns[0], MCDE_NET_DNS_MAX);
c.nsearch = ReadLines(tSearch, c.search[0], sizeof c.search[0],
MCDE_NET_DNS_MAX);
SetText(note, "Applying ...");
XmUpdateDisplay(note);
if (mcde_sys_net_configure(ifs[shown - 1].name, &c) != MCDE_SYS_OK) {
Message(holder, mcde_sys_error(), 1);
SetText(note, "Not applied.");
return;
}
Show(shown);
SetText(note, "Applied: the card has the new settings, now and at "
"every start.");
}
static void
UnlockClicked(Widget w, XtPointer data, XtPointer call)
{
char cmd[512];
(void) w;
(void) data;
(void) call;
AsRoot("Unlock Network", "dtsystemsettings -pane network", cmd,
sizeof cmd);
Spawn(cmd);
}
static void
MakeCard(Widget parent, const McdeSysInterface *it)
{
Widget frame, pages, bottom, page;
int r = mcde_sys_net_config(it->name, &cfg);
ntabs = 0;
tabRow = Stack(XtVaCreateWidget("tabs", xmRowColumnWidgetClass,
parent, XmNorientation, XmHORIZONTAL, XmNradioBehavior, True,
XmNradioAlwaysOne, True, XmNspacing, 0, XmNmarginWidth, 0,
XmNmarginHeight, 0, NULL), NULL);
/* managed at once: the pane is realized here, so the frame and its
* form follow their children (ShowLater) */
frame = Stack(XtVaCreateManagedWidget("frame", xmFrameWidgetClass,
parent, XmNrightAttachment, XmATTACH_FORM, NULL), tabRow);
XtVaSetValues(frame, XmNtopOffset, 0, NULL);
pages = XtVaCreateManagedWidget("pages", xmFormWidgetClass, frame, NULL);
page = Tab("TCP/IP", pages);
MakeTcpIp(page);
page = Tab("DNS", pages);
MakeDns(page);
if (it->kind == MCDE_NET_WIFI) {
page = Tab("Wi-Fi", pages);
(void) MakeWifi(page, it->name);
}
page = Tab(it->kind == MCDE_NET_WIFI ? "Card" : "Ethernet", pages);
MakeEthernet(page, it);
XtManageChild(tabPages[0]);
XtManageChild(tabRow);
note = Stack(Text(parent, "note", r == MCDE_SYS_OK ? " " :
mcde_sys_error()), frame);
bottom = Stack(XtVaCreateManagedWidget("bottom", xmRowColumnWidgetClass,
parent, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0,
XmNmarginHeight, 0, XmNspacing, 10, NULL), note);
if (!root) {
Text(bottom, "locked", "Click the lock to make changes.");
Button(bottom, "Unlock...", UnlockClicked, NULL);
} else
Text(bottom, "unlocked", "Unlocked: changes take effect at "
"Apply Now.");
XtSetSensitive(Button(bottom, "Apply Now", ApplyClicked, NULL),
root && r == MCDE_SYS_OK);
}
/* ---- the pane ---------------------------------------------------------- */
static void
Show(int which)
{
WidgetList kids;
Cardinal n;
shown = which < 0 || which > nifs ? 0 : which;
XtVaGetValues(holder, XmNchildren, &kids, XmNnumChildren, &n, NULL);
while (n > 0) {
Widget k = kids[--n];
if (k != showRow && k != showSep)
XtDestroyWidget(k);
}
if (showMenu != NULL)
XtVaSetValues(showMenu, XmNmenuHistory, showButtons[shown], NULL);
if (shown == 0) {
ReadInterfaces();
MakeStatus(holder);
} else
MakeCard(holder, &ifs[shown - 1]);
}
/* the first page once the pane is realized: built under an unrealized form
* the containers kept the 16x16 of their empty start (Xt defers
* change_managed until realization; measured with xwininfo on Xvfb) */
static void
ShowLater(XtPointer data, XtIntervalId *id)
{
(void) data;
(void) id;
if (holder != NULL)
Show(0);
}
static void
ShowChosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
Show((int) (long) data);
}
static void
Gone(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
showMenu = NULL;
holder = NULL;
showRow = showSep = NULL;
}
Widget
MakeNetwork(Widget parent)
{
Widget box = XtVaCreateWidget("network", xmFormWidgetClass, parent,
NULL);
Widget row, pull, sep;
Arg a[1];
char label[64];
int i;
root = geteuid() == 0;
showRow = showSep = NULL;
ReadInterfaces();
row = Stack(XtVaCreateManagedWidget("showRow", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL), NULL);
Text(row, "showLabel", "Show:");
pull = XmCreatePulldownMenu(row, "showMenu", NULL, 0);
for (i = 0; i <= nifs; i++) {
XmString s;
if (i == 0)
snprintf(label, sizeof label, "Network Status");
else
Kind(&ifs[i - 1], label, sizeof label);
s = XmStringCreateLocalized(label);
showButtons[i] = XtVaCreateManagedWidget("show",
xmPushButtonWidgetClass, pull, XmNlabelString, s, NULL);
XmStringFree(s);
XtAddCallback(showButtons[i], XmNactivateCallback, ShowChosen,
(XtPointer) (long) i);
}
XtSetArg(a[0], XmNsubMenuId, pull);
showMenu = XmCreateOptionMenu(row, "show", a, 1);
XtUnmanageChild(XmOptionLabelGadget(showMenu));
XtManageChild(showMenu);
sep = Stack(XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box,
NULL), row);
XtVaSetValues(sep, XmNrightAttachment, XmATTACH_FORM, NULL);
showRow = row;
showSep = sep;
holder = box;
XtAddCallback(box, XmNdestroyCallback, Gone, NULL);
XtAppAddTimeOut(app, 0, ShowLater, NULL);
return box;
}
/* dtsystemsettings -renew-dhcp IF: what the terminal of "Renew DHCP Lease"
* runs as root */
int
RenewDhcpCommand(const char *ifname)
{
int r = mcde_sys_net_renew(ifname);
printf("%s\n", r == MCDE_SYS_OK ? "The lease is renewed." :
mcde_sys_error());
printf("\nPress Return to close this window.\n");
fflush(stdout);
(void) getchar();
return r == MCDE_SYS_OK ? 0 : 1;
}