Skip to content

Commit 183c8d7

Browse files
Merge pull request #246 from CarterLi/master
WMTheme: support macOS
2 parents 01a9179 + e5b1103 commit 183c8d7

File tree

8 files changed

+250
-26
lines changed

8 files changed

+250
-26
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,16 @@ if (BUILD_TESTS)
413413
PRIVATE libfastfetch
414414
)
415415

416+
add_executable(fastfetch-test-list
417+
tests/list.c
418+
)
419+
target_link_libraries(fastfetch-test-list
420+
PRIVATE libfastfetch
421+
)
422+
416423
enable_testing()
417424
add_test(NAME test-strbuf COMMAND fastfetch-test-strbuf)
425+
add_test(NAME test-list COMMAND fastfetch-test-list)
418426
endif()
419427

420428
##################

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/FFlist.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compF
4646

4747
void ffListDestroy(FFlist* list)
4848
{
49-
if(list->data != NULL)
50-
free(list->data);
49+
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
50+
list->capacity = list->length = 0;
51+
free(list->data);
52+
list->data = NULL;
5153
}

src/util/FFstrbuf.c

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

32+
void ffStrbufInitS(FFstrbuf* strbuf, const char* str)
33+
{
34+
ffStrbufInitA(strbuf, 0);
35+
ffStrbufAppendS(strbuf, str);
36+
}
37+
3238
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf)
3339
{
3440
if(strbuf->allocated == 0)
@@ -94,14 +100,7 @@ void ffStrbufAppendS(FFstrbuf* strbuf, const char* value)
94100
if(value == NULL)
95101
return;
96102

97-
for(uint32_t i = 0; value[i] != '\0'; i++)
98-
{
99-
if(i % 16 == 0)
100-
ffStrbufEnsureFree(strbuf, 16);
101-
strbuf->chars[strbuf->length++] = value[i];
102-
}
103-
104-
strbuf->chars[strbuf->length] = '\0';
103+
ffStrbufAppendNS(strbuf, (uint32_t)strlen(value), value);
105104
}
106105

107106
void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
@@ -110,15 +109,8 @@ void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
110109
return;
111110

112111
ffStrbufEnsureFree(strbuf, length);
113-
114-
for(uint32_t i = 0; i < length; i++)
115-
{
116-
if(value[i] == '\0')
117-
break;
118-
119-
strbuf->chars[strbuf->length++] = value[i];
120-
}
121-
112+
memcpy(&strbuf->chars[strbuf->length], value, length);
113+
strbuf->length += length;
122114
strbuf->chars[strbuf->length] = '\0';
123115
}
124116

@@ -572,5 +564,8 @@ uint16_t ffStrbufToUInt16(const FFstrbuf* strbuf, uint16_t defaultValue)
572564

573565
void ffStrbufDestroy(FFstrbuf* strbuf)
574566
{
567+
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
568+
strbuf->allocated = strbuf->length = 0;
575569
free(strbuf->chars);
570+
strbuf->chars = NULL;
576571
}

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

tests/list.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "fastfetch.h"
2+
3+
#include <string.h>
4+
#include <stdarg.h>
5+
#include <stdlib.h>
6+
7+
static void testFailed(const FFlist* list, const char* message, ...)
8+
{
9+
va_list args;
10+
va_start(args, message);
11+
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr);
12+
vfprintf(stderr, message, args);
13+
for (uint32_t i = 0; i < list->length; ++i)
14+
{
15+
fprintf(stderr, "%u ", *(uint32_t*)ffListGet(list, i));
16+
}
17+
fputc('\n', stderr);
18+
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr);
19+
fputc('\n', stderr);
20+
va_end(args);
21+
exit(1);
22+
}
23+
24+
int main(int argc, char** argv)
25+
{
26+
FF_UNUSED(argc, argv)
27+
28+
FFlist list;
29+
30+
//initA
31+
32+
ffListInitA(&list, sizeof(uint32_t), 0);
33+
34+
if(ffListGet(&list, 0) != NULL)
35+
testFailed(&list, "ffListGet(&list, 0) != NULL");
36+
37+
if(list.elementSize != sizeof(int))
38+
testFailed(&list, "list.elementSize != sizeof(int)");
39+
40+
if(list.capacity != 0)
41+
testFailed(&list, "list.capacity != 0");
42+
43+
if(list.length != 0)
44+
testFailed(&list, "list.length != 0");
45+
46+
//add
47+
for (uint32_t i = 1; i <= FF_LIST_DEFAULT_ALLOC + 1; ++i)
48+
{
49+
*(uint32_t*)ffListAdd(&list) = i;
50+
51+
if(list.elementSize != sizeof(uint32_t))
52+
testFailed(&list, "list.elementSize != sizeof(uint32_t)");
53+
54+
if(list.length != i)
55+
testFailed(&list, "list.length != i");
56+
57+
if(i <= FF_LIST_DEFAULT_ALLOC)
58+
{
59+
if(list.capacity != FF_LIST_DEFAULT_ALLOC)
60+
testFailed(&list, "list.length != FF_LIST_DEFAULT_ALLOC");
61+
}
62+
else
63+
{
64+
if(list.capacity != FF_LIST_DEFAULT_ALLOC * 2)
65+
testFailed(&list, "list.length != FF_LIST_DEFAULT_ALLOC * 2");
66+
}
67+
68+
if(*(uint32_t*)ffListGet(&list, 0) != 1)
69+
testFailed(&list, "*(int*)ffListGet(&list, 0) != 1");
70+
71+
if(*(uint32_t*)ffListGet(&list, i - 1) != i)
72+
testFailed(&list, "*(int*)ffListGet(&list, i - 1) != i");
73+
}
74+
75+
//Destroy
76+
ffListDestroy(&list);
77+
78+
if(list.elementSize != sizeof(uint32_t))
79+
testFailed(&list, "list.elementSize != sizeof(uint32_t)");
80+
81+
if(list.capacity != 0)
82+
testFailed(&list, "list.capacity != 0");
83+
84+
if(list.length != 0)
85+
testFailed(&list, "list.length != 0");
86+
87+
//Success
88+
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
89+
}

