Skip to content

Commit 0749c1d

Browse files
committed
core-modules: macos: init some modules support
1 parent 8cf19c2 commit 0749c1d

5 files changed

Lines changed: 205 additions & 14 deletions

File tree

include/core-modules.hh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <pwd.h>
44
#include <sys/utsname.h>
55

6-
#include "platform.hpp"
76
#include "cufetch/cufetch.hh"
87
#include "config.hpp"
98

@@ -17,6 +16,7 @@ MODFUNC(host_version);
1716
MODFUNC(host_vendor);
1817

1918
// os.cc
19+
inline utsname g_uname_infos;
2020
inline std::FILE* os_release;
2121
MODFUNC(os_name);
2222
MODFUNC(os_pretty_name);
@@ -43,6 +43,7 @@ MODFUNC(android_cpu_vendor);
4343
MODFUNC(android_cpu_model_name);
4444

4545
// user.cc
46+
inline struct passwd* g_pwd;
4647
inline bool is_tty = false;
4748
inline std::string term_pid, term_name, wm_name, de_name, wm_path_exec;
4849
std::string get_terminal_name();
@@ -114,10 +115,5 @@ MODFUNC(theme_gsettings_font);
114115
MODFUNC(theme_gsettings_cursor_name);
115116
MODFUNC(theme_gsettings_cursor_size);
116117

117-
#if CF_LINUX
118-
inline struct passwd* g_pwd;
119-
inline utsname g_uname_infos;
120-
#endif
121-
122118
void core_plugins_start(const Config& config);
123119
void core_plugins_finish();

src/core-modules/android/battery.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ MODFUNC(battery_status)
8080
if (!assert_doc())
8181
return read_value_dumpsys("powered", true);
8282

83+
std::string charge_status{ str_tolower(doc["status"].as_string()) };
84+
charge_status.at(0) = toupper(charge_status.at(0));
8385
switch (fnv1a16::hash(doc["plugged"].as_string()))
8486
{
85-
case "PLUGGED_AC"_fnv1a16: return "AC Connected, " + str_tolower(doc["status"].as_string());
86-
case "PLUGGED_USB"_fnv1a16: return "USB Connected, " + str_tolower(doc["status"].as_string());
87-
case "PLUGGED_WIRELESS"_fnv1a16: return "Wireless Connected, "+ str_tolower(doc["status"].as_string());
88-
default: return "Discharging";
87+
case "PLUGGED_AC"_fnv1a16: return "AC Connected, " + charge_status;
88+
case "PLUGGED_USB"_fnv1a16: return "USB Connected, " + charge_status;
89+
case "PLUGGED_WIRELESS"_fnv1a16: return "Wireless Connected, "+ charge_status;
90+
default: return charge_status;
8991
}
9092
}
9193

src/core-modules/core-modules.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,11 @@ void core_plugins_start(const Config& config)
165165
const size_t uptime_hours = uptime_secs / (60 * 60);
166166
const size_t uptime_days = uptime_secs / (60 * 60 * 24);
167167

168-
#if CF_LINUX || CF_ANDROID
169168
if (uname(&g_uname_infos) != 0)
170169
die(_("uname() failed: {}\nCould not get system infos"), std::strerror(errno));
171-
#endif
172170

173-
#if CF_LINUX
174171
if (g_pwd = getpwuid(getuid()), !g_pwd)
175172
die(_("getpwent failed: {}\nCould not get user infos"), std::strerror(errno));
176-
#endif
177173

178174
term_pid = get_terminal_pid();
179175
term_name = get_terminal_name();

src/core-modules/macos/cpu.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "platform.hpp"
2+
#if CF_MACOS
3+
4+
#include <sys/sysctl.h>
5+
#include <unistd.h>
6+
7+
#include <cstdint>
8+
#include <string>
9+
#include <ratio>
10+
#include <string>
11+
12+
#include "core-modules.hh"
13+
14+
static bool get_sysctl(int name[2], void* ret, size_t* oldlenp)
15+
{
16+
return (sysctl(name, 2, ret, oldlenp, NULL, 0) == 0);
17+
}
18+
19+
static bool get_sysctl(const char* name, void* ret, size_t* oldlenp)
20+
{
21+
return (sysctlbyname(name, ret, oldlenp, NULL, 0) == 0);
22+
}
23+
24+
MODFUNC(cpu_name)
25+
{
26+
char buf[1024];
27+
size_t len = sizeof(buf);
28+
get_sysctl("machdep.cpu.brand_string", &buf, &len);
29+
return buf;
30+
}
31+
32+
MODFUNC(cpu_nproc)
33+
{
34+
char buf[1024];
35+
size_t len = sizeof(buf);
36+
if (!get_sysctl("hw.logicalcpu_max", &buf, &len))
37+
get_sysctl("hw.ncpu", &buf, &len);
38+
return buf;
39+
}
40+
41+
MODFUNC(cpu_freq_cur)
42+
{
43+
std::uint64_t freq = 0;
44+
size_t length = sizeof(freq);
45+
if (!get_sysctl("hw.cpufrequency", &freq, &length))
46+
get_sysctl({ CTL_HW, HW_CPU_FREQ }, &freq, &length);
47+
48+
return fmt::to_string(static_cast<double>(freq) / std::giga().num);
49+
}
50+
51+
MODFUNC(cpu_freq_min)
52+
{
53+
std::uint64_t freq = 0;
54+
size_t length = sizeof(freq);
55+
get_sysctl("hw.cpufrequency_min", &freq, &length);
56+
57+
return fmt::to_string(static_cast<double>(freq) / std::giga().num);
58+
}
59+
60+
MODFUNC(cpu_freq_max)
61+
{
62+
std::uint64_t freq = 0;
63+
size_t length = sizeof(freq);
64+
get_sysctl("hw.cpufrequency_max", &freq, &length);
65+
66+
return fmt::to_string(static_cast<double>(freq) / std::giga().num);
67+
}
68+
69+
MODFUNC(cpu_freq_bios)
70+
{ return MAGIC_LINE; }
71+
72+
#endif

