Skip to content

Commit 254c4e5

Browse files
committed
WMTheme: support macOS
1 parent 01a9179 commit 254c4e5

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The following libraries are used if present at runtime:
4444
* [`libXFConf`](https://gitlab.xfce.org/xfce/xfconf): Needed for XFWM theme and XFCE Terminal font.
4545
* [`libsqlite3`](https://www.sqlite.org/index.html): Needed for pkg & rpm package count.
4646
* [`librpm`](http://rpm.org/): Slower fallback for rpm package count. Needed on openSUSE.
47-
* [`libplist`](https://github.com/libimobiledevice/libplist): Binary `plist` file parser. Needed for iTerm2 Terminal font.
47+
* [`libplist`](https://github.com/libimobiledevice/libplist): Binary `plist` file parser ( macOS ). Needed for iTerm2 Terminal font and WM Theme.
4848

4949
## Support status
5050
All categories not listed here should work without needing a specific implementation.

src/modules/wmtheme.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,87 @@ static void printOpenbox(FFinstance* instance, const FFstrbuf* dePrettyName)
229229
ffStrbufDestroy(&absolutePath);
230230
}
231231

232+
#ifdef FF_HAVE_LIBPLIST
233+
#include "common/library.h"
234+
#include "common/io.h"
235+
#include <plist/plist.h>
236+
237+
static const char* quartzCompositorParsePlist(FFinstance* instance, const FFstrbuf* content, FFstrbuf* theme) {
238+
FF_LIBRARY_LOAD(libplist, &instance->config.libplist, "dlopen libplist failed", "libplist-2.0"FF_LIBRARY_EXTENSION, 2);
239+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_is_binary);
240+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_from_bin);
241+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_from_xml);
242+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_dict_get_item);
243+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_get_string_ptr);
244+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_get_uint_val);
245+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libplist, plist_free);
246+
247+
plist_t root_node = NULL;
248+
if (ffplist_is_binary(content->chars, content->length))
249+
ffplist_from_bin(content->chars, content->length, &root_node);
250+
else
251+
ffplist_from_xml(content->chars, content->length, &root_node);
252+
253+
if(root_node == NULL)
254+
return "parsing Quartz Compositor preference file failed";
255+
256+
plist_t pWmThemeColor = ffplist_dict_get_item(root_node, "AppleAccentColor");
257+
uint64_t wmThemeColor = 100;
258+
if (pWmThemeColor != NULL)
259+
ffplist_get_uint_val(pWmThemeColor, &wmThemeColor);
260+
switch (wmThemeColor)
261+
{
262+
case (uint64_t)-1: ffStrbufAppendS(theme, " (Graphite)");
263+
case 0: ffStrbufAppendS(theme, "Red"); break;
264+
case 1: ffStrbufAppendS(theme, "Orange"); break;
265+
case 2: ffStrbufAppendS(theme, "Yellow"); break;
266+
case 3: ffStrbufAppendS(theme, "Green"); break;
267+
case 5: ffStrbufAppendS(theme, "Purple"); break;
268+
case 6: ffStrbufAppendS(theme, "Pink"); break;
269+
default: ffStrbufAppendS(theme, "Blue"); break;
270+
}
271+
272+
plist_t pWmTheme = ffplist_dict_get_item(root_node, "AppleInterfaceStyle");
273+
ffStrbufAppendF(theme, " (%s)", pWmTheme != NULL ? ffplist_get_string_ptr(pWmTheme, NULL) : "Light");
274+
275+
ffplist_free(root_node);
276+
return NULL;
277+
}
278+
279+
static void printQuartzCompositor(FFinstance* instance) {
280+
FFstrbuf fileName;
281+
ffStrbufInitS(&fileName, instance->state.passwd->pw_dir);
282+
ffStrbufAppendS(&fileName, "/Library/Preferences/.GlobalPreferences.plist");
283+
284+
FFstrbuf content;
285+
ffStrbufInit(&content);
286+
ffAppendFileBuffer(fileName.chars, &content);
287+
ffStrbufDestroy(&fileName);
288+
if(content.length == 0)
289+
{
290+
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmTheme, "reading Quartz Compositor preference file failed");
291+
ffStrbufDestroy(&content);
292+
return;
293+
}
294+
295+
FFstrbuf theme;
296+
ffStrbufInit(&theme);
297+
const char* error = quartzCompositorParsePlist(instance, &content, &theme);
298+
ffStrbufDestroy(&content);
299+
300+
if(error != NULL)
301+
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmTheme, "%s", error);
302+
else
303+
printWMTheme(instance, theme.chars);
304+
}
305+
#else
306+
static void printQuartzCompositor(FFinstance* instance)
307+
{
308+
FF_UNUSED(instance);
309+
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmTheme, "Fastfetch was compiled without libplist support");
310+
}
311+
#endif
312+
232313
void ffPrintWMTheme(FFinstance* instance)
233314
{
234315
#ifdef __ANDROID__
@@ -261,6 +342,8 @@ void ffPrintWMTheme(FFinstance* instance)
261342
printWMThemeFromSettings(instance, "/org/mate/Marco/general/theme", "org.mate.Marco.general", NULL, "theme");
262343
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Openbox") == 0)
263344
printOpenbox(instance, &result->dePrettyName);
345+
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Quartz Compositor") == 0)
346+
printQuartzCompositor(instance);
264347
else
265348
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmTheme, "Unknown WM: %s", result->wmPrettyName.chars);
266349
}

src/util/FFstrbuf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src)
2929
ffStrbufAppend(strbuf, src);
3030
}
3131

32+
void ffStrbufInitS(FFstrbuf* strbuf, const char* str)
33+
{
34+
uint32_t bufSize = (uint32_t)strlen(str) + 1;
35+
ffStrbufInitA(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC > bufSize ? FASTFETCH_STRBUF_DEFAULT_ALLOC : bufSize);
36+
memcpy(strbuf->chars, str, bufSize);
37+
strbuf->length = bufSize - 1;
38+
}
39+
3240
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf)
3341
{
3442
if(strbuf->allocated == 0)

src/util/FFstrbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct FFstrbuf
2424
void ffStrbufInit(FFstrbuf* strbuf);
2525
void ffStrbufInitA(FFstrbuf* strbuf, uint32_t allocate);
2626
void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src);
27+
void ffStrbufInitS(FFstrbuf* strbuf, const char* str);
2728

2829
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free);
2930

0 commit comments

Comments
 (0)