tests/strbuf.c

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,58 @@ int main(int argc, char** argv)
2626

2727
//initA
2828

29-
ffStrbufInitA(&strbuf, 64);
29+
ffStrbufInitA(&strbuf, 0);
3030

31-
if(strbuf.allocated != 64)
32-
testFailed(&strbuf, "strbuf.allocated != 64");
31+
if(strbuf.chars[0] != 0) //make sure chars[0] is accessable
32+
testFailed(&strbuf, "strbuf.chars[0] != 0");
33+
34+
if(strbuf.allocated != 0)
35+
testFailed(&strbuf, "strbuf.allocated != 0");
3336

3437
if(strbuf.length != 0)
35-
testFailed(&strbuf, "testSrbuf.length != 0");
38+
testFailed(&strbuf, "strbuf.length != 0");
3639

3740
//appendS
3841

39-
ffStrbufAppendS(&strbuf, "123456789");
42+
ffStrbufAppendS(&strbuf, "12345");
43+
44+
if(strbuf.length != 5)
45+
testFailed(&strbuf, "strbuf.length != 5");
46+
47+
if(strbuf.allocated < 6)
48+
testFailed(&strbuf, "strbuf.allocated < 6");
49+
50+
if(ffStrbufCompS(&strbuf, "12345") != 0)
51+
testFailed(&strbuf, "strbuf.data != \"12345\"");
52+
53+
//appendNS
54+
55+
ffStrbufAppendNS(&strbuf, 4, "67890");
4056

4157
if(strbuf.length != 9)
4258
testFailed(&strbuf, "strbuf.length != 9");
4359

44-
if(strcmp(strbuf.chars, "123456789") != 0)
60+
if(strbuf.allocated < 10)
61+
testFailed(&strbuf, "strbuf.allocated < 10");
62+
63+
if(ffStrbufCompS(&strbuf, "123456789") != 0)
4564
testFailed(&strbuf, "strbuf.data != \"123456789\"");
4665

66+
//appendS long
67+
68+
ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
69+
70+
if(strbuf.length != 109)
71+
testFailed(&strbuf, "strbuf.length != 109");
72+
73+
if(strbuf.allocated < 110)
74+
testFailed(&strbuf, "strbuf.allocated < 110");
75+
76+
if(strbuf.chars[strbuf.length] != 0)
77+
testFailed(&strbuf, "strbuf.chars[strbuf.length] != 0");
78+
79+
strbuf.length = 9;
80+
4781
//startsWithS
4882

4983
if(!ffStrbufStartsWithS(&strbuf, "123"))
@@ -79,6 +113,18 @@ int main(int argc, char** argv)
79113
if(strcmp(strbuf.chars, "12316") != 0)
80114
testFailed(&strbuf, "strbuf.chars != \"12316\"");
81115

116+
//Destroy
117+
118+
ffStrbufDestroy(&strbuf);
119+
if(strbuf.allocated != 0)
120+
testFailed(&strbuf, "strbuf.allocated != 0");
121+
122+
if(strbuf.length != 0)
123+
testFailed(&strbuf, "strbuf.length != 0");
124+
125+
if(strbuf.chars != NULL)
126+
testFailed(&strbuf, "strbuf.chars != NULL");
127+
82128
//Success
83129
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
84130
}

0 commit comments

Comments
 (0)