Files
mcde/programs/dtsystemsettings/printers.c
T
lotherk f37f703eb2 MCDE dtsystemsettings: the Printers pane, CUPS and mDNS (t0143)
New programs/dtsystemsettings/printers.c replaces the "printers" entry that
only started dtprintinfo:

  * the left list is the queues of CUPS (McdeSys mcde_sys_printers: lpstat
    -p and -d, parsed; the default is marked with a star and named in the
    status line), the right side is what is chosen -- state, location,
    model, whether it is the default and whether it is shared;
  * "Add Printer..." lists the printers mDNS finds (_ipp._tcp through
    avahi-browse -rtp, names unescaped: avahi writes \032 for a space in
    decimal), fills the queue name from the chosen one and shows its URI;
  * "Add", "Remove Printer..." and "Set as Default..." run in the terminal
    of su(1) (dtsystemsettings -add-printer NAME URI | -remove-printer |
    -set-default-printer), the pattern of the other root actions; adding
    uses the driverless model (lpadmin -p NAME -E -v URI -m everywhere);
  * "Print Queue..." opens dtprintinfo -all, CDE's queue view.

McdeSys gained mcde_sys_printers, _printer_add, _printer_remove,
_printer_default and _printers_discovered (lib/McdeSys/printers.c, built for
every backend).  Names and URIs are checked before they reach an argument
list (letters, digits, - and _; ipp://, ipps://, socket://, usb:// without
spaces or shell punctuation); the mDNS output is only parsed.

The task asked for libcups rather than command lines; measured 2026-11-14 on
the build host, cupsGetDests2() returns 0x2 and leaves num_dests untouched --
through dlopen and linked directly -- while _cupsGetDests() finds no queue
and lpstat -p lists the two the desktop has.  The library path is a
follow-up, and AGENTS.md carries the measurement.

Measured under Xvfb with xdotool, against the host's CUPS:

  - the pane lists HP_LaserJet_M110w_CC1016 (idle) and Samsung_CLP-365
    (disabled, its reason shown), the default star on Samsung and "Default
    printer: Samsung_CLP-365" below -- the same two and the same default that
    lpstat -p / lpstat -d print; the detail side shows the chosen queue
    (docs/screenshots/leopard/System-Preferences-Print-and-Fax-mcde.png);
  - "Add Printer..." finds "HP LaserJet M110w (CC1016)" over mDNS, choosing
    it fills the name HP_LaserJet_M110w_CC1016 and the URI
    ipp://192.168.1.147:631/ipp/print
    (docs/screenshots/leopard/System-Preferences-Print-and-Fax-Add-Printer-
    mcde.png);
  - "dtsystemsettings -set-default-printer Samsung_CLP-365" as the user
    answers "The default printer was not changed: The default printer needs
    the administrator." -- the root step runs in the su terminal;
  - test_sysinfo prints both queues and the discovered printer;
  - make lib/McdeSys, programs/dtsystemsettings exit=0; check lib/McdeSys:
    test_sysinfo and test_netmount PASS.

Open (follow-up): printing a test page, the fax/Bluetooth/AppleTalk printers
of the reference, the NetBSD CUPS path, and adding a queue verified in a VM
with an IPP printer.
2026-11-14 13:27:22 +00:00

461 lines
13 KiB
C

