Files

210 lines
5.6 KiB
C

/*
* sysinfo.c -- dtsystemsettings: what this computer is (t0188)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The reference's window with a tree on the left and the details on the
* right: Hardware, Network and Software, each filled from what the system
* tells through McdeSys (mcde_sys_info, mcde_sys_interfaces, the disks) --
* no probing of the firmware, no program run for each value. It is the
* content of "About this computer" with room for the parts that do not fit
* in the box.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/Text.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
static Widget sectionList, detailText, statusLine;
static void
Size(unsigned long long b, char *buf, size_t len)
{
if (b >= 1000000000000ULL)
(void)snprintf(buf, len, "%.1f TB", b / 1e12);
else if (b >= 1000000000ULL)
(void)snprintf(buf, len, "%.1f GB", b / 1e9);
else if (b >= 1000000ULL)
(void)snprintf(buf, len, "%.0f MB", b / 1e6);
else
(void)snprintf(buf, len, "%llu bytes", b);
}
static void
Append(char *buf, size_t len, const char *fmt, ...)
{
va_list ap;
size_t at = strlen(buf);
if (at >= len - 1)
return;
va_start(ap, fmt);
(void)vsnprintf(buf + at, len - at, fmt, ap);
va_end(ap);
}
static void
Hardware(char *buf, size_t len)
{
McdeSysInfo info;
char size[32], freeb[32];
(void)snprintf(buf, len, "");
if (mcde_sys_info(&info) != MCDE_SYS_OK) {
Append(buf, len, "%s\n", mcde_sys_error());
return;
}
Append(buf, len, "Model:\t%s\n", info.model[0] != '\0' ? info.model :
"(the firmware does not say)");
Append(buf, len, "Processor:\t%s (%d)\n", info.cpu[0] != '\0' ?
info.cpu : "(unknown)", info.cpus);
Size(info.memory, size, sizeof size);
Append(buf, len, "Memory:\t%s\n", size);
if (info.disk[0] != '\0') {
Size(info.diskSize, size, sizeof size);
Size(info.diskFree, freeb, sizeof freeb);
Append(buf, len, "Startup disk:\t%s, %s (%s free)\n",
info.disk, size, freeb);
}
}
static void
Network(char *buf, size_t len)
{
McdeSysInterface ifs[16];
char host[256];
int n, i;
(void)snprintf(buf, len, "");
if (mcde_sys_hostname_get(host, sizeof host) == MCDE_SYS_OK)
Append(buf, len, "Name:\t%s\n", host);
n = mcde_sys_interfaces(ifs, 16);
if (n <= 0) {
Append(buf, len, "No network interface answered.\n");
return;
}
Append(buf, len, "\n");
for (i = 0; i < n; i++)
Append(buf, len, "%s\t%s%s\n", ifs[i].name,
ifs[i].address[0] != '\0' ? ifs[i].address : "no address",
ifs[i].ssid[0] != '\0' ? ifs[i].ssid : "");
}
static void
Software(char *buf, size_t len)
{
McdeSysInfo info;
(void)snprintf(buf, len, "");
if (mcde_sys_info(&info) != MCDE_SYS_OK) {
Append(buf, len, "%s\n", mcde_sys_error());
return;
}
Append(buf, len, "System:\t%s %s\n", info.system, info.version);
Append(buf, len, "Kernel:\t%s\n", info.kernel);
Append(buf, len, "Desktop:\tMCDE (Motif)\n");
}
static void
Show(int which)
{
char text[4096];
if (which == 1)
Network(text, sizeof text);
else if (which == 2)
Software(text, sizeof text);
else
Hardware(text, sizeof text);
SetText(detailText, text);
XtVaSetValues(detailText, XmNvalue, text, NULL);
SetText(statusLine, which == 1 ? "The interfaces the system "
"reports." : which == 2 ? "What the system calls itself." :
"What the firmware and the kernel say.");
}
static void
Chosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
if (((XmListCallbackStruct *) call)->item_position > 0)
Show(((XmListCallbackStruct *) call)->item_position - 1);
}
static void
RefreshCB(Widget w, XtPointer data, XtPointer call)
{
int *pos = NULL, n = 0, at = 0;
(void) w;
(void) data;
(void) call;
if (XmListGetSelectedPos(sectionList, &pos, &n) && n > 0) {
at = pos[0] - 1;
XtFree((char *) pos);
}
Show(at);
}
Widget
MakeSysinfo(Widget parent)
{
Widget box = XtVaCreateWidget("sysinfo", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget body, sw, buttons;
static const char *const sections[3] = { "Hardware", "Network",
"Software" };
int i;
body = XtVaCreateManagedWidget("sysinfoBody", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 16,
XmNmarginWidth, 0, NULL);
sectionList = XmCreateScrolledList(body, "sections", NULL, 0);
XtVaSetValues(sectionList, XmNvisibleItemCount, 6, NULL);
XtVaSetValues(XtParent(sectionList), XmNwidth, 200, XmNheight, 220,
NULL);
for (i = 0; i < 3; i++) {
XmString s = XmStringCreateLocalized((char *) sections[i]);
XmListAddItemUnselected(sectionList, s, 0);
XmStringFree(s);
}
XtManageChild(sectionList);
XtAddCallback(sectionList, XmNbrowseSelectionCallback, Chosen, NULL);
sw = XtVaCreateManagedWidget("detailBox", xmScrolledWindowWidgetClass,
body, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 380,
XmNheight, 260, NULL);
detailText = XtVaCreateManagedWidget("detailText", xmTextWidgetClass,
sw, XmNeditable, False, XmNcursorPositionVisible, False,
XmNwordWrap, False, NULL);
buttons = XtVaCreateManagedWidget("sysinfoButtons",
xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL,
XmNspacing, 8, XmNmarginWidth, 0, NULL);
(void) Button(buttons, "Refresh", RefreshCB, NULL);
statusLine = Text(box, "sysinfoStatus", "");
XmListSelectPos(sectionList, 1, False);
Show(0);
return box;
}