src/core-modules/macos/os.cc

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <fstream>
2+
#include <string>
3+
#include "core-modules.hh"
4+
#include "cufetch/common.hh"
5+
#include "rapidxml-1.13/rapidxml.hpp"
6+
#include "switch_fnv1a.hpp"
7+
8+
std::ifstream f("/System/Library/CoreServices/SystemVersion.plist", std::ios::in);
9+
std::string buffer(std::istreambuf_iterator<char>{ f }, std::istreambuf_iterator<char>{});
10+
rapidxml::xml_document<> doc;
11+
12+
static std::string get_codename(const std::string_view os_version_id)
13+
{
14+
std::string major;
15+
std::string minor{ UNKNOWN };
16+
size_t pos1 = os_version_id.find('.');
17+
if (pos1 != os_version_id.npos)
18+
{
19+
major = os_version_id.substr(0, pos1);
20+
size_t pos2 = os_version_id.find('.', pos1 + 1);
21+
if (pos2 != os_version_id.npos)
22+
minor = os_version_id.substr(pos1 + 1, pos2);
23+
}
24+
25+
switch (fnv1a16::hash(major))
26+
{
27+
case "15"_fnv1a16: return "Sequoia";
28+
case "14"_fnv1a16: return "Sonoma";
29+
case "13"_fnv1a16: return "Ventura";
30+
case "12"_fnv1a16: return "Monterey";
31+
case "11"_fnv1a16: return "Big Sur";
32+
case "10"_fnv1a16:
33+
{
34+
switch (fnv1a16::hash(minor))
35+
{
36+
case "16"_fnv1a16: return "Big Sur";
37+
case "15"_fnv1a16: return "Catalina";
38+
case "14"_fnv1a16: return "Mojave";
39+
case "13"_fnv1a16: return "High Sierra";
40+
case "12"_fnv1a16: return "Sierra";
41+
case "11"_fnv1a16: return "El Capitan";
42+
case "10"_fnv1a16: return "Yosemite";
43+
case "9"_fnv1a16: return "Mavericks";
44+
case "8"_fnv1a16: return "Mountain Lion";
45+
case "7"_fnv1a16: return "Lion";
46+
case "6"_fnv1a16: return "Snow Leopard";
47+
case "5"_fnv1a16: return "Leopard";
48+
case "4"_fnv1a16: return "Tiger";
49+
case "3"_fnv1a16: return "Panther";
50+
case "2"_fnv1a16: return "Jaguar";
51+
case "1"_fnv1a16: return "Puma";
52+
case "0"_fnv1a16: return "Cheetah";
53+
}
54+
}
55+
}
56+
57+
return UNKNOWN;
58+
}
59+
60+
static void assert_doc()
61+
{
62+
if (!doc.first_node("plist"))
63+
{
64+
buffer.push_back('\0');
65+
doc.parse<0>(&buffer[0]);
66+
}
67+
}
68+
69+
static std::string get_plist_value(const std::string_view name)
70+
{
71+
assert_doc();
72+
rapidxml::xml_node<>* root_node = doc.first_node("plist")->first_node("dict")->first_node("key");
73+
for (; root_node; root_node = root_node->next_sibling())
74+
{
75+
const std::string_view key = root_node->value(); // <key>ProductName</key>
76+
root_node = root_node->next_sibling();
77+
const std::string_view value = root_node->value(); // <string>macOS</string>
78+
if (key == name)
79+
return value.data();
80+
}
81+
return UNKNOWN;
82+
}
83+
84+
MODFUNC(os_name)
85+
{ return get_plist_value("ProductName"); }
86+
87+
MODFUNC(os_version_id)
88+
{ return get_plist_value("ProductUserVisibleVersion"); }
89+
90+
MODFUNC(os_version_codename)
91+
{ return get_codename(os_version_id(nullptr)); }
92+
93+
MODFUNC(os_pretty_name)
94+
{
95+
const std::string& codename = os_version_codename(nullptr);
96+
if (codename != UNKNOWN)
97+
return os_name(nullptr) + " " + os_version_id(nullptr) + " (" + codename + ")";
98+
return os_name(nullptr) + " " + os_version_id(nullptr);
99+
}
100+
101+
MODFUNC(os_kernel_name)
102+
{ return g_uname_infos.sysname; }
103+
104+
MODFUNC(os_kernel_version)
105+
{ return g_uname_infos.release; }
106+
107+
MODFUNC(os_hostname)
108+
{ return g_uname_infos.nodename; }
109+
110+
MODFUNC(os_initsys_name)
111+
{ return MAGIC_LINE; }
112+
113+
MODFUNC(os_initsys_version)
114+
{ return UNKNOWN; }
115+
116+
unsigned long os_uptime()
117+
{
118+
struct timeval boot_time;
119+
size_t size = sizeof(boot_time);
120+
int name[] = { CTL_KERN, KERN_BOOTTIME };
121+
if (sysctl(name, 2, &boot_time, &size, NULL, 0) != 0)
122+
die(_("failed to get uptime"));
123+
124+
return time(NULL) - boot_time.tv_sec;
125+
}

0 commit comments

Comments
 (0)