/*
* printers.c -- dtsystemsettings: the printers (t0143)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The queues of CUPS on the left (McdeSys mcde_sys_printers), what is
* chosen on the right: its state, where it stands, its model, whether it is
* the default and whether it is shared. Below the list "Add Printer...",
* which lists the printers mDNS finds (mcde_sys_printers_discovered) and
* adds the chosen one with the driverless model (lpadmin -m everywhere,
* through the administrator's terminal), "Remove Printer...",
* "Set as Default..." and the CDE print queue (dtprintinfo).
*
* The reference's fax, Bluetooth and AppleTalk printers are left out (no
* fax on the desktop, no Bluetooth printing, AFP/papd is a follow-up).
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <ctype.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/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Mcde/McdeSys.h>
#include <Dt/McdePrefs.h>
#include "ui.h"
#define MAX_PRINTERS 32
#define MAX_FOUND 16
static Widget list, status, nameVal, stateVal, locVal, modelVal;
static Widget defVal, sharedVal, searchField, addButton, removeButton;
static Widget defButton;
static McdeSysPrinter printers[MAX_PRINTERS];
static int nprinters, chosen = -1;
static char foundName[MAX_FOUND][MCDE_SYS_PRINTER_TEXT];
static char foundUri[MAX_FOUND][MCDE_SYS_PRINTER_URI];
static int nfound;
/* "HP LaserJet" -> "HP_LaserJet" for a queue name */
static void
QueueName(const char *from, char *to, size_t len)
{
size_t i, n = 0;
for (i = 0; from[i] != '\0' && n + 1 < len; i++) {
unsigned char c = (unsigned char) from[i];
to[n++] = (char) (isalnum(c) || c == '-' || c == '_' ? c : '_');
}
while (n > 0 && to[n - 1] == '_')
n--;
to[n] = '\0';
if (n == 0)
(void)snprintf(to, len, "printer");
}
static void
ShowChoice(int at)
{
char line[256];
if (at < 0 || at >= nprinters) {
chosen = -1;
SetText(nameVal, "-");
SetText(stateVal, "-");
SetText(locVal, "-");
SetText(modelVal, "-");
SetText(defVal, "-");
SetText(sharedVal, "-");
XtSetSensitive(removeButton, False);
XtSetSensitive(defButton, False);
return;
}
chosen = at;
SetText(nameVal, printers[at].name);
SetText(stateVal, printers[at].state);
SetText(locVal, printers[at].location[0] != '\0' ?
printers[at].location : "-");
SetText(modelVal, printers[at].info[0] != '\0' ? printers[at].info : "-");
SetText(defVal, printers[at].is_default ? "yes" : "no");
SetText(sharedVal, printers[at].shared ? "yes" : "no");
XtSetSensitive(removeButton, True);
XtSetSensitive(defButton, !printers[at].is_default);
(void)snprintf(line, sizeof line, "Default printer: %s",
"see the list");
(void) line;
}
/* the list, filtered by the search text (a name that contains it) */
/* 1 when HAY contains NEEDLE without case */
static int
Contains(const char *hay, const char *needle)
{
size_t n = strlen(needle), i;
if (n == 0)
return 1;
for (; *hay != '\0'; hay++) {
for (i = 0; i < n && hay[i] != '\0'; i++)
if (tolower((unsigned char) hay[i]) !=
tolower((unsigned char) needle[i]))
break;
if (i == n)
return 1;
}
return 0;
}
static void
FillList(void)
{
char *filter = XmTextFieldGetString(searchField);
int i;
XmListDeleteAllItems(list);
for (i = 0; i < nprinters; i++) {
char row[300];
XmString s;
if (filter != NULL && !Contains(printers[i].name, filter))
continue;
(void)snprintf(row, sizeof row, "%s%s - %s",
printers[i].is_default ? "* " : " ", printers[i].name,
printers[i].state);
s = XmStringCreateLocalized(row);
XmListAddItemUnselected(list, s, 0);
XmStringFree(s);
}
if (filter != NULL)
XtFree(filter);
}
static void
Reload(void)
{
char line[256];
int i, def = -1;
nprinters = mcde_sys_printers(printers, MAX_PRINTERS);
if (nprinters < 0) {
nprinters = 0;
SetText(status, mcde_sys_error());
XtSetSensitive(addButton, True);
FillList();
ShowChoice(-1);
return;
}
for (i = 0; i < nprinters; i++)
if (printers[i].is_default)
def = i;
if (def >= 0)
(void)snprintf(line, sizeof line, "Default printer: %s",
printers[def].name);
else
(void)snprintf(line, sizeof line, "Default printer: none "
"chosen");
SetText(status, line);
FillList();
ShowChoice(nprinters > 0 ? 0 : -1);
if (nprinters > 0)
XmListSelectPos(list, 1, False);
}
static void
ListChosen(Widget w, XtPointer data, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
(void) w;
(void) data;
if (c->item_position > 0)
ShowChoice(c->item_position - 1);
}
static void
SearchChanged(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
FillList();
}
static void
RefreshCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
Reload();
}
/* the root actions: the terminal of su(1) asks, then dtsystemsettings runs
* the command (the pattern of the other panes) */
static void
SetDefaultRun(Widget w, XtPointer data, XtPointer call)
{
char command[700], shell[900];
(void) w;
(void) data;
(void) call;
if (chosen < 0)
return;
(void)snprintf(command, sizeof command,
"dtsystemsettings -set-default-printer %s", printers[chosen].name);
AsRoot("Set Default Printer", command, shell, sizeof shell);
Spawn(shell);
}
static void
RemoveRun(Widget w, XtPointer data, XtPointer call)
{
char command[700], shell[900];
int at = chosen;
(void) w;
(void) data;
(void) call;
if (at < 0)
return;
(void)snprintf(command, sizeof command,
"dtsystemsettings -remove-printer %s", printers[at].name);
AsRoot("Remove Printer", command, shell, sizeof shell);
Spawn(shell);
}
static void
QueueCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
Spawn("dtprintinfo -all");
}
/* ---- the Add Printer window -------------------------------------------- */
static Widget addShell, foundList, addName, addNote;
static void
FoundChosen(Widget w, XtPointer data, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
char name[64];
(void) w;
(void) data;
if (c->item_position < 1 || c->item_position > nfound)
return;
QueueName(foundName[c->item_position - 1], name, sizeof name);
XmTextFieldSetString(addName, name);
SetText(addNote, foundUri[c->item_position - 1]);
}
static void
AddRun(Widget w, XtPointer data, XtPointer call)
{
char *name = XmTextFieldGetString(addName);
char command[900], shell[1000];
(void) w;
(void) data;
(void) call;
if (name == NULL || name[0] == '\0') {
SetText(addNote, "Type a name (or choose a printer above).");
if (name != NULL)
XtFree(name);
return;
}
if (nfound <= 0) {
SetText(addNote, "No printer was found on the network.");
XtFree(name);
return;
}
(void)snprintf(command, sizeof command,
"dtsystemsettings -add-printer %s %s", name, foundUri[0]);
XtFree(name);
AsRoot("Add Printer", command, shell, sizeof shell);
Spawn(shell);
XtPopdown(addShell);
}
static void
AddClose(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
XtPopdown(addShell);
}
static void
AddCB(Widget w, XtPointer data, XtPointer call)
{
(void) data;
(void) call;
if (addShell == NULL) {
Widget box, row, buttons;
XmString s;
int i;
addShell = XtVaCreatePopupShell("addPrinter",
topLevelShellWidgetClass, XtParent(XtParent(w)),
XmNtitle, "Add Printer", NULL);
box = XtVaCreateManagedWidget("addBox",
xmRowColumnWidgetClass, addShell, XmNorientation,
XmVERTICAL, XmNspacing, 8, XmNmarginWidth, 12,
XmNmarginHeight, 12, XmNwidth, 460, NULL);
s = XmStringCreateLocalized((char *) "Printers found on the "
"network:");
XtVaCreateManagedWidget("foundLabel", xmLabelWidgetClass, box,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING,
NULL);
XmStringFree(s);
foundList = XmCreateScrolledList(box, "found", NULL, 0);
XtVaSetValues(foundList, XmNvisibleItemCount, 5, NULL);
XtManageChild(foundList);
XtAddCallback(foundList, XmNbrowseSelectionCallback,
FoundChosen, NULL);
row = XtVaCreateManagedWidget("addNameRow",
xmRowColumnWidgetClass, box, XmNorientation,
XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 0, NULL);
s = XmStringCreateLocalized((char *) "Name:");
XtVaCreateManagedWidget("addNameLabel", xmLabelWidgetClass,
row, XmNlabelString, s, NULL);
XmStringFree(s);
addName = XtVaCreateManagedWidget("addName",
xmTextFieldWidgetClass, row, XmNcolumns, 24,
XmNheight, 28, NULL);
addNote = Text(box, "addNote", "");
buttons = XtVaCreateManagedWidget("addButtons",
xmRowColumnWidgetClass, box, XmNorientation,
XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 0,
XmNentryAlignment, XmALIGNMENT_CENTER, NULL);
(void) Button(buttons, "Add (asks the administrator)", AddRun,
NULL);
(void) Button(buttons, "Cancel", AddClose, NULL);
(void) i;
}
/* the printers in reach, anew each time the window opens */
XmListDeleteAllItems(foundList);
nfound = mcde_sys_printers_discovered(foundName, foundUri, MAX_FOUND);
if (nfound < 0) {
nfound = 0;
SetText(addNote, mcde_sys_error());
} else if (nfound == 0) {
SetText(addNote, "No printer answered (mDNS).");
} else {
for (int i = 0; i < nfound; i++) {
XmString s = XmStringCreateLocalized(foundName[i]);
XmListAddItemUnselected(foundList, s, 0);
XmStringFree(s);
}
SetText(addNote, "Choose one; the address is added with the "
"driverless model.");
XmListSelectPos(foundList, 1, False);
}
XtPopup(addShell, XtGrabNone);
}
/* ---- the pane ----------------------------------------------------------- */
/* a line of the details: the caption and its value */
static Widget
Detail(Widget parent, const char *caption)
{
Widget row = XtVaCreateManagedWidget("detailRow",
xmRowColumnWidgetClass, parent, XmNorientation, XmHORIZONTAL,
XmNpacking, XmPACK_COLUMN, XmNspacing, 8, XmNmarginWidth, 0,
NULL);
XmString s = XmStringCreateLocalized((char *) caption);
Widget v;
XtVaCreateManagedWidget("detailLabel", xmLabelWidgetClass, row,
XmNlabelString, s, XmNwidth, 120, XmNrecomputeSize, False,
XmNalignment, XmALIGNMENT_END, NULL);
XmStringFree(s);
s = XmStringCreateLocalized((char *) "-");
v = XtVaCreateManagedWidget("detailValue", xmLabelWidgetClass, row,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XmStringFree(s);
return v;
}
Widget
MakePrinters(Widget parent)
{
Widget box = XtVaCreateWidget("printers", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget top, body, left, details, buttons;
XmString s;
top = XtVaCreateManagedWidget("printerTop", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 6,
XmNmarginWidth, 0, NULL);
s = XmStringCreateLocalized((char *) "Printers:");
XtVaCreateManagedWidget("printerLabel", xmLabelWidgetClass, top,
XmNlabelString, s, NULL);
XmStringFree(s);
searchField = XtVaCreateManagedWidget("printerSearch",
xmTextFieldWidgetClass, top, XmNcolumns, 22, NULL);
XtAddCallback(searchField, XmNvalueChangedCallback, SearchChanged,
NULL);
body = XtVaCreateManagedWidget("printerBody", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 16,
XmNmarginWidth, 0, NULL);
left = XtVaCreateManagedWidget("printerListBox",
xmRowColumnWidgetClass, body, XmNorientation, XmVERTICAL,
XmNspacing, 2, XmNmarginWidth, 0, NULL);
list = XmCreateScrolledList(left, "printers", NULL, 0);
XtVaSetValues(list, XmNvisibleItemCount, 10, NULL);
XtVaSetValues(XtParent(list), XmNwidth, 280, XmNheight, 300, NULL);
XtManageChild(list);
XtAddCallback(list, XmNbrowseSelectionCallback, ListChosen, NULL);
details = XtVaCreateManagedWidget("printerDetail",
xmRowColumnWidgetClass, body, XmNorientation, XmVERTICAL,
XmNspacing, 4, XmNmarginWidth, 0, NULL);
nameVal = Detail(details, "Name:");
stateVal = Detail(details, "State:");
locVal = Detail(details, "Location:");
modelVal = Detail(details, "Model:");
defVal = Detail(details, "Default printer:");
sharedVal = Detail(details, "Shared:");
buttons = XtVaCreateManagedWidget("printerButtons",
xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL,
XmNspacing, 8, XmNmarginWidth, 0, NULL);
addButton = Button(buttons, "Add Printer...", AddCB, NULL);
removeButton = Button(buttons, "Remove Printer...", RemoveRun, NULL);
defButton = Button(buttons, "Set as Default...", SetDefaultRun, NULL);
(void) Button(buttons, "Print Queue...", QueueCB, NULL);
(void) Button(buttons, "Refresh", RefreshCB, NULL);
status = Text(box, "printerStatus", "");
Reload();
if (nprinters > 0) {
XmListSelectPos(list, 1, False);
ShowChoice(0);
}
return box;
}