master ohmu/fastfetch-openbsd / src / detection / wmtheme / wmtheme_linux.c
ohmu  ·  2026-10-15
  1#include "wmtheme.h"
  2#include "common/io.h"
  3#include "common/properties.h"
  4#include "common/parsing.h"
  5#include "common/settings.h"
  6#include "common/strutil.h"
  7#include "common/mallocHelper.h"
  8#include "detection/gtk_qt/gtk_qt.h"
  9#include "detection/displayserver/displayserver.h"
 10
 11static bool detectWMThemeFromConfigFile(const char* configFile, const char* themeRegex, const char* defaultValue, FFstrbuf* themeOrError) {
 12    if (!ffParsePropFileConfig(configFile, themeRegex, themeOrError)) {
 13        ffStrbufAppendF(themeOrError, "Config file %s doesn't exist", configFile);
 14        return false;
 15    }
 16
 17    if (themeOrError->length == 0) {
 18        if (defaultValue == nullptr) {
 19            ffStrbufAppendF(themeOrError, "Couldn't find WM theme in %s", configFile);
 20            return false;
 21        }
 22
 23        ffStrbufAppendS(themeOrError, defaultValue);
 24        return true;
 25    }
 26
 27    // Remove Plasma-generated prefixes
 28    uint32_t idx = 0;
 29
 30    idx = ffStrbufFirstIndexS(themeOrError, "qml_");
 31    if (idx != themeOrError->length) {
 32        ffStrbufSubstrAfter(themeOrError, idx + 3);
 33    }
 34
 35    idx = ffStrbufFirstIndexS(themeOrError, "svg__");
 36    if (idx != themeOrError->length) {
 37        ffStrbufSubstrAfter(themeOrError, idx + 4);
 38    }
 39
 40    return true;
 41}
 42
 43static bool detectWMThemeFromSettings(const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey, FFstrbuf* themeOrError) {
 44    const char* theme = ffSettingsGetGnome(dconfKey, gsettingsSchemaName, gsettingsPath, gsettingsKey, FF_VARIANT_TYPE_STRING).strValue;
 45
 46    if (!ffStrSet(theme)) {
 47        ffStrbufAppendS(themeOrError, "Couldn't find WM theme in DConf or GSettings");
 48        return false;
 49    }
 50
 51    ffStrbufAppendS(themeOrError, theme);
 52    return true;
 53}
 54
 55static bool detectGTKThemeAsWMTheme(FFstrbuf* themeOrError) {
 56    const FFGTKResult* gtk = ffDetectGTK4();
 57    if (gtk->theme.length > 0) {
 58        goto ok;
 59    }
 60
 61    gtk = ffDetectGTK3();
 62    if (gtk->theme.length > 0) {
 63        goto ok;
 64    }
 65
 66    gtk = ffDetectGTK2();
 67    if (gtk->theme.length > 0) {
 68        goto ok;
 69    }
 70
 71    ffStrbufAppendS(themeOrError, "Couldn't detect GTK4/3/2 theme");
 72    return false;
 73
 74ok:
 75    ffStrbufAppend(themeOrError, &gtk->theme);
 76    return true;
 77}
 78
 79static bool detectMutter(FFstrbuf* themeOrError) {
 80    const char* theme = ffSettingsGetGnome("/org/gnome/shell/extensions/user-theme/name", "org.gnome.shell.extensions.user-theme", nullptr, "name", FF_VARIANT_TYPE_STRING).strValue;
 81    if (ffStrSet(theme)) {
 82        ffStrbufAppendS(themeOrError, theme);
 83        return true;
 84    }
 85
 86    return detectGTKThemeAsWMTheme(themeOrError);
 87}
 88
 89static bool detectMuffin(FFstrbuf* themeOrError) {
 90    FF_AUTO_FREE const char* name = ffSettingsGetGnome("/org/cinnamon/theme/name", "org.cinnamon.theme", nullptr, "name", FF_VARIANT_TYPE_STRING).strValue;
 91    FF_AUTO_FREE const char* theme = ffSettingsGetGnome("/org/cinnamon/desktop/wm/preferences/theme", "org.cinnamon.desktop.wm.preferences", nullptr, "theme", FF_VARIANT_TYPE_STRING).strValue;
 92
 93    if (name == nullptr && theme == nullptr) {
 94        ffStrbufAppendS(themeOrError, "Couldn't find muffin theme in GSettings / DConf");
 95        return false;
 96    }
 97
 98    if (name == nullptr) {
 99        ffStrbufAppendS(themeOrError, theme);
100        return true;
101    }
102
103    if (theme == nullptr) {
104        ffStrbufAppendS(themeOrError, name);
105        return true;
106    }
107
108    ffStrbufAppendF(themeOrError, "%s (%s)", name, theme);
109    return true;
110}
111
112static bool detectXFWM4(FFstrbuf* themeOrError) {
113    const char* theme = ffSettingsGetXFConf("xfwm4", "/general/theme", FF_VARIANT_TYPE_STRING).strValue;
114
115    if (theme == nullptr) {
116        ffStrbufAppendS(themeOrError, "Couldn't find xfwm4::/general/theme in XFConf");
117        return false;
118    }
119
120    ffStrbufAppendS(themeOrError, theme);
121    return true;
122}
123
124static bool detectOpenbox(const FFstrbuf* dePrettyName, FFstrbuf* themeOrError) {
125    FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateA(64);
126    const char* configFileSubpath = "openbox/rc.xml";
127    if (ffStrbufIgnCaseEqualS(dePrettyName, "LXQt")) {
128        configFileSubpath = "openbox/lxqt-rc.xml";
129    } else if (ffStrbufIgnCaseEqualS(dePrettyName, "LXDE")) {
130        configFileSubpath = "openbox/lxde-rc.xml";
131    }
132
133    if (!ffSearchUserConfigFile(&instance.state.platform.configDirs, configFileSubpath, &absolutePath)) {
134        ffStrbufAppendF(themeOrError, "Couldn't find config file \"%s\"", configFileSubpath);
135        return false;
136    }
137
138    FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
139    if (!ffReadFileBuffer(absolutePath.chars, &content)) {
140        ffStrbufAppendF(themeOrError, "Couldn't read \"%s\"", absolutePath.chars);
141        return false;
142    }
143
144    const char* themeStart = strstr(content.chars, "<theme>");
145    if (themeStart == nullptr) {
146        goto theme_not_found;
147    }
148
149    const char* themeEnd = strstr(themeStart, "</theme>");
150    if (__builtin_expect(themeEnd == nullptr, false)) { // very rare case
151        goto theme_not_found;
152    }
153
154    const char* nameStart = strstr(themeStart, "<name>");
155    if (nameStart == nullptr) {
156        goto name_not_found;
157    }
158
159    const char* nameEnd = strstr(nameStart, "</name>");
160    if (nameEnd == nullptr || nameEnd > themeEnd) { // (nameEnd > themeEnd) means name is not a theme's child
161        goto name_not_found;
162    }
163
164    nameStart += strlen("<name>");
165    ffStrbufAppendNS(themeOrError, (uint32_t) (nameEnd - nameStart), nameStart);
166    ffStrbufTrim(themeOrError, ' ');
167
168    if (themeOrError->length == 0) {
169        goto name_not_found;
170    }
171
172    return true;
173
174theme_not_found:
175    ffStrbufAppendF(themeOrError, "Couldn't find theme node in \"%s\"", absolutePath.chars);
176    return false;
177
178name_not_found:
179    ffStrbufAppendF(themeOrError, "Couldn't find theme name in \"%s\"", absolutePath.chars);
180    return false;
181}
182
183static bool detectCosmicComp(FFstrbuf* themeOrError) {
184    FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateCopy(FF_LIST_FIRST(FFstrbuf, instance.state.platform.configDirs));
185    ffStrbufAppendS(&path, "cosmic/");
186    uint32_t basePathLength = path.length;
187
188    const char* variant;
189    {
190        char isDarkC;
191        ffStrbufAppendS(&path, "com.system76.CosmicTheme.Mode/v1/is_dark");
192        if (ffReadFileData(path.chars, 1, &isDarkC) == 1) {
193            variant = isDarkC == 't' || isDarkC == '1' ? "Dark" : "Light";
194        } else {
195            ffStrbufAppendF(themeOrError, "Couldn't read cosmic theme mode from %s", path.chars);
196            return false;
197        }
198        ffStrbufSubstrBefore(&path, basePathLength);
199    }
200
201    FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
202    ffStrbufAppendF(&path, "com.system76.CosmicTheme.%s/v1/name", variant);
203    if (ffReadFileBuffer(path.chars, &name)) {
204        ffStrbufTrimSpace(&name);
205        ffStrbufTrim(&name, '"');
206    }
207    ffStrbufSubstrBefore(&path, basePathLength);
208
209    if (name.length > 0) {
210        ffStrbufAppend(themeOrError, &name);
211    } else {
212        ffStrbufAppendS(themeOrError, variant);
213    }
214
215    FF_STRBUF_AUTO_DESTROY accent = ffStrbufCreate();
216    ffStrbufAppendF(&path, "com.system76.CosmicTheme.%s/v1/accent", variant);
217    if (ffReadFileBuffer(path.chars, &accent)) {
218        const char* baseStart = strstr(accent.chars, "base:");
219        if (baseStart != nullptr) {
220            const char* contentStart = strchr(baseStart, '(');
221            if (contentStart != nullptr) {
222                int depth = 1;
223                const char* contentEnd = contentStart + 1;
224                while (*contentEnd != '\0' && depth > 0) {
225                    if (*contentEnd == '(') {
226                        ++depth;
227                    } else if (*contentEnd == ')') {
228                        --depth;
229                    }
230                    ++contentEnd;
231                }
232
233                if (depth == 0) {
234                    double red = 0.0, green = 0.0, blue = 0.0, alpha = 1.0;
235                    bool ok = true;
236                    for (uint32_t i = 0; i < 4; ++i) {
237                        const char* key =
238                            i == 0 ? "red:" : (i == 1 ? "green:" : (i == 2 ? "blue:" : "alpha:"));
239                        const char* componentStart = strstr(contentStart, key);
240                        if (componentStart == nullptr || componentStart >= contentEnd) {
241                            ok = false;
242                            break;
243                        }
244
245                        componentStart += strlen(key);
246                        char* componentEnd = nullptr;
247                        double value = strtod(componentStart, &componentEnd);
248                        if (componentEnd == componentStart) {
249                            ok = false;
250                            break;
251                        }
252
253                        if (i == 0) {
254                            red = value;
255                        } else if (i == 1) {
256                            green = value;
257                        } else if (i == 2) {
258                            blue = value;
259                        } else {
260                            alpha = value;
261                        }
262                    }
263
264                    if (ok) {
265                        uint32_t r = red <= 0.0 ? 0 : (red >= 1.0 ? 255 : (uint32_t) (red * 255.0 + 0.5));
266                        uint32_t g = green <= 0.0 ? 0 : (green >= 1.0 ? 255 : (uint32_t) (green * 255.0 + 0.5));
267                        uint32_t b = blue <= 0.0 ? 0 : (blue >= 1.0 ? 255 : (uint32_t) (blue * 255.0 + 0.5));
268                        uint32_t a = alpha <= 0.0 ? 0 : (alpha >= 1.0 ? 255 : (uint32_t) (alpha * 255.0 + 0.5));
269                        uint32_t rgb = (r << 16) | (g << 8) | b;
270                        if (a == 255) {
271                            ffStrbufAppendF(themeOrError, " - #%06X", rgb);
272                        } else {
273                            ffStrbufAppendF(themeOrError, " - #%06X%02X", rgb, a);
274                        }
275                    }
276                }
277            }
278        }
279    }
280    ffStrbufSubstrBefore(&path, basePathLength);
281
282    if (name.length > 0) {
283        ffStrbufAppendF(themeOrError, " (%s)", variant);
284    }
285
286    return true;
287}
288
289bool ffDetectWmTheme(FFstrbuf* themeOrError) {
290    const FFDisplayServerResult* wm = ffConnectDisplayServer();
291
292    if (wm->wmPrettyName.length == 0) {
293        ffStrbufAppendS(themeOrError, "WM Theme needs successful WM detection");
294        return false;
295    }
296
297    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_KWIN)) {
298        return detectWMThemeFromConfigFile("kwinrc", "theme =", "Breeze", themeOrError);
299    }
300
301    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_COSMIC_COMP)) {
302        return detectCosmicComp(themeOrError);
303    }
304
305    if (
306        ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_XFWM4) ||
307        (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, "labwc") && ffStrbufIgnCaseEqualS(&wm->dePrettyName, FF_DE_PRETTY_XFCE4))) {
308        return detectXFWM4(themeOrError);
309    }
310
311    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_MUTTER)) {
312        if (
313            ffStrbufIgnCaseEqualS(&wm->dePrettyName, FF_DE_PRETTY_GNOME) ||
314            ffStrbufIgnCaseEqualS(&wm->dePrettyName, FF_DE_PRETTY_GNOME_CLASSIC)) {
315            return detectMutter(themeOrError);
316        } else {
317            return detectGTKThemeAsWMTheme(themeOrError);
318        }
319    }
320
321    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_MUFFIN)) {
322        return detectMuffin(themeOrError);
323    }
324
325    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_MARCO)) {
326        return detectWMThemeFromSettings("/org/mate/Marco/general/theme", "org.mate.Marco.general", nullptr, "theme", themeOrError);
327    }
328
329    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_OPENBOX)) {
330        return detectOpenbox(&wm->dePrettyName, themeOrError);
331    }
332
333    if (ffStrbufIgnCaseEqualS(&wm->wmPrettyName, FF_WM_PRETTY_ENLIGHTENMENT)) {
334        return detectGTKThemeAsWMTheme(themeOrError);
335    }
336
337    ffStrbufAppendS(themeOrError, "Unknown WM: ");
338    ffStrbufAppend(themeOrError, &wm->wmPrettyName);
339    return false;
340}