Files
lotherk d72634fd62 MCDE: the web browser as the desktop's default program (t0179)
A web page has one action on the desktop now, McdeWeb
(programs/types/datatypes.dt.src): it runs "dtsystemsettings -open-web
%Arg_1%", which reads the browser from ~/.dt/defaults (web:, chosen in the
Default Programs pane) and starts it with execlp -- no shell, and an argument
with a shell character is refused.  -open-web is handled before Xt is set up,
so an action needs no display.

The .html and .htm criteria of programs/types/firefox.dt map their Open to
McdeWeb: a double click in dtfile now follows the file's choice instead of a
hard-wired Firefox.  McdeWeb takes ARG_TYPE * -- an action that names a data
type the file does not have is refused ("Either action ... does not apply",
measured), and it is only named by the types that may use it.

dtthemesync gained the Firefox part: every profile under ~/.mozilla/firefox
with a prefs.js gets a user.js (downloads into ~/Downloads, reduced motion,
no cosmetic animations, no smooth scrolling, ui.key.accelKey 91 -- Command is
Mod4 in MCDE) and a chrome/userChrome.css written from the palette.

Measured under Xvfb with xdotool and a wrapper that records its arguments:

  - dtsystemsettings -open-web http://example.org with web: true answers
    "Opened in true: http://example.org" and exits 0, without a display;
  - -open-web "http://x;rm -rf /" answers "That is not an address" and exits
    1; -open-web "" answers "No address was given";
  - a double click on page.html in dtfile with web: mcdetest-browser hands
    exactly that path to the wrapper (cat of what it got);
  - the same double click with web: firefox opens a "Mozilla Firefox" window;
  - dtthemesync -v in a test home with a fake profile writes user.js and
    chrome/userChrome.css; user.js carries browser.download.dir
    <home>/Downloads, prefersReducedMotion 1, cosmeticAnimations false,
    smoothScroll false, ui.key.accelKey 91, and the CSS starts with
    --toolbar-bgcolor: #c6b2a8 (the palette Default.dp);
  - the Default Programs pane shows web: firefox and, after the fix, the
    links choice that the file has ("in a tab of the window that is there")
    -- docs/screenshots/leopard/web-browser-default-mcde.png;
  - make programs/dtsystemsettings, programs/dtthemesync, programs/types
    exit=0; check programs/dtsystemsettings: test_panes PASS; check lib/DtSvc
    PASS.

Not measured: Firefox's own Cmd+T/W/L in a running window (the user.js sets
the accelerator; that needs a person at the keyboard), and dtexplorer -- the
Motif browser of the task -- which is its own program and a follow-up.
2026-11-14 15:54:22 +00:00

371 lines
10 KiB
C

