From e7a16d005c1a880e443fcc7a7cdbeab1744f3445 Mon Sep 17 00:00:00 2001 From: Mars Date: Mon, 28 Apr 2025 11:59:19 -0400 Subject: [PATCH] macos --- src/main.cpp | 5 ++++- src/os/macos.cpp | 50 +++++++++++++++++++++++++---------------- src/os/macos/bridge.hpp | 28 +++++++++++------------ src/os/macos/bridge.mm | 26 ++++++++++++--------- 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 115b184..6ff01f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -267,7 +267,10 @@ fn main() -> i32 { const Config& config = Config::getInstance(); const SystemData data = SystemData::fetchSystemData(config); - debug_log("{}", *os::GetPackageCount()); + if (const Result& packageCount = os::GetPackageCount()) + debug_log("{}", *packageCount); + else + debug_at(packageCount.error()); Element document = vbox({ hbox({ SystemInfoBox(config, data), filler() }), text("") }); diff --git a/src/os/macos.cpp b/src/os/macos.cpp index 3f74159..8079674 100644 --- a/src/os/macos.cpp +++ b/src/os/macos.cpp @@ -1,48 +1,53 @@ #ifdef __APPLE__ +// clang-format off #include -#include #include #include -#include "macos/bridge.h" -#include "os.h" -#include "src/util/types.h" +#include "macos/bridge.hpp" +#include "os.hpp" +#include "src/core/util/defs.hpp" +#include "src/core/util/error.hpp" +#include "src/core/util/types.hpp" +// clang-format on -fn os::GetMemInfo() -> Result { +using namespace util::types; + +fn os::GetMemInfo() -> Result { u64 mem = 0; usize size = sizeof(mem); if (sysctlbyname("hw.memsize", &mem, &size, nullptr, 0) == -1) - return Err(OsError::withErrno("Failed to get memory info")); + return Err(DraconisError::withErrno("Failed to get memory info")); return mem; } -fn os::GetNowPlaying() -> Result { return GetCurrentPlayingInfo(); } +fn os::GetNowPlaying() -> Result { return GetCurrentPlayingInfo(); } -fn os::GetOSVersion() -> Result { return GetMacOSVersion(); } +fn os::GetOSVersion() -> Result { return GetMacOSVersion(); } fn os::GetDesktopEnvironment() -> Option { return None; } fn os::GetWindowManager() -> Option { return None; } -fn os::GetKernelVersion() -> Result { +fn os::GetKernelVersion() -> Result { std::array kernelVersion {}; usize kernelVersionLen = sizeof(kernelVersion); if (sysctlbyname("kern.osrelease", kernelVersion.data(), &kernelVersionLen, nullptr, 0) == -1) - return Err(OsError::withErrno("Failed to get kernel version")); + return Err(DraconisError::withErrno("Failed to get kernel version")); return kernelVersion.data(); } -fn os::GetHost() -> Result { +fn os::GetHost() -> Result { std::array hwModel {}; size_t hwModelLen = sizeof(hwModel); if (sysctlbyname("hw.model", hwModel.data(), &hwModelLen, nullptr, 0) == -1) - return Err(OsError::withErrno("Failed to get host info")); + return Err(DraconisError::withErrno("Failed to get host info")); // taken from https://github.com/fastfetch-cli/fastfetch/blob/dev/src/detection/host/host_mac.c // shortened a lot of the entries to remove unnecessary info @@ -195,20 +200,27 @@ fn os::GetHost() -> Result { { "iMac9,1", "iMac (24/20-inch, 2009)" }, }; - const auto it = modelNameByHwModel.find(hwModel.data()); - if (it == modelNameByHwModel.end()) - return Err(OsError::withErrno("Failed to get host info")); + const auto iter = modelNameByHwModel.find(hwModel.data()); + if (iter == modelNameByHwModel.end()) + return Err(DraconisError::withErrno("Failed to get host info")); - return String(it->second); + return String(iter->second); } -fn os::GetDiskUsage() -> Result { +fn os::GetDiskUsage() -> Result { struct statvfs vfs; if (statvfs("/", &vfs) != 0) - return Err(OsError::withErrno("Failed to get disk usage")); + return Err(DraconisError::withErrno("Failed to get disk usage")); - return DiskSpace { .used_bytes=(vfs.f_blocks - vfs.f_bfree) * vfs.f_frsize, .total_bytes=vfs.f_blocks * vfs.f_frsize }; + return DiskSpace { .used_bytes = (vfs.f_blocks - vfs.f_bfree) * vfs.f_frsize, + .total_bytes = vfs.f_blocks * vfs.f_frsize }; +} + +fn os::GetPackageCount() -> Result { + using util::error::DraconisErrorCode; + + return Err(DraconisError(DraconisErrorCode::Other, "Not implemented on macOS")); } fn os::GetShell() -> Option { return None; } diff --git a/src/os/macos/bridge.hpp b/src/os/macos/bridge.hpp index ffb4bfe..713bb7f 100644 --- a/src/os/macos/bridge.hpp +++ b/src/os/macos/bridge.hpp @@ -2,25 +2,25 @@ #ifdef __APPLE__ -#include +// clang-format off +#include "src/core/util/defs.hpp" +#include "src/core/util/error.hpp" +#include "src/core/util/types.hpp" +// clang-format on +using util::error::DraconisError; +using util::types::MediaInfo, util::types::String, util::types::Result; -#include "../../util/macros.h" - -#ifdef __OBJC__ - -#import + #ifdef __OBJC__ + #import @interface Bridge : NSObject + (void)fetchCurrentPlayingMetadata:(void (^)(Result))completion; -+ (Result)macOSVersion; ++ (Result)macOSVersion; @end - -#else - + #else extern "C++" { - fn GetCurrentPlayingInfo() -> Result; - fn GetMacOSVersion() -> Result; + fn GetCurrentPlayingInfo() -> Result; + fn GetMacOSVersion() -> Result; } - -#endif + #endif #endif diff --git a/src/os/macos/bridge.mm b/src/os/macos/bridge.mm index f3bbd4b..366c0e3 100644 --- a/src/os/macos/bridge.mm +++ b/src/os/macos/bridge.mm @@ -1,5 +1,8 @@ #ifdef __APPLE__ +// clang-format off +#import "bridge.hpp" + #import #include #include @@ -8,7 +11,11 @@ #include #include -#import "bridge.h" +#include "src/core/util/error.hpp" +// clang-format on + +using util::error::DraconisErrorCode; +using util::types::Err, util::types::Option; using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(dispatch_queue_t queue, void (^handler)(NSDictionary* information)); @@ -59,7 +66,7 @@ using MRMediaRemoteGetNowPlayingInfoFunction = ); } -+ (Result)macOSVersion { ++ (Result)macOSVersion { NSProcessInfo* processInfo = [NSProcessInfo processInfo]; NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion]; @@ -84,21 +91,21 @@ using MRMediaRemoteGetNowPlayingInfoFunction = extern "C++" { // NOLINTBEGIN(misc-use-internal-linkage) - fn GetCurrentPlayingInfo() -> Result { - __block Result result; + fn GetCurrentPlayingInfo() -> Result { + __block Result result; - dispatch_semaphore_t const semaphore = dispatch_semaphore_create(0); + const dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [Bridge fetchCurrentPlayingMetadata:^(std::expected metadataResult) { if (!metadataResult) { - result = Err(OsError { OsErrorCode::InternalError, metadataResult.error() }); + result = Err(DraconisError(DraconisErrorCode::InternalError, metadataResult.error())); dispatch_semaphore_signal(semaphore); return; } const NSDictionary* const metadata = *metadataResult; if (!metadata) { - result = Err(OsError { OsErrorCode::InternalError, "No metadata" }); + result = Err(DraconisError(DraconisErrorCode::InternalError, "No metadata")); dispatch_semaphore_signal(semaphore); return; } @@ -107,8 +114,7 @@ extern "C++" { const NSString* const artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"]; result = MediaInfo( - title ? Option(String([title UTF8String])) : None, - artist ? Option(String([artist UTF8String])) : None + title ? Option(String([title UTF8String])) : None, artist ? Option(String([artist UTF8String])) : None ); dispatch_semaphore_signal(semaphore); @@ -118,7 +124,7 @@ extern "C++" { return result; } - fn GetMacOSVersion() -> Result { return [Bridge macOSVersion]; } + fn GetMacOSVersion() -> Result { return [Bridge macOSVersion]; } // NOLINTEND(misc-use-internal-linkage) }