This commit is contained in:
Mars 2025-04-28 11:59:19 -04:00
parent 1a2fba7fb8
commit e7a16d005c
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
4 changed files with 65 additions and 44 deletions

View file

@ -267,7 +267,10 @@ fn main() -> i32 {
const Config& config = Config::getInstance(); const Config& config = Config::getInstance();
const SystemData data = SystemData::fetchSystemData(config); const SystemData data = SystemData::fetchSystemData(config);
debug_log("{}", *os::GetPackageCount()); if (const Result<u64, DraconisError>& packageCount = os::GetPackageCount())
debug_log("{}", *packageCount);
else
debug_at(packageCount.error());
Element document = vbox({ hbox({ SystemInfoBox(config, data), filler() }), text("") }); Element document = vbox({ hbox({ SystemInfoBox(config, data), filler() }), text("") });

View file

@ -1,48 +1,53 @@
#ifdef __APPLE__ #ifdef __APPLE__
// clang-format off
#include <flat_map> #include <flat_map>
#include <span>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include "macos/bridge.h" #include "macos/bridge.hpp"
#include "os.h" #include "os.hpp"
#include "src/util/types.h" #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<u64, OsError> { using namespace util::types;
fn os::GetMemInfo() -> Result<u64, DraconisError> {
u64 mem = 0; u64 mem = 0;
usize size = sizeof(mem); usize size = sizeof(mem);
if (sysctlbyname("hw.memsize", &mem, &size, nullptr, 0) == -1) 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; return mem;
} }
fn os::GetNowPlaying() -> Result<MediaInfo, NowPlayingError> { return GetCurrentPlayingInfo(); } fn os::GetNowPlaying() -> Result<MediaInfo, DraconisError> { return GetCurrentPlayingInfo(); }
fn os::GetOSVersion() -> Result<String, OsError> { return GetMacOSVersion(); } fn os::GetOSVersion() -> Result<String, DraconisError> { return GetMacOSVersion(); }
fn os::GetDesktopEnvironment() -> Option<String> { return None; } fn os::GetDesktopEnvironment() -> Option<String> { return None; }
fn os::GetWindowManager() -> Option<String> { return None; } fn os::GetWindowManager() -> Option<String> { return None; }
fn os::GetKernelVersion() -> Result<String, OsError> { fn os::GetKernelVersion() -> Result<String, DraconisError> {
std::array<char, 256> kernelVersion {}; std::array<char, 256> kernelVersion {};
usize kernelVersionLen = sizeof(kernelVersion); usize kernelVersionLen = sizeof(kernelVersion);
if (sysctlbyname("kern.osrelease", kernelVersion.data(), &kernelVersionLen, nullptr, 0) == -1) 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(); return kernelVersion.data();
} }
fn os::GetHost() -> Result<String, OsError> { fn os::GetHost() -> Result<String, DraconisError> {
std::array<char, 256> hwModel {}; std::array<char, 256> hwModel {};
size_t hwModelLen = sizeof(hwModel); size_t hwModelLen = sizeof(hwModel);
if (sysctlbyname("hw.model", hwModel.data(), &hwModelLen, nullptr, 0) == -1) 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 // 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 // shortened a lot of the entries to remove unnecessary info
@ -195,20 +200,27 @@ fn os::GetHost() -> Result<String, OsError> {
{ "iMac9,1", "iMac (24/20-inch, 2009)" }, { "iMac9,1", "iMac (24/20-inch, 2009)" },
}; };
const auto it = modelNameByHwModel.find(hwModel.data()); const auto iter = modelNameByHwModel.find(hwModel.data());
if (it == modelNameByHwModel.end()) if (iter == modelNameByHwModel.end())
return Err(OsError::withErrno("Failed to get host info")); return Err(DraconisError::withErrno("Failed to get host info"));
return String(it->second); return String(iter->second);
} }
fn os::GetDiskUsage() -> Result<DiskSpace, OsError> { fn os::GetDiskUsage() -> Result<DiskSpace, DraconisError> {
struct statvfs vfs; struct statvfs vfs;
if (statvfs("/", &vfs) != 0) 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<u64, DraconisError> {
using util::error::DraconisErrorCode;
return Err(DraconisError(DraconisErrorCode::Other, "Not implemented on macOS"));
} }
fn os::GetShell() -> Option<String> { return None; } fn os::GetShell() -> Option<String> { return None; }

View file

@ -2,25 +2,25 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include <expected> // 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 <Foundation/Foundation.h>
#ifdef __OBJC__
#import <Foundation/Foundation.h>
@interface Bridge : NSObject @interface Bridge : NSObject
+ (void)fetchCurrentPlayingMetadata:(void (^)(Result<NSDictionary*, const char*>))completion; + (void)fetchCurrentPlayingMetadata:(void (^)(Result<NSDictionary*, const char*>))completion;
+ (Result<String, OsError>)macOSVersion; + (Result<String, DraconisError>)macOSVersion;
@end @end
#else
#else
extern "C++" { extern "C++" {
fn GetCurrentPlayingInfo() -> Result<MediaInfo, NowPlayingError>; fn GetCurrentPlayingInfo() -> Result<MediaInfo, DraconisError>;
fn GetMacOSVersion() -> Result<String, OsError>; fn GetMacOSVersion() -> Result<String, DraconisError>;
} }
#endif
#endif
#endif #endif

View file

@ -1,5 +1,8 @@
#ifdef __APPLE__ #ifdef __APPLE__
// clang-format off
#import "bridge.hpp"
#import <dispatch/dispatch.h> #import <dispatch/dispatch.h>
#include <expected> #include <expected>
#include <functional> #include <functional>
@ -8,7 +11,11 @@
#include <string> #include <string>
#include <utility> #include <utility>
#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 = using MRMediaRemoteGetNowPlayingInfoFunction =
void (*)(dispatch_queue_t queue, void (^handler)(NSDictionary* information)); void (*)(dispatch_queue_t queue, void (^handler)(NSDictionary* information));
@ -59,7 +66,7 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
); );
} }
+ (Result<String, OsError>)macOSVersion { + (Result<String, DraconisError>)macOSVersion {
NSProcessInfo* processInfo = [NSProcessInfo processInfo]; NSProcessInfo* processInfo = [NSProcessInfo processInfo];
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion]; NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion];
@ -84,21 +91,21 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
extern "C++" { extern "C++" {
// NOLINTBEGIN(misc-use-internal-linkage) // NOLINTBEGIN(misc-use-internal-linkage)
fn GetCurrentPlayingInfo() -> Result<MediaInfo, NowPlayingError> { fn GetCurrentPlayingInfo() -> Result<MediaInfo, DraconisError> {
__block Result<MediaInfo, NowPlayingError> result; __block Result<MediaInfo, DraconisError> result;
dispatch_semaphore_t const semaphore = dispatch_semaphore_create(0); const dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[Bridge fetchCurrentPlayingMetadata:^(std::expected<NSDictionary*, const char*> metadataResult) { [Bridge fetchCurrentPlayingMetadata:^(std::expected<NSDictionary*, const char*> metadataResult) {
if (!metadataResult) { if (!metadataResult) {
result = Err(OsError { OsErrorCode::InternalError, metadataResult.error() }); result = Err(DraconisError(DraconisErrorCode::InternalError, metadataResult.error()));
dispatch_semaphore_signal(semaphore); dispatch_semaphore_signal(semaphore);
return; return;
} }
const NSDictionary* const metadata = *metadataResult; const NSDictionary* const metadata = *metadataResult;
if (!metadata) { if (!metadata) {
result = Err(OsError { OsErrorCode::InternalError, "No metadata" }); result = Err(DraconisError(DraconisErrorCode::InternalError, "No metadata"));
dispatch_semaphore_signal(semaphore); dispatch_semaphore_signal(semaphore);
return; return;
} }
@ -107,8 +114,7 @@ extern "C++" {
const NSString* const artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"]; const NSString* const artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"];
result = MediaInfo( result = MediaInfo(
title ? Option(String([title UTF8String])) : None, title ? Option(String([title UTF8String])) : None, artist ? Option(String([artist UTF8String])) : None
artist ? Option(String([artist UTF8String])) : None
); );
dispatch_semaphore_signal(semaphore); dispatch_semaphore_signal(semaphore);
@ -118,7 +124,7 @@ extern "C++" {
return result; return result;
} }
fn GetMacOSVersion() -> Result<String, OsError> { return [Bridge macOSVersion]; } fn GetMacOSVersion() -> Result<String, DraconisError> { return [Bridge macOSVersion]; }
// NOLINTEND(misc-use-internal-linkage) // NOLINTEND(misc-use-internal-linkage)
} }