/*
* defaults.c -- dtsystemsettings: the default programs (t0180)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The reference's "General" page of the browser, made the desktop's own:
* which program opens a web page, mail, an image, a film, a text file, and
* how a link from another program arrives (a new window or a tab in the
* window that is there). The choices are kept in ~/.dt/defaults, a small
* "key: value" file:
*
* web: firefox
* mail: dtmail
* image: dtphoto
* video: xine
* text: dtpad
* web-open: tab
*
* The actions of programs/types read it (McdeWeb and its neighbours); the
* file is English and stays readable. A program is offered only when it is
* on this computer ($PATH), and the entry says so when none is.
*
* 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 <sys/stat.h>
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ToggleB.h>
#include "ui.h"
typedef struct {
const char *key; /* the line of ~/.dt/defaults */
const char *label;
const char *const candidates[6];
} Kind;
static const Kind kinds[] = {
{ "web", "Web browser:", { "firefox", "chromium", "epiphany",
"dillo", NULL } },
{ "mail", "Mail:", { "dtmail", "thunderbird", "evolution", "mutt",
NULL } },
{ "image", "Images:", { "dtphoto", "eog", "feh", "display", NULL } },
{ "video", "Video:", { "xine", "vlc", "mpv", "totem", NULL } },
{ "text", "Text:", { "dtpad", "gedit", "mousepad", "leafpad",
NULL } },
};
#define NKINDS ((int) (sizeof kinds / sizeof kinds[0]))
static Widget windowToggle, tabToggle, status;
/* ---- ~/.dt/defaults ("key: value") -------------------------------------- */
static void
DefaultsPath(char *buf, size_t len)
{
const char *home = getenv("HOME");
(void)snprintf(buf, len, "%s/.dt/defaults", home != NULL ? home :
".");
}
/* the value of KEY, "" when the file does not name it */
static void
Get(const char *key, char *buf, size_t len)
{
char path[1024], line[256];
FILE *f;
buf[0] = '\0';
DefaultsPath(path, sizeof path);
if ((f = fopen(path, "r")) == NULL)
return;
while (fgets(line, sizeof line, f) != NULL) {
char *c;
line[strcspn(line, "\r\n")] = '\0';
if ((c = strchr(line, ':')) == NULL)
continue;
*c = '\0';
if (strcmp(line, key) != 0)
continue;
c++;
while (*c == ' ' || *c == '\t')
c++;
(void)snprintf(buf, len, "%s", c);
break;
}
fclose(f);
}
static void
Set(const char *key, const char *value)
{
char path[1024], tmp[1100], line[256];
FILE *f;
int done = 0;
DefaultsPath(path, sizeof path);
(void)snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL)
return;
fputs("# the desktop's default programs (System Settings > Default\n"
"# Programs): \"key: value\", one a line, English keys.\n", f);
{
FILE *old = fopen(path, "r");
if (old != NULL) {
while (fgets(line, sizeof line, old) != NULL) {
char copy[256], *c;
(void)snprintf(copy, sizeof copy, "%s", line);
copy[strcspn(copy, "\r\n")] = '\0';
if ((c = strchr(copy, ':')) != NULL) {
*c = '\0';
if (strcmp(copy, key) == 0) {
done = 1;
continue;
}
}
if (strncmp(line, "# the desktop's", 15) == 0)
continue; /* the header anew */
fputs(line, f);
}
fclose(old);
}
}
fprintf(f, "%s: %s\n", key, value);
fclose(f);
(void)rename(tmp, path);
(void)done;
}
/* 1 when NAME is on this computer */
static int
Present(const char *name)
{
char path[1024], *p, *save = NULL;
const char *env = getenv("PATH");
struct stat st;
if (env == NULL)
env = "/usr/bin:/bin";
(void)snprintf(path, sizeof path, "%s", env);
for (p = strtok_r(path, ":", &save); p != NULL;
p = strtok_r(NULL, ":", &save)) {
char file[1400];
(void)snprintf(file, sizeof file, "%s/%s", p, name);
if (stat(file, &st) == 0 && S_ISREG(st.st_mode) &&
access(file, X_OK) == 0)
return 1;
}
return 0;
}
/* ---- the rows ----------------------------------------------------------- */
/* dtsystemsettings -open-web URL|PATH: the browser from ~/.dt/defaults,
* started without a shell (t0179). Returns 0 when the program started and
* printed nothing, 1 with a sentence otherwise. */
int
OpenWebCommand(const char *arg)
{
char path[1024], line[256], prog[128] = "";
FILE *f;
pid_t pid;
const char *home = getenv("HOME");
const char *p;
if (arg == NULL || arg[0] == '\0') {
printf("No address was given.\n");
return 1;
}
for (p = arg; *p != '\0'; p++)
if (*p == '\n' || *p == ';' || *p == '|' || *p == '`' ||
*p == '$' || *p == '\'' || *p == '"') {
printf("That is not an address: %s\n", arg);
return 1;
}
if (home != NULL && (size_t) snprintf(path, sizeof path,
"%s/.dt/defaults", home) < sizeof path &&
(f = fopen(path, "r")) != NULL) {
while (fgets(line, sizeof line, f) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "web:", 4) == 0) {
char *v = line + 4;
while (*v == ' ' || *v == '\t')
v++;
(void)snprintf(prog, sizeof prog, "%s", v);
}
}
fclose(f);
}
if (prog[0] == '\0')
(void)snprintf(prog, sizeof prog, "firefox");
pid = fork();
if (pid == 0) {
(void)setsid();
execlp(prog, prog, arg, (char *) NULL);
_exit(127);
}
if (pid < 0) {
printf("The browser could not be started.\n");
return 1;
}
printf("Opened in %s: %s\n", prog, arg);
return 0;
}
static void
Chosen(Widget w, XtPointer data, XtPointer call)
{
const Kind *k = (const Kind *) data;
const char *value = (const char *) XtName(w);
char line[128];
(void) call;
(void)snprintf(line, sizeof line, "%s", value);
Set(k->key, line);
SetText(status, "Kept in ~/.dt/defaults.");
}
static Widget
Row(Widget parent, const Kind *k)
{
Widget row = XtVaCreateManagedWidget("defaultRow",
xmRowColumnWidgetClass, parent, XmNorientation, XmHORIZONTAL,
XmNspacing, 10, XmNmarginWidth, 0, NULL);
XmString s = XmStringCreateLocalized((char *) k->label);
Widget l = XtVaCreateManagedWidget("defaultLabel",
xmLabelWidgetClass, row, XmNlabelString, s, XmNwidth, 200,
XmNrecomputeSize, False, XmNalignment, XmALIGNMENT_END, NULL);
Widget opt, pd, item[6];
char cur[64];
int i, n = 0, at = 0;
XmStringFree(s);
(void) l;
opt = XmCreateOptionMenu(row, "defaultChoice", NULL, 0);
pd = XmCreatePulldownMenu(opt, "defaultMenu", NULL, 0);
Get(k->key, cur, sizeof cur);
for (i = 0; k->candidates[i] != NULL; i++) {
XmString is;
if (!Present(k->candidates[i]))
continue;
is = XmStringCreateLocalized((char *) k->candidates[i]);
item[n] = XtVaCreateManagedWidget(k->candidates[i],
xmPushButtonGadgetClass, pd, XmNlabelString, is, NULL);
XmStringFree(is);
XtAddCallback(item[n], XmNactivateCallback, Chosen,
(XtPointer) k);
if (cur[0] != '\0' && strcmp(cur, k->candidates[i]) == 0)
at = n;
n++;
}
if (n == 0) {
XmString is = XmStringCreateLocalized((char *) "(none on this "
"computer)");
item[0] = XtVaCreateManagedWidget("none",
xmPushButtonGadgetClass, pd, XmNlabelString, is, NULL);
XmStringFree(is);
XtSetSensitive(item[0], False);
n = 1;
}
XtVaSetValues(opt, XmNsubMenuId, pd, XmNmenuHistory, item[at], NULL);
XtManageChild(opt);
return row;
}
/* the pair once the pane is laid out: a radio state set before its page is
* managed does not paint (measured, as in the Keyboard pane) */
static void
SyncLinks(XtPointer data, XtIntervalId *id)
{
char v[32];
Widget want;
(void) data;
(void) id;
Get("web-open", v, sizeof v);
want = strcmp(v, "window") == 0 ? windowToggle : tabToggle;
if (getenv("MCDE_DEFAULTS_DEBUG") != NULL)
fprintf(stderr, "defaults: web-open='%s' -> %s\n", v,
want == tabToggle ? "tab" : "window");
XmToggleButtonSetState(windowToggle, False, True);
XmToggleButtonSetState(tabToggle, False, True);
XmToggleButtonSetState(want, True, True);
/* the radio's own indicator follows the group only after a redraw */
XmUpdateDisplay(want);
XmUpdateDisplay(windowToggle);
}
static void
LinkChosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
Boolean on = ((XmToggleButtonCallbackStruct *) call)->set;
if (on)
Set("web-open", (const char *) data);
XmToggleButtonSetState(w, on, False);
XmToggleButtonSetState(w == windowToggle ? tabToggle : windowToggle,
False, False);
}
Widget
MakeDefaults(Widget parent)
{
Widget box = XtVaCreateWidget("defaults", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget radio;
char v[32];
int i;
(void) Text(box, "defaultsLabel", "Open with:");
for (i = 0; i < NKINDS; i++)
(void) Row(box, &kinds[i]);
(void) Text(box, "linksLabel", "A link from another program opens:");
/* two toggles kept exclusive by the callbacks: a radio box in a page
* that is laid out late does not paint its state (measured) */
radio = XtVaCreateManagedWidget("linksMode",
xmRowColumnWidgetClass, box, XmNorientation, XmVERTICAL,
XmNspacing, 2, XmNmarginWidth, 0, NULL);
windowToggle = XtVaCreateManagedWidget("in a new window",
xmToggleButtonWidgetClass, radio, NULL);
tabToggle = XtVaCreateManagedWidget("in a tab of the window that is "
"there", xmToggleButtonWidgetClass, radio, NULL);
Get("web-open", v, sizeof v);
XmToggleButtonSetState(strcmp(v, "window") == 0 ? windowToggle :
tabToggle, True, False);
{
/* the pair again once the pane is laid out (a radio state set
* before does not paint, measured) */
Widget want = strcmp(v, "window") == 0 ? windowToggle :
tabToggle;
XmToggleButtonSetState(windowToggle, False, False);
XmToggleButtonSetState(tabToggle, False, False);
XmToggleButtonSetState(want, True, False);
}
XtAppAddTimeOut(app, 0, SyncLinks, NULL);
XtAddCallback(windowToggle, XmNvalueChangedCallback, LinkChosen,
(XtPointer) "window");
XtAddCallback(tabToggle, XmNvalueChangedCallback, LinkChosen,
(XtPointer) "tab");
(void) Text(box, "linksNote", "The web action of the desktop reads it "
"and starts the browser with the window or the tab.");
(void) Text(box, "defaultsNote", "The choices are kept in "
"~/.dt/defaults; the programs of programs/types read the file.");
status = Text(box, "defaultsStatus", "");
return box;
}