Skip to content

Commit 07231ff

Browse files
Merge pull request #247 from CarterLi/master
[macOS] support cpuUsage and other improvements
2 parents fea4a9d + f9fa482 commit 07231ff

File tree

15 files changed

+207
-37
lines changed

15 files changed

+207
-37
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ endif()
256256

257257
if(LINUX OR BSD)
258258
list(APPEND LIBFASTFETCH_SRC
259+
src/detection/cpuUsage/cpuUsage_linux.c
259260
src/detection/host/host_linux.c
260261
src/detection/os/os_linux.c
261262
src/detection/gpu/gpu_linux.c
@@ -272,6 +273,7 @@ endif()
272273

273274
if(APPLE)
274275
list(APPEND LIBFASTFETCH_SRC
276+
src/detection/cpuUsage/cpuUsage_apple.c
275277
src/util/apple/cf_helpers.c
276278
src/util/apple/osascript.m
277279
src/detection/host/host_apple.c
@@ -295,6 +297,7 @@ endif()
295297

296298
if(ANDROID)
297299
list(APPEND LIBFASTFETCH_SRC
300+
src/detection/cpuUsage/cpuUsage_linux.c
298301
src/detection/host/host_android.c
299302
src/detection/os/os_android.c
300303
src/detection/gpu/gpu_android.c

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Pacman, dpkg, rpm, emerge, xbps, nix, Flatpak, Snap, apk, pkg, brew, MacPorts
7171

7272
##### WM themes
7373
```
74-
KWin, Mutter, Muffin, Marco, XFWM, Openbox (LXDE, LXQT & without DE)
74+
KWin, Mutter, Muffin, Marco, XFWM, Openbox (LXDE, LXQT & without DE), Quartz Compositor (macOS)
7575
```
7676

7777
##### DE versions
@@ -81,7 +81,7 @@ KDE Plasma, Gnome, Cinnamon, Mate, XFCE4, LXQt
8181

8282
##### Terminal fonts
8383
```
84-
Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, ITerm2, Apple Terminal, TTY
84+
Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, iTerm2, Apple Terminal, TTY
8585
```
8686

8787
## Building

src/common/processing.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
#include <unistd.h>
77
#include <sys/wait.h>
88

9-
void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
9+
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
1010
{
1111
int pipes[2];
1212

1313
if(pipe(pipes) == -1)
14-
return;
14+
return "pipe() failed";
1515

1616
pid_t childPid = fork();
1717
if(childPid == -1)
18-
return;
18+
return "fork() failed";
1919

2020
//Child
2121
if(childPid == 0)
@@ -33,4 +33,6 @@ void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
3333
waitpid(childPid, NULL, 0);
3434
ffAppendFDBuffer(pipes[0], buffer);
3535
close(pipes[0]);
36+
37+
return NULL;
3638
}

src/common/processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
#include "util/FFstrbuf.h"
77

8-
void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]);
8+
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]);
99

1010
#endif

src/detection/cpuUsage/cpuUsage.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#ifndef FF_INCLUDED_detection_cpu_cpuUsage
4+
#define FF_INCLUDED_detection_cpu_cpuUsage
5+
6+
const char* ffGetCpuUsagePercent(double* result);
7+
8+
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "fastfetch.h"
2+
#include "cpuUsage.h"
3+
4+
#include <mach/processor_info.h>
5+
#include <mach/mach_host.h>
6+
#include <unistd.h>
7+
8+
static const char* getCpuUsageInfo(long* inUseAll, long* totalAll)
9+
{
10+
natural_t numCPUs = 0U;
11+
processor_info_array_t cpuInfo;
12+
mach_msg_type_number_t numCpuInfo;
13+
14+
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS)
15+
return "host_processor_info() failed";
16+
if (numCPUs * CPU_STATE_MAX != numCpuInfo)
17+
return "Unexpected host_processor_info() result";
18+
19+
for (natural_t i = 0U; i < numCPUs; ++i) {
20+
integer_t inUse = cpuInfo[CPU_STATE_MAX * i + CPU_STATE_USER]
21+
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_SYSTEM]
22+
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_NICE];
23+
integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
24+
*inUseAll += inUse;
25+
*totalAll += total;
26+
}
27+
return NULL;
28+
}
29+
30+
const char* ffGetCpuUsagePercent(double* result)
31+
{
32+
long inUseAll1 = 0, totalAll1 = 0;
33+
const char* error = getCpuUsageInfo(&inUseAll1, &totalAll1);
34+
if(error)
35+
return error;
36+
37+
sleep(1);
38+
39+
long inUseAll2 = 0, totalAll2 = 0;
40+
error = getCpuUsageInfo(&inUseAll2, &totalAll2);
41+
if(error)
42+
return error;
43+
44+
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
45+
return NULL;
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "fastfetch.h"
2+
#include "cpuUsage.h"
3+
4+
#include <unistd.h>
5+
#include <stdio.h>
6+
7+
static const char* getCpuUsageInfo(FILE* procStat, long* inUseAll, long* totalAll)
8+
{
9+
long user, nice, system, idle, iowait, irq, softirq;
10+
11+
if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0)
12+
{
13+
fclose(procStat);
14+
return "fscanf() failed";
15+
}
16+
*inUseAll = user + nice + system;
17+
*totalAll = *inUseAll + idle + iowait + irq + softirq;
18+
return NULL;
19+
}
20+
21+
const char* ffGetCpuUsagePercent(double* result)
22+
{
23+
FILE* procStat = fopen("/proc/stat", "r");
24+
if(procStat == NULL)
25+
return "fopen(\"""/proc/stat\", \"r\") == NULL";
26+
27+
const char* error = NULL;
28+
long inUseAll1 = 0, totalAll1 = 0;
29+
error = getCpuUsageInfo(procStat, &inUseAll1, &totalAll1);
30+
if(error)
31+
goto exit;
32+
33+
sleep(1);
34+
rewind(procStat);
35+
36+
long inUseAll2 = 0, totalAll2 = 0;
37+
error = getCpuUsageInfo(procStat, &inUseAll2, &totalAll2);
38+
if(error)
39+
goto exit;
40+
41+
*result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100;
42+
43+
exit:
44+
fclose(procStat);
45+
return error;
46+
}

src/detection/memory/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void ffDetectMemoryImpl(FFMemoryResult* memory);
55

66
static void calculatePercentage(FFMemoryStorage* storage)
77
{
8-
if(storage->error.length == 0)
8+
if(storage->error.length != 0)
99
return;
1010

1111
if(storage->bytesTotal == 0)

src/detection/os/os_apple.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "common/sysctl.h"
44

55
#include <stdlib.h>
6+
#include <string.h>
67

78
typedef enum PListKey
89
{
@@ -12,7 +13,7 @@ typedef enum PListKey
1213
PLIST_KEY_OTHER
1314
} PListKey;
1415

15-
static void parseFile(FFOSResult* os)
16+
static void parseSystemVersion(FFOSResult* os)
1617
{
1718
FILE* plist = fopen("/System/Library/CoreServices/SystemVersion.plist", "r");
1819
if(plist == NULL)
@@ -58,6 +59,33 @@ static void parseFile(FFOSResult* os)
5859
fclose(plist);
5960
}
6061

62+
void parseOSXSoftwareLicense(FFOSResult* os)
63+
{
64+
FILE* rtf = fopen("/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf", "r");
65+
if(rtf == NULL)
66+
return;
67+
68+
char* line = NULL;
69+
size_t len = 0;
70+
const char* searchStr = "\\f0\\b SOFTWARE LICENSE AGREEMENT FOR macOS ";
71+
const size_t searchLen = strlen(searchStr);
72+
while(getline(&line, &len, rtf) != EOF)
73+
{
74+
if (strncmp(line, searchStr, searchLen) == 0)
75+
{
76+
ffStrbufAppendS(&os->codename, line + searchLen);
77+
ffStrbufTrimRight(&os->codename, '\n');
78+
ffStrbufTrimRight(&os->codename, '\\');
79+
break;
80+
}
81+
}
82+
83+
if(line != NULL)
84+
free(line);
85+
86+
fclose(rtf);
87+
}
88+
6189
void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance)
6290
{
6391
FF_UNUSED(instance);
@@ -76,17 +104,21 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance)
76104
ffStrbufInitA(&os->variant, 0);
77105
ffStrbufInitA(&os->variantID, 0);
78106

79-
parseFile(os);
107+
parseSystemVersion(os);
80108

81109
if(ffStrbufStartsWithIgnCaseS(&os->name, "MacOS"))
82110
ffStrbufAppendS(&os->id, "macos");
83111

84112
if(os->version.length == 0)
85113
ffSysctlGetString("kern.osproductversion", &os->version);
86114

87-
//TODO map version to pretty name
115+
if(os->buildID.length == 0)
116+
ffSysctlGetString("kern.osversion", &os->buildID);
117+
88118
ffStrbufAppend(&os->prettyName, &os->name);
89119
ffStrbufAppend(&os->versionID, &os->version);
90120
ffSysctlGetString("kern.ostype", &os->systemName);
91121
ffSysctlGetString("hw.machine", &os->architecture);
122+
123+
parseOSXSoftwareLicense(os);
92124
}

src/detection/terminalShell.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ static void getTerminalFromEnv(FFTerminalShellResult* result)
130130
getenv("WT_PROFILE_ID") != NULL
131131
)) term = "Windows Terminal";
132132

133+
//Alacritty
134+
if(!ffStrSet(term) && (
135+
getenv("ALACRITTY_SOCKET") != NULL ||
136+
getenv("ALACRITTY_LOG") != NULL ||
137+
getenv("ALACRITTY_WINDOW_ID") != NULL
138+
)) term = "Alacritty";
139+
133140
//Termux
134141
if(!ffStrSet(term) && (
135142
getenv("TERMUX_VERSION") != NULL ||

0 commit comments

Comments
 (0)