/* * appearance.c -- dtsystemsettings: the colours and the fonts of the desktop * * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. * * Appearance: CDE's palettes (*.dp, eight colours each), every one drawn as * a row of its colours. Choosing one does what dtstyle does * (programs/dtstyle/ColorMain.c, the resource "*0*ColorPalette: NAME.dp" * merged with _DtAddToResource(3)); dtsession saves the resources at logout * (dtsession/SmSave.c, OutputResource) and its colour server allocates the * palette at the next login. On a TrueColor screen -- every screen today -- * the colour server cannot change colours that are allocated, so dtstyle * says the same: the palette takes effect at the next session. * * Fonts: the family and size of the render table every MCDE program draws * with (dtsession/sys.resources.src: *mcde.fontName, *mcde.fontSize), merged * the same way; a program started after the change uses it at once. The * families are the ones fontconfig knows, listed through Xft. * * The palettes are found where dtsession's colour server looks * (dtsession/SrvFile_io.c): ~/.dt/palettes, CDE_CONFIGURATION_TOP/palettes, * CDE_INSTALLATION_TOP/palettes; a name found earlier wins. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md); Xft as Motif uses * it. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include
#include "ui.h" /* ---- the resources as they are now -------------------------------------- */ /* VALUE of the resource NAME/CLASS in the server's resources, or DEF */ void ServerResource(Display *dpy, const char *name, const char *class, const char *def, char *buf, size_t len) { char *s = XResourceManagerString(dpy), *type = NULL; XrmDatabase db; XrmValue v; snprintf(buf, len, "%s", def); if (s == NULL || (db = XrmGetStringDatabase(s)) == NULL) return; if (XrmGetResource(db, name, class, &type, &v) && v.addr != NULL) snprintf(buf, len, "%.*s", (int) v.size, (char *) v.addr); XrmDestroyDatabase(db); /* XrmValue counts the NUL; a value may end in blanks */ while (buf[0] != '\0' && (buf[strlen(buf) - 1] == ' ' || buf[strlen(buf) - 1] == '\t')) buf[strlen(buf) - 1] = '\0'; } /* ---- Appearance --------------------------------------------------------- */ #define MAX_PALETTES 128 #define SWATCH 14 typedef struct { char name[64]; char path[512]; } Palette; static Palette pal[MAX_PALETTES]; static int npal; static Widget appNote, appLogout; static int PaletteCompare(const void *a, const void *b) { return strcmp(((const Palette *) a)->name, ((const Palette *) b)->name); } static void ScanPalettes(const char *dir) { DIR *d = opendir(dir); struct dirent *de; int i; if (d == NULL) return; while ((de = readdir(d)) != NULL && npal < MAX_PALETTES) { size_t n = strlen(de->d_name); if (n < 4 || strcmp(de->d_name + n - 3, ".dp") != 0 || n - 3 >= sizeof pal[0].name) continue; for (i = 0; i < npal; i++) if (strncmp(pal[i].name, de->d_name, n - 3) == 0 && pal[i].name[n - 3] == '\0') break; if (i < npal) continue; snprintf(pal[npal].name, sizeof pal[npal].name, "%.*s", (int) (n - 3), de->d_name); snprintf(pal[npal].path, sizeof pal[npal].path, "%s/%s", dir, de->d_name); npal++; } closedir(d); } /* the palette's colours side by side; None when it cannot be read */ static Pixmap Swatches(Widget w, const char *path) { Display *dpy = XtDisplay(w); Screen *scr = XtScreen(w); Colormap cmap = DefaultColormapOfScreen(scr); char line[64]; FILE *fp = fopen(path, "r"); Pixmap pm; GC gc; XColor c; int n = 0; if (fp == NULL) return None; pm = XCreatePixmap(dpy, RootWindowOfScreen(scr), 8 * SWATCH + 1, SWATCH + 1, (unsigned int) DefaultDepthOfScreen(scr)); gc = XCreateGC(dpy, pm, 0, NULL); XSetForeground(dpy, gc, BlackPixelOfScreen(scr)); XFillRectangle(dpy, pm, gc, 0, 0, 8 * SWATCH + 1, SWATCH + 1); while (n < 8 && fgets(line, sizeof line, fp) != NULL) { line[strcspn(line, " \t\r\n")] = '\0'; /* "#RRRRGGGGBBBB" or a colour's name (Black.dp: "Black") */ if (line[0] == '\0' || !XParseColor(dpy, cmap, line, &c) || !XAllocColor(dpy, cmap, &c)) continue; XSetForeground(dpy, gc, c.pixel); XFillRectangle(dpy, pm, gc, n * SWATCH + 1, 1, SWATCH - 1, SWATCH - 1); n++; } fclose(fp); XFreeGC(dpy, gc); if (n == 0) { XFreePixmap(dpy, pm); return None; } return pm; } static void PaletteChosen(Widget w, XtPointer data, XtPointer call) { const Palette *p = (const Palette *) data; char res[160]; if (!((XmToggleButtonCallbackStruct *) call)->set) return; /* dtstyle's line (ColorMain.c), screen 0 as the colour server's */ snprintf(res, sizeof res, "*%d*ColorPalette: %s.dp\n", XScreenNumberOfScreen(XtScreen(w)), p->name); _DtAddToResource(XtDisplay(w), res); snprintf(res, sizeof res, "%s takes effect when you log in again.", p->name); SetText(appNote, res); XtSetSensitive(appLogout, True); } static void LogOutNow(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; Spawn("dtaction ExitSession"); } Widget MakeAppearance(Widget parent) { Display *dpy = XtDisplay(parent); Widget box = XtVaCreateWidget("appearance", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); Widget sw, grid; char dir[512], current[64], name[64], class[64]; const char *home = getenv("HOME"); int i, scr = XScreenNumberOfScreen(XtScreen(parent)); npal = 0; if (home != NULL) { snprintf(dir, sizeof dir, "%s/.dt/palettes", home); ScanPalettes(dir); } ScanPalettes(CDE_CONFIGURATION_TOP "/palettes"); ScanPalettes(CDE_INSTALLATION_TOP "/palettes"); qsort(pal, (size_t) npal, sizeof pal[0], PaletteCompare); /* what the colour server reads: Dtsession..ColorPalette */ snprintf(name, sizeof name, "dtsession.%d.colorPalette", scr); snprintf(class, sizeof class, "Dtsession.%d.ColorPalette", scr); ServerResource(dpy, name, class, "Default.dp", current, sizeof current); if (strlen(current) > 3 && strcmp(current + strlen(current) - 3, ".dp") == 0) current[strlen(current) - 3] = '\0'; (void) Text(box, "label", "The colours of the desktop:"); sw = XtVaCreateManagedWidget("palettes", xmScrolledWindowWidgetClass, box, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 560, XmNheight, 300, NULL); grid = XtVaCreateManagedWidget("grid", xmRowColumnWidgetClass, sw, XmNpacking, XmPACK_COLUMN, XmNnumColumns, (npal + 3) / 4, XmNorientation, XmHORIZONTAL, XmNradioBehavior, True, NULL); for (i = 0; i < npal; i++) { XmString s = XmStringCreateLocalized(pal[i].name); Pixmap pm = Swatches(parent, pal[i].path); Widget t; t = XtVaCreateManagedWidget("palette", xmToggleButtonWidgetClass, grid, XmNlabelString, s, XmNindicatorOn, XmINDICATOR_NONE, XmNshadowThickness, 2, XmNset, strcmp(pal[i].name, current) == 0 ? XmSET : XmUNSET, XmNlabelType, pm != None ? XmPIXMAP_AND_STRING : XmSTRING, XmNlabelPixmap, pm, XmNpixmapPlacement, XmPIXMAP_TOP, XmNalignment, XmALIGNMENT_CENTER, /* Motif measured the pair a few pixels short of the * Xft text and cut its bottom off (measured) */ XmNmarginHeight, 6, NULL); XmStringFree(s); XtAddCallback(t, XmNvalueChangedCallback, PaletteChosen, (XtPointer) &pal[i]); } appNote = Text(box, "note", ""); { char text[128]; snprintf(text, sizeof text, "In use: %s.", current); SetText(appNote, text); } appLogout = PaneButton(box, "Log Out Now...", LogOutNow, NULL); XtSetSensitive(appLogout, False); return box; } /* ---- Fonts -------------------------------------------------------------- */ #define MAX_FAMILIES 512 static Widget fontList, fontSize, fontPreview; static char *families[MAX_FAMILIES]; static int nfamilies; static int StringCompare(const void *a, const void *b) { return strcmp(*(char *const *) a, *(char *const *) b); } static void ListFamilies(Display *dpy, int scr) { XftFontSet *fs = XftListFonts(dpy, scr, NULL, XFT_FAMILY, NULL); int i, j; nfamilies = 0; if (fs == NULL) return; for (i = 0; i < fs->nfont && nfamilies < MAX_FAMILIES; i++) { FcChar8 *f = NULL; if (FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &f) != FcResultMatch || f == NULL || f[0] == '.') continue; families[nfamilies++] = strdup((const char *) f); } XftFontSetDestroy(fs); qsort(families, (size_t) nfamilies, sizeof families[0], StringCompare); for (i = j = 0; i < nfamilies; i++) { if (j > 0 && strcmp(families[j - 1], families[i]) == 0) { free(families[i]); continue; } families[j++] = families[i]; } nfamilies = j; } static void FontsGone(Widget w, XtPointer data, XtPointer call) { int i; (void) w; (void) data; (void) call; for (i = 0; i < nfamilies; i++) free(families[i]); nfamilies = 0; } /* the chosen family, or NULL */ static const char * Family(void) { int *pos = NULL, n = 0; const char *f = NULL; if (XmListGetSelectedPos(fontList, &pos, &n) && n > 0 && pos[0] >= 1 && pos[0] <= nfamilies) f = families[pos[0] - 1]; XtFree((char *) pos); return f; } /* the preview in the chosen font; its size into SIZE */ static const char * Preview(int *size) { const char *f = Family(); XmRendition r; XmRenderTable rt; Arg a[4]; int n = 0; *size = 10; if (f == NULL) return NULL; XmScaleGetValue(fontSize, size); XtSetArg(a[n], XmNfontType, XmFONT_IS_XFT); n++; XtSetArg(a[n], XmNfontName, (XtArgVal) f); n++; XtSetArg(a[n], XmNfontSize, *size); n++; r = XmRenditionCreate(fontPreview, XmFONTLIST_DEFAULT_TAG, a, n); rt = XmRenderTableAddRenditions(NULL, &r, 1, XmMERGE_REPLACE); XmRenditionFree(r); XtVaSetValues(fontPreview, XmNrenderTable, rt, NULL); XmRenderTableFree(rt); return f; } static void FontApply(Widget w, XtPointer data, XtPointer call) { char res[256]; int size; const char *f = Preview(&size); (void) w; (void) data; (void) call; if (f == NULL) return; /* sys.resources' lines, with the new values */ snprintf(res, sizeof res, "*mcde.fontName: %s\n*mcde.fontSize: %d\n", f, size); _DtAddToResource(XtDisplay(fontList), res); } Widget MakeFonts(Widget parent) { Display *dpy = XtDisplay(parent); Widget box = XtVaCreateWidget("fonts", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); char name[128], size[16]; int i, sel = 0; Arg a[4]; int n = 0; XtAddCallback(box, XmNdestroyCallback, FontsGone, NULL); ServerResource(dpy, "dtsystemsettings.mcde.fontName", "Dtsystemsettings.Mcde.FontName", "Hack", name, sizeof name); ServerResource(dpy, "dtsystemsettings.mcde.fontSize", "Dtsystemsettings.Mcde.FontSize", "10", size, sizeof size); ListFamilies(dpy, XScreenNumberOfScreen(XtScreen(parent))); /* the font in use is offered even when fontconfig does not list it * here (a session without MCDE's fonts.conf) */ for (i = 0; i < nfamilies; i++) if (strcmp(families[i], name) == 0) break; if (i == nfamilies && nfamilies < MAX_FAMILIES) { memmove(families + 1, families, (size_t) nfamilies * sizeof families[0]); families[0] = strdup(name); nfamilies++; } (void) Text(box, "label", "The font of every window:"); XtSetArg(a[n], XmNvisibleItemCount, 10); n++; XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++; XtSetArg(a[n], XmNwidth, 320); n++; fontList = XmCreateScrolledList(box, "families", a, n); for (i = 0; i < nfamilies; i++) { XmString s = XmStringCreateLocalized(families[i]); XmListAddItemUnselected(fontList, s, 0); XmStringFree(s); if (strcmp(families[i], name) == 0) sel = i + 1; } XtManageChild(fontList); if (sel > 0) { XmListSelectPos(fontList, sel, False); XmListSetPos(fontList, sel > 4 ? sel - 4 : 1); } XtAddCallback(fontList, XmNbrowseSelectionCallback, FontApply, NULL); (void) Text(box, "label", "Size (points):"); fontSize = XtVaCreateManagedWidget("size", xmScaleWidgetClass, box, XmNorientation, XmHORIZONTAL, XmNminimum, 7, XmNmaximum, 20, XmNvalue, atoi(size) >= 7 && atoi(size) <= 20 ? atoi(size) : 10, XmNshowValue, True, XmNscaleWidth, 320, NULL); XtAddCallback(fontSize, XmNvalueChangedCallback, FontApply, NULL); fontPreview = Text(box, "preview", "The quick brown fox jumps over the lazy dog. 0123456789"); (void) Text(box, "note", "Programs started from now on use it; the others when they start " "again."); { int sz; (void) Preview(&sz); } return box; }