Files
mcde/programs/dtthemesync/dtthemesync.c
T
lotherkandClaude Opus 5.5 c1367c039b mcde: dtthemesync -- GTK, Qt, KDE and X programs take MCDE's palette
From the session's palette and font, with Motif's own colour calculation:
X resources (and the 16 terminal colours), GTK 1, GTK 2/3/4 themes with
Motif's bevels and no animation, qt5ct/qt6ct schemes and kdeglobals.  It
is also the XSETTINGS manager, so running GTK programs change with the
palette at once; System Settings runs it when a palette or font is chosen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:00:27 +00:00

852 lines
26 KiB
C

/*
* dtthemesync -- MCDE: every toolkit looks like MCDE
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* "MCDE is king, das gesamte andere System muss folgen" (Konrad,
* 2026-11-11). dtthemesync reads the palette MCDE uses and its font and
* writes the look of the other toolkits from them:
*
* X resources *foreground, *background, *color0..15 (xterm and
* the X clients), merged into RESOURCE_MANAGER
* GTK 1 ~/.gtkrc
* GTK 2, 3, 4 ~/.themes/MCDE-a|MCDE-b/gtk-2.0, gtk-3.0, gtk-4.0
* Qt 5, 6 ~/.config/qt5ct, qt6ct (colour scheme, Fusion)
* KDE programs ~/.config/kdeglobals [Colors:*]
*
* Colours: a palette (*.dp) is eight backgrounds; the foreground, select
* colour and shadows of each come from Motif's XmCalculateColorRGB, as the
* colour server makes them (dtsession/SrvFile_io.c). The sets' jobs are
* CDE's: 1 active window, 2 inactive, 4 text fields, 5 main windows, 6 menus
* and dialogs, 8 the front panel (docs/reference/cde-colorsets.txt in the
* Makronix tree). Edges are Motif's bevels -- top shadow light, bottom
* shadow dark -- and nothing is animated, blurred or shadowed.
*
* Live: GTK 2, 3 and 4 follow an XSETTINGS manager; dtthemesync is one
* ("dtthemesync -daemon", started at login) and on every run switches its
* Net/ThemeName between MCDE-a and MCDE-b, which makes running GTK programs
* load the new files. GTK 1 programs re-read ~/.gtkrc at _GTK_READ_RCFILES.
* Qt and xterm take the colours when they start.
*
* usage: dtthemesync [-v] [-palette NAME.dp] | -daemon
* Run by Xsession.d/0078.dtthemesync and by System Settings, Appearance.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <Xm/Xm.h>
#ifndef CDE_INSTALLATION_TOP
#define CDE_INSTALLATION_TOP "/usr/dt"
#endif
#ifndef CDE_CONFIGURATION_TOP
#define CDE_CONFIGURATION_TOP "/etc/dt"
#endif
typedef struct { unsigned short r, g, b; } Rgb;
typedef struct { Rgb bg, fg, sc, ts, bs; } ColorSet;
static ColorSet set[9]; /* 1..8 */
static char font[64] = "Hack";
static int fontSize = 10;
static int verbose;
static char home[1024];
/* ---- the palette ---------------------------------------------------------- */
static void
Hex(const Rgb *c, char *buf, size_t len)
{
snprintf(buf, len, "#%02x%02x%02x", c->r >> 8, c->g >> 8, c->b >> 8);
}
/* "r,g,b" for Qt and KDE */
static void
Dec(const Rgb *c, char *buf, size_t len)
{
snprintf(buf, len, "%d,%d,%d", c->r >> 8, c->g >> 8, c->b >> 8);
}
static Rgb
FromX(const XColor *x)
{
Rgb c = { x->red, x->green, x->blue };
return c;
}
/* a palette file NAME from the places the colour server looks */
static FILE *
OpenPalette(const char *name)
{
const char *dirs[4];
char path[2048], user[1100];
FILE *f;
int i;
snprintf(user, sizeof user, "%s/.dt/palettes", home);
dirs[0] = user;
dirs[1] = CDE_CONFIGURATION_TOP "/palettes";
dirs[2] = CDE_INSTALLATION_TOP "/palettes";
dirs[3] = NULL;
for (i = 0; dirs[i] != NULL; i++) {
snprintf(path, sizeof path, "%s/%s", dirs[i], name);
if ((f = fopen(path, "r")) != NULL) {
if (verbose)
fprintf(stderr, "dtthemesync: palette %s\n", path);
return f;
}
}
return NULL;
}
static int
LoadPalette(Display *dpy, const char *name)
{
XmColorProc calc = XmGetColorCalculation();
FILE *f = OpenPalette(name);
char line[256];
int n = 0;
if (f == NULL)
return -1;
while (n < 8 && fgets(line, sizeof line, f) != NULL) {
XColor bg, fg, sc, ts, bs;
line[strcspn(line, " \t\r\n")] = '\0';
if (line[0] != '#' || !XParseColor(dpy,
DefaultColormap(dpy, DefaultScreen(dpy)), line, &bg))
continue;
(*calc)(&bg, &fg, &sc, &ts, &bs);
n++;
set[n].bg = FromX(&bg);
set[n].fg = FromX(&fg);
set[n].sc = FromX(&sc);
set[n].ts = FromX(&ts);
set[n].bs = FromX(&bs);
}
fclose(f);
return n == 8 ? 0 : -1;
}
/* the palette and font of the session: dtsession's ColorPalette, MCDE's
* font resources (System Settings, Appearance and Fonts) */
static void
SessionSettings(Display *dpy, char *palette, size_t len)
{
char *rm = XResourceManagerString(dpy), *type;
XrmDatabase db;
XrmValue v;
snprintf(palette, len, "Default.dp");
if (rm == NULL)
return;
XrmInitialize();
db = XrmGetStringDatabase(rm);
if (XrmGetResource(db, "dtsession.0.colorPalette",
"Dtsession.0.ColorPalette", &type, &v) && v.addr != NULL)
snprintf(palette, len, "%s", (char *) v.addr);
if (XrmGetResource(db, "mcde.fontName", "Mcde.FontName", &type, &v) &&
v.addr != NULL)
snprintf(font, sizeof font, "%s", (char *) v.addr);
if (XrmGetResource(db, "mcde.fontSize", "Mcde.FontSize", &type, &v) &&
v.addr != NULL && atoi((char *) v.addr) > 4)
fontSize = atoi((char *) v.addr);
XrmDestroyDatabase(db);
}
/* ---- files ---------------------------------------------------------------- */
static void
MakeDirs(const char *path)
{
char buf[2048], *p;
snprintf(buf, sizeof buf, "%s", path);
for (p = buf + 1; *p != '\0'; p++)
if (*p == '/') {
*p = '\0';
(void) mkdir(buf, 0755);
*p = '/';
}
(void) mkdir(buf, 0755);
}
/* FMT into PATH below HOME, written beside and renamed over the old file */
static int
WriteFile(const char *rel, const char *text)
{
char path[2048], tmp[2100], dir[2048], *slash;
FILE *f;
snprintf(path, sizeof path, "%s/%s", home, rel);
snprintf(dir, sizeof dir, "%s", path);
if ((slash = strrchr(dir, '/')) != NULL) {
*slash = '\0';
MakeDirs(dir);
}
snprintf(tmp, sizeof tmp, "%s.new", path);
if ((f = fopen(tmp, "w")) == NULL)
return -1;
fputs(text, f);
if (fclose(f) != 0 || rename(tmp, path) != 0)
return -1;
if (verbose)
fprintf(stderr, "dtthemesync: %s\n", path);
return 0;
}
/* ---- the colours by job ----------------------------------------------- */
#define ACTIVE (&set[1])
#define TEXT (&set[4])
#define MAIN (&set[5])
#define MENU (&set[6])
/* the 16 terminal colours: the ANSI hues CDE's dtterm has, black and white
* taken from the palette's darkest and lightest shadows so that a terminal
* sits in the scheme */
static const char *ansi[16] = {
NULL, "#b21818", "#18b218", "#b26818", "#1818b2", "#b218b2",
"#18b2b2", NULL, NULL, "#ff5454", "#54ff54", "#ffff54", "#5454ff",
"#ff54ff", "#54ffff", NULL };
static void
XResources(Display *dpy, char *out, size_t len)
{
char bg[16], fg[16], c0[16], c7[16], c8[16], c15[16];
size_t at;
int i;
Hex(&TEXT->bg, bg, sizeof bg);
Hex(&TEXT->fg, fg, sizeof fg);
Hex(&MAIN->bs, c0, sizeof c0);
Hex(&MAIN->bg, c7, sizeof c7);
Hex(&MAIN->bs, c8, sizeof c8);
Hex(&MAIN->ts, c15, sizeof c15);
at = (size_t) snprintf(out, len,
"! MCDE colours for X programs that are not Motif (dtthemesync)\n"
"*foreground: %s\n*background: %s\n"
"XTerm*foreground: %s\nXTerm*background: %s\n"
"XTerm*faceName: %s\nXTerm*faceSize: %d\n"
"XClock*background: %s\nXLoad*background: %s\n",
fg, bg, fg, bg, font, fontSize, bg, bg);
for (i = 0; i < 16 && at < len; i++)
at += (size_t) snprintf(out + at, len - at, "*color%d: %s\n", i,
i == 0 ? c0 : i == 7 ? c7 : i == 8 ? c8 : i == 15 ? c15 :
ansi[i]);
(void) dpy;
}
static void
Gtk1(char *out, size_t len)
{
char b[16], f[16], t[16], tf[16], s[16], sf[16], m[16], mf[16], l[16];
Hex(&MAIN->bg, b, sizeof b);
Hex(&MAIN->fg, f, sizeof f);
Hex(&TEXT->bg, t, sizeof t);
Hex(&TEXT->fg, tf, sizeof tf);
Hex(&ACTIVE->bg, s, sizeof s);
Hex(&ACTIVE->fg, sf, sizeof sf);
Hex(&MENU->bg, m, sizeof m);
Hex(&MENU->fg, mf, sizeof mf);
Hex(&MAIN->ts, l, sizeof l);
snprintf(out, len,
"# MCDE (dtthemesync): written from the palette, changes are lost\n"
"style \"mcde\" {\n"
" fontset = \"-*-%s-medium-r-normal--*-%d0-*-*-*-*-*-*\"\n"
" bg[NORMAL] = \"%s\"\n bg[PRELIGHT] = \"%s\"\n"
" bg[ACTIVE] = \"%s\"\n bg[SELECTED] = \"%s\"\n"
" bg[INSENSITIVE] = \"%s\"\n"
" fg[NORMAL] = \"%s\"\n fg[PRELIGHT] = \"%s\"\n"
" fg[ACTIVE] = \"%s\"\n fg[SELECTED] = \"%s\"\n"
" base[NORMAL] = \"%s\"\n base[SELECTED] = \"%s\"\n"
" text[NORMAL] = \"%s\"\n text[SELECTED] = \"%s\"\n"
"}\n"
"style \"mcde-menu\" {\n"
" bg[NORMAL] = \"%s\"\n fg[NORMAL] = \"%s\"\n"
" bg[PRELIGHT] = \"%s\"\n fg[PRELIGHT] = \"%s\"\n"
"}\n"
"widget_class \"*\" style \"mcde\"\n"
"widget_class \"*Menu*\" style \"mcde-menu\"\n",
font, fontSize, b, l, b, s, b, f, f, f, sf, t, s, tf, sf,
m, mf, s, sf);
}
static void
Gtk2(char *out, size_t len)
{
char b[16], f[16], t[16], tf[16], s[16], sf[16], m[16], mf[16], l[16],
d[16];
Hex(&MAIN->bg, b, sizeof b);
Hex(&MAIN->fg, f, sizeof f);
Hex(&TEXT->bg, t, sizeof t);
Hex(&TEXT->fg, tf, sizeof tf);
Hex(&ACTIVE->bg, s, sizeof s);
Hex(&ACTIVE->fg, sf, sizeof sf);
Hex(&MENU->bg, m, sizeof m);
Hex(&MENU->fg, mf, sizeof mf);
Hex(&MAIN->ts, l, sizeof l);
Hex(&MAIN->bs, d, sizeof d);
snprintf(out, len,
"# MCDE (dtthemesync): written from the palette, changes are lost\n"
"gtk-font-name = \"%s %d\"\n"
"gtk-enable-animations = 0\n"
"style \"mcde\" {\n"
" xthickness = 2\n ythickness = 2\n"
" bg[NORMAL] = \"%s\"\n bg[PRELIGHT] = \"%s\"\n"
" bg[ACTIVE] = \"%s\"\n bg[SELECTED] = \"%s\"\n"
" bg[INSENSITIVE] = \"%s\"\n"
" fg[NORMAL] = \"%s\"\n fg[PRELIGHT] = \"%s\"\n"
" fg[ACTIVE] = \"%s\"\n fg[SELECTED] = \"%s\"\n"
" fg[INSENSITIVE] = \"%s\"\n"
" base[NORMAL] = \"%s\"\n base[SELECTED] = \"%s\"\n"
" base[ACTIVE] = \"%s\"\n"
" text[NORMAL] = \"%s\"\n text[SELECTED] = \"%s\"\n"
" text[ACTIVE] = \"%s\"\n"
" GtkWidget::focus-line-width = 1\n"
"}\n"
"style \"mcde-menu\" {\n"
" bg[NORMAL] = \"%s\"\n fg[NORMAL] = \"%s\"\n"
" bg[PRELIGHT] = \"%s\"\n fg[PRELIGHT] = \"%s\"\n"
"}\n"
"class \"GtkWidget\" style \"mcde\"\n"
"widget_class \"*Menu*\" style \"mcde-menu\"\n"
"widget_class \"*MenuBar*\" style \"mcde-menu\"\n",
font, fontSize, b, l, d, s, b, f, f, f, sf, d, t, s, s, tf, sf, sf,
m, mf, s, sf);
}
/* GTK 3 and 4: the built-in theme with MCDE's colours over it, Motif's
* bevels (outset/inset in the shadow colours) and nothing that moves */
static void
GtkCss(char *out, size_t len, int four)
{
char b[16], f[16], t[16], tf[16], s[16], sf[16], m[16], mf[16], l[16],
d[16], a[16];
size_t at;
Hex(&MAIN->bg, b, sizeof b);
Hex(&MAIN->fg, f, sizeof f);
Hex(&TEXT->bg, t, sizeof t);
Hex(&TEXT->fg, tf, sizeof tf);
Hex(&ACTIVE->bg, s, sizeof s);
Hex(&ACTIVE->fg, sf, sizeof sf);
Hex(&MENU->bg, m, sizeof m);
Hex(&MENU->fg, mf, sizeof mf);
Hex(&MAIN->ts, l, sizeof l);
Hex(&MAIN->bs, d, sizeof d);
Hex(&MAIN->sc, a, sizeof a);
at = (size_t) snprintf(out, len,
"/* MCDE (dtthemesync): written from the palette, changes are lost */\n"
"@import url(\"%s\");\n\n",
four ? "resource:///org/gtk/libgtk/theme/Default/gtk-light.css" :
"resource:///org/gtk/libgtk/theme/Adwaita/gtk-contained.css");
/* the names both GTK 3 (Adwaita) and GTK 4 (Default) use */
at += (size_t) snprintf(out + at, len - at,
"@define-color theme_bg_color %s;\n"
"@define-color theme_fg_color %s;\n"
"@define-color theme_base_color %s;\n"
"@define-color theme_text_color %s;\n"
"@define-color theme_selected_bg_color %s;\n"
"@define-color theme_selected_fg_color %s;\n"
"@define-color insensitive_bg_color %s;\n"
"@define-color borders %s;\n"
"@define-color window_bg_color %s;\n"
"@define-color window_fg_color %s;\n"
"@define-color view_bg_color %s;\n"
"@define-color view_fg_color %s;\n"
"@define-color accent_bg_color %s;\n"
"@define-color accent_fg_color %s;\n"
"@define-color headerbar_bg_color %s;\n"
"@define-color headerbar_fg_color %s;\n"
"@define-color popover_bg_color %s;\n"
"@define-color popover_fg_color %s;\n"
"@define-color card_bg_color %s;\n"
"@define-color mcde_light %s;\n"
"@define-color mcde_dark %s;\n\n",
b, f, t, tf, s, sf, b, d, b, f, t, tf, s, sf, m, mf, m, mf, b, l, d);
at += (size_t) snprintf(out + at, len - at,
"* { font-family: \"%s\"; font-size: %dpt;\n"
" box-shadow: none; text-shadow: none; -gtk-icon-shadow: none;\n"
" transition: none; animation: none; background-image: none;\n"
" border-radius: 0; }\n"
"window, .background, dialog { background-color: %s; color: %s; }\n"
"entry, textview text, treeview.view, list, .view { "
"background-color: %s; color: %s; }\n"
"button, combobox button, spinbutton button, headerbar button {\n"
" background-color: %s; color: %s;\n"
" border: 2px solid; border-color: %s %s %s %s; }\n"
"button:active, button:checked {\n"
" border-color: %s %s %s %s; background-color: %s; }\n"
"entry, textview, frame > border, scrolledwindow.frame {\n"
" border: 2px solid; border-color: %s %s %s %s; }\n"
"menubar, menu, .menu, popover > contents, .popup {\n"
" background-color: %s; color: %s; }\n"
"menu menuitem:hover, .menu menuitem:hover, popover modelbutton:hover {\n"
" background-color: %s; color: %s; }\n"
"headerbar, .titlebar { background-color: %s; color: %s; }\n"
"*:selected, selection { background-color: %s; color: %s; }\n",
font, fontSize, b, f, t, tf, b, f, l, d, d, l, d, l, l, d, a,
d, l, l, d, m, mf, s, sf, m, mf, s, sf);
}
/* Qt: a qt5ct/qt6ct colour scheme -- twenty-one colours in QPalette's
* order: WindowText Button Light Midlight Dark Mid Text BrightText
* ButtonText Base Window Shadow Highlight HighlightedText Link LinkVisited
* AlternateBase NoRole ToolTipBase ToolTipText PlaceholderText */
static void
QtScheme(char *out, size_t len)
{
char f[16], b[16], l[16], d[16], t[16], tf[16], s[16], sf[16], m[16],
mf[16];
char row[1024];
Hex(&MAIN->fg, f, sizeof f);
Hex(&MAIN->bg, b, sizeof b);
Hex(&MAIN->ts, l, sizeof l);
Hex(&MAIN->bs, d, sizeof d);
Hex(&TEXT->bg, t, sizeof t);
Hex(&TEXT->fg, tf, sizeof tf);
Hex(&ACTIVE->bg, s, sizeof s);
Hex(&ACTIVE->fg, sf, sizeof sf);
Hex(&MENU->bg, m, sizeof m);
Hex(&MENU->fg, mf, sizeof mf);
snprintf(row, sizeof row,
"%s, %s, %s, %s, %s, %s, %s, #ffffff, %s, %s, %s, #000000, %s, %s, "
"#0000ee, #551a8b, %s, #000000, %s, %s, %s",
f, b, l, l, d, d, tf, f, t, b, s, sf, b, m, mf, d);
snprintf(out, len,
"# MCDE (dtthemesync): written from the palette, changes are lost\n"
"[ColorScheme]\nactive_colors=%s\ninactive_colors=%s\n"
"disabled_colors=%s\n", row, row, row);
}
static void
QtConf(char *out, size_t len, const char *scheme)
{
snprintf(out, len,
"[Appearance]\ncolor_scheme_path=%s\ncustom_palette=true\n"
"style=Fusion\nstandard_dialogs=default\n\n"
"[Fonts]\nfixed=\"%s,%d,-1,5,50,0,0,0,0,0\"\n"
"general=\"%s,%d,-1,5,50,0,0,0,0,0\"\n\n"
"[Interface]\nactivate_item_on_single_click=1\n"
"gui_effects=@Invalid()\nmenus_have_icons=true\n",
scheme, font, fontSize, font, fontSize);
}
static void
KdeGlobals(char *out, size_t len)
{
char f[24], b[24], t[24], tf[24], s[24], sf[24], m[24], mf[24];
static const char *groups[] = { "Window", "View", "Button", "Selection",
"Tooltip", "Header" };
size_t at = 0;
int i;
Dec(&MAIN->fg, f, sizeof f);
Dec(&MAIN->bg, b, sizeof b);
Dec(&TEXT->bg, t, sizeof t);
Dec(&TEXT->fg, tf, sizeof tf);
Dec(&ACTIVE->bg, s, sizeof s);
Dec(&ACTIVE->fg, sf, sizeof sf);
Dec(&MENU->bg, m, sizeof m);
Dec(&MENU->fg, mf, sizeof mf);
at += (size_t) snprintf(out + at, len - at,
"# MCDE (dtthemesync): written from the palette, changes are lost\n"
"[General]\nColorScheme=MCDE\nfont=%s,%d,-1,5,50,0,0,0,0,0\n\n"
"[KDE]\nAnimationDurationFactor=0\n\n", font, fontSize);
for (i = 0; i < 6 && at < len; i++) {
const char *bg = i == 1 ? t : i == 3 ? s : i == 4 ? m : b;
const char *fg = i == 1 ? tf : i == 3 ? sf : i == 4 ? mf : f;
at += (size_t) snprintf(out + at, len - at,
"[Colors:%s]\nBackgroundNormal=%s\nBackgroundAlternate=%s\n"
"ForegroundNormal=%s\nDecorationFocus=%s\n"
"DecorationHover=%s\n\n", groups[i], bg, bg, fg, s, s);
}
}
/* ---- XSETTINGS ------------------------------------------------------------- */
static Atom
SettingsSelection(Display *dpy)
{
char name[32];
snprintf(name, sizeof name, "_XSETTINGS_S%d", DefaultScreen(dpy));
return XInternAtom(dpy, name, False);
}
static size_t
Pad4(size_t n)
{
return (n + 3) & ~(size_t) 3;
}
static size_t
PutString(unsigned char *p, const char *name, const char *value,
unsigned long serial)
{
size_t at = 0, n = strlen(name), v = strlen(value);
unsigned int u;
p[at++] = 1; /* XSettingsTypeString */
p[at++] = 0;
p[at++] = (unsigned char) (n & 0xff); /* CARD16, LSB first */
p[at++] = (unsigned char) (n >> 8);
memset(p + at, 0, Pad4(n));
memcpy(p + at, name, n);
at += Pad4(n);
u = (unsigned int) serial;
memcpy(p + at, &u, 4);
at += 4;
u = (unsigned int) v;
memcpy(p + at, &u, 4);
at += 4;
memset(p + at, 0, Pad4(v));
memcpy(p + at, value, v);
return at + Pad4(v);
}
static size_t
PutInt(unsigned char *p, const char *name, int value, unsigned long serial)
{
size_t at = 0, n = strlen(name);
unsigned int u;
p[at++] = 0; /* XSettingsTypeInteger */
p[at++] = 0;
p[at++] = (unsigned char) (n & 0xff);
p[at++] = (unsigned char) (n >> 8);
memset(p + at, 0, Pad4(n));
memcpy(p + at, name, n);
at += Pad4(n);
u = (unsigned int) serial;
memcpy(p + at, &u, 4);
at += 4;
memcpy(p + at, &value, 4);
return at + 4;
}
/* the settings on WINDOW: the theme (A or B), the font, no animations;
* byte order LSB first (x86 and the like; a client reads either) */
static void
WriteSettings(Display *dpy, Window w, const char *theme, unsigned long serial)
{
unsigned char buf[1024];
char fontName[96];
size_t at = 12;
unsigned int u;
snprintf(fontName, sizeof fontName, "%s %d", font, fontSize);
memset(buf, 0, 12);
{
unsigned int probe = 1;
buf[0] = *(unsigned char *) &probe ? 0 /* LSBFirst */ :
1 /* MSBFirst */;
}
at += PutString(buf + at, "Net/ThemeName", theme, serial);
at += PutString(buf + at, "Gtk/FontName", fontName, serial);
at += PutInt(buf + at, "Gtk/EnableAnimations", 0, serial);
at += PutInt(buf + at, "Net/CursorBlink", 0, serial);
u = (unsigned int) serial;
memcpy(buf + 4, &u, 4);
u = 4;
memcpy(buf + 8, &u, 4);
XChangeProperty(dpy, w, XInternAtom(dpy, "_XSETTINGS_SETTINGS", False),
XInternAtom(dpy, "_XSETTINGS_SETTINGS", False), 8, PropModeReplace,
buf, (int) at);
XFlush(dpy);
}
/* -daemon: own the XSETTINGS selection for this screen and keep the window
* that carries the settings; runs until the session ends */
static int
Daemon(void)
{
Display *dpy = XOpenDisplay(NULL);
Atom sel;
Window w;
XEvent ev;
if (dpy == NULL)
return 1;
sel = SettingsSelection(dpy);
if (XGetSelectionOwner(dpy, sel) != None) {
if (verbose)
fprintf(stderr, "dtthemesync: XSETTINGS has an owner\n");
XCloseDisplay(dpy);
return 0;
}
w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), -10, -10, 1, 1, 0,
0, 0);
XSelectInput(dpy, w, PropertyChangeMask);
WriteSettings(dpy, w, "MCDE-a", 1);
XSetSelectionOwner(dpy, sel, w, CurrentTime);
if (XGetSelectionOwner(dpy, sel) != w)
return 1;
/* the clients learn of the owner from MANAGER on the root window */
ev.xclient.type = ClientMessage;
ev.xclient.window = DefaultRootWindow(dpy);
ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = CurrentTime;
ev.xclient.data.l[1] = (long) sel;
ev.xclient.data.l[2] = (long) w;
ev.xclient.data.l[3] = 0;
ev.xclient.data.l[4] = 0;
XSendEvent(dpy, DefaultRootWindow(dpy), False, StructureNotifyMask, &ev);
XFlush(dpy);
for (;;) {
XNextEvent(dpy, &ev);
if (ev.type == SelectionClear)
break;
}
XCloseDisplay(dpy);
return 0;
}
/* GTK 1 programs read ~/.gtkrc again at this message (gtkwindow.c) */
static void
Gtk1Reload(Display *dpy)
{
XEvent ev;
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.message_type = XInternAtom(dpy, "_GTK_READ_RCFILES", False);
ev.xclient.format = 8;
ev.xclient.window = DefaultRootWindow(dpy);
XSendEvent(dpy, DefaultRootWindow(dpy), False, NoEventMask, &ev);
}
/* ---- main ------------------------------------------------------------------ */
int
main(int argc, char **argv)
{
static char buf[32768];
char palette[256] = "", theme[16], state[1100], scheme[1200];
const char *h = getenv("HOME");
Display *dpy;
Window owner;
FILE *f;
int i, daemon = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-v") == 0)
verbose = 1;
else if (strcmp(argv[i], "-daemon") == 0)
daemon = 1;
else if (strcmp(argv[i], "-palette") == 0 && i + 1 < argc)
snprintf(palette, sizeof palette, "%s", argv[++i]);
else {
fprintf(stderr, "usage: dtthemesync [-v] [-palette "
"NAME.dp] | -daemon\n");
return 2;
}
}
if (h == NULL || h[0] == '\0') {
fprintf(stderr, "dtthemesync: HOME is not set\n");
return 1;
}
snprintf(home, sizeof home, "%s", h);
if (daemon)
return Daemon();
/* Motif's colour calculation needs Xt and a shell of Motif's (it reads
* the display's colour thresholds); the shell is never shown */
{
XtAppContext app;
int ac = 0;
XtToolkitInitialize();
app = XtCreateApplicationContext();
dpy = XtOpenDisplay(app, NULL, "dtthemesync", "Dtthemesync",
NULL, 0, &ac, NULL);
if (dpy == NULL) {
fprintf(stderr, "dtthemesync: cannot open the display\n");
return 1;
}
(void) XtVaAppCreateShell("dtthemesync", "Dtthemesync",
applicationShellWidgetClass, dpy, XmNmappedWhenManaged,
False, NULL);
}
{
char p[256];
SessionSettings(dpy, p, sizeof p);
if (palette[0] == '\0')
snprintf(palette, sizeof palette, "%s", p);
}
if (LoadPalette(dpy, palette) != 0) {
fprintf(stderr, "dtthemesync: no palette %s\n", palette);
return 1;
}
/* the theme name flips at every run, so that GTK loads it anew */
snprintf(state, sizeof state, "%s/.dt/themesync.state", home);
snprintf(theme, sizeof theme, "MCDE-a");
if ((f = fopen(state, "r")) != NULL) {
char last[16] = "";
if (fscanf(f, "%15s", last) == 1 && strcmp(last, "MCDE-a") == 0)
snprintf(theme, sizeof theme, "MCDE-b");
fclose(f);
}
XResources(dpy, buf, sizeof buf);
(void) WriteFile(".dt/themesync.Xresources", buf);
{
/* merged as dtsession and dtstyle merge theirs */
XrmDatabase cur, add = XrmGetStringDatabase(buf);
char *rm = XResourceManagerString(dpy);
cur = rm != NULL ? XrmGetStringDatabase(rm) : NULL;
if (cur != NULL) {
char tmp[1200];
XrmMergeDatabases(add, &cur);
snprintf(tmp, sizeof tmp, "%s/.dt/themesync.rm", home);
XrmPutFileDatabase(cur, tmp);
if ((f = fopen(tmp, "r")) != NULL) {
static char all[262144];
size_t n = fread(all, 1, sizeof all - 1, f);
all[n] = '\0';
fclose(f);
XChangeProperty(dpy, DefaultRootWindow(dpy),
XA_RESOURCE_MANAGER, XA_STRING, 8,
PropModeReplace, (unsigned char *) all,
(int) n);
}
(void) unlink(tmp);
} else
XChangeProperty(dpy, DefaultRootWindow(dpy),
XA_RESOURCE_MANAGER, XA_STRING, 8, PropModeReplace,
(unsigned char *) buf, (int) strlen(buf));
}
Gtk1(buf, sizeof buf);
(void) WriteFile(".gtkrc", buf);
Gtk2(buf, sizeof buf);
{
char rel[64];
snprintf(rel, sizeof rel, ".themes/%s/gtk-2.0/gtkrc", theme);
(void) WriteFile(rel, buf);
GtkCss(buf, sizeof buf, 0);
snprintf(rel, sizeof rel, ".themes/%s/gtk-3.0/gtk.css", theme);
(void) WriteFile(rel, buf);
GtkCss(buf, sizeof buf, 1);
snprintf(rel, sizeof rel, ".themes/%s/gtk-4.0/gtk.css", theme);
(void) WriteFile(rel, buf);
snprintf(rel, sizeof rel, ".themes/%s/index.theme", theme);
snprintf(buf, sizeof buf, "[Desktop Entry]\nType=X-GNOME-Metatheme\n"
"Name=%s\nComment=MCDE, from the palette %s (dtthemesync)\n"
"Encoding=UTF-8\n\n[X-GNOME-Metatheme]\nGtkTheme=%s\n",
theme, palette, theme);
(void) WriteFile(rel, buf);
}
/* gtk-2.0 programs started without XSETTINGS read this */
snprintf(buf, sizeof buf, "# MCDE (dtthemesync)\ngtk-theme-name = \"%s\"\n"
"include \"%s/.themes/%s/gtk-2.0/gtkrc\"\n", theme, home, theme);
(void) WriteFile(".gtkrc-2.0", buf);
snprintf(buf, sizeof buf, "[Settings]\ngtk-theme-name=%s\n"
"gtk-font-name=%s %d\ngtk-enable-animations=false\n", theme, font,
fontSize);
(void) WriteFile(".config/gtk-3.0/settings.ini", buf);
(void) WriteFile(".config/gtk-4.0/settings.ini", buf);
QtScheme(buf, sizeof buf);
(void) WriteFile(".config/qt5ct/colors/MCDE.conf", buf);
(void) WriteFile(".config/qt6ct/colors/MCDE.conf", buf);
snprintf(scheme, sizeof scheme, "%s/.config/qt5ct/colors/MCDE.conf", home);
QtConf(buf, sizeof buf, scheme);
(void) WriteFile(".config/qt5ct/qt5ct.conf", buf);
snprintf(scheme, sizeof scheme, "%s/.config/qt6ct/colors/MCDE.conf", home);
QtConf(buf, sizeof buf, scheme);
(void) WriteFile(".config/qt6ct/qt6ct.conf", buf);
KdeGlobals(buf, sizeof buf);
(void) WriteFile(".config/kdeglobals", buf);
if ((f = fopen(state, "w")) != NULL) {
fprintf(f, "%s\n", theme);
fclose(f);
}
/* live: the XSETTINGS manager's window gets the new theme name */
owner = XGetSelectionOwner(dpy, SettingsSelection(dpy));
if (owner == None) {
pid_t pid = fork();
if (pid == 0) {
/* a daemon: nothing of ours open (a pipe of the caller
* would wait for it) */
int fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
(void) dup2(fd, 0);
(void) dup2(fd, 1);
(void) dup2(fd, 2);
if (fd > 2)
(void) close(fd);
}
setsid();
execl(CDE_INSTALLATION_TOP "/bin/dtthemesync",
"dtthemesync", "-daemon", (char *) NULL);
_exit(127);
}
for (i = 0; i < 20 && owner == None; i++) {
usleep(100000);
owner = XGetSelectionOwner(dpy, SettingsSelection(dpy));
}
}
if (owner != None) {
static unsigned long serial;
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
/* the next serial after the one there */
if (XGetWindowProperty(dpy, owner,
XInternAtom(dpy, "_XSETTINGS_SETTINGS", False), 0, 2, False,
AnyPropertyType, &type, &format, &n, &after, &data) ==
Success && data != NULL && n >= 8) {
unsigned int u;
memcpy(&u, data + 4, 4);
serial = u + 1;
} else
serial = 2;
if (data != NULL)
XFree(data);
WriteSettings(dpy, owner, theme, serial);
}
Gtk1Reload(dpy);
XFlush(dpy);
if (verbose)
fprintf(stderr, "dtthemesync: %s from %s, font %s %d\n", theme,
palette, font, fontSize);
return 0;
}