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