macos
This commit is contained in:
parent
1a2fba7fb8
commit
e7a16d005c
4 changed files with 65 additions and 44 deletions
|
@ -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<u64, DraconisError>& packageCount = os::GetPackageCount())
|
||||
debug_log("{}", *packageCount);
|
||||
else
|
||||
debug_at(packageCount.error());
|
||||
|
||||
Element document = vbox({ hbox({ SystemInfoBox(config, data), filler() }), text("") });
|
||||
|
||||
|
|
|
@ -1,48 +1,53 @@
|
|||
#ifdef __APPLE__
|
||||
|
||||
// clang-format off
|
||||
#include <flat_map>
|
||||
#include <span>
|
||||
#include <sys/statvfs.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#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<u64, OsError> {
|
||||
using namespace util::types;
|
||||
|
||||
fn os::GetMemInfo() -> Result<u64, DraconisError> {
|
||||
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<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::GetWindowManager() -> Option<String> { return None; }
|
||||
|
||||
fn os::GetKernelVersion() -> Result<String, OsError> {
|
||||
fn os::GetKernelVersion() -> Result<String, DraconisError> {
|
||||
std::array<char, 256> 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<String, OsError> {
|
||||
fn os::GetHost() -> Result<String, DraconisError> {
|
||||
std::array<char, 256> 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<String, OsError> {
|
|||
{ "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<DiskSpace, OsError> {
|
||||
fn os::GetDiskUsage() -> Result<DiskSpace, DraconisError> {
|
||||
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<u64, DraconisError> {
|
||||
using util::error::DraconisErrorCode;
|
||||
|
||||
return Err(DraconisError(DraconisErrorCode::Other, "Not implemented on macOS"));
|
||||
}
|
||||
|
||||
fn os::GetShell() -> Option<String> { return None; }
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include <expected>
|
||||
|
||||
#include "../../util/macros.h"
|
||||
// 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;
|
||||
|
||||
#ifdef __OBJC__
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface Bridge : NSObject
|
||||
+ (void)fetchCurrentPlayingMetadata:(void (^)(Result<NSDictionary*, const char*>))completion;
|
||||
+ (Result<String, OsError>)macOSVersion;
|
||||
+ (Result<String, DraconisError>)macOSVersion;
|
||||
@end
|
||||
|
||||
#else
|
||||
|
||||
extern "C++" {
|
||||
fn GetCurrentPlayingInfo() -> Result<MediaInfo, NowPlayingError>;
|
||||
fn GetMacOSVersion() -> Result<String, OsError>;
|
||||
fn GetCurrentPlayingInfo() -> Result<MediaInfo, DraconisError>;
|
||||
fn GetMacOSVersion() -> Result<String, DraconisError>;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
#ifdef __APPLE__
|
||||
|
||||
// clang-format off
|
||||
#import "bridge.hpp"
|
||||
|
||||
#import <dispatch/dispatch.h>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
|
@ -8,7 +11,11 @@
|
|||
#include <string>
|
||||
#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 =
|
||||
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];
|
||||
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion];
|
||||
|
||||
|
@ -84,21 +91,21 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
|
|||
|
||||
extern "C++" {
|
||||
// NOLINTBEGIN(misc-use-internal-linkage)
|
||||
fn GetCurrentPlayingInfo() -> Result<MediaInfo, NowPlayingError> {
|
||||
__block Result<MediaInfo, NowPlayingError> result;
|
||||
fn GetCurrentPlayingInfo() -> Result<MediaInfo, DraconisError> {
|
||||
__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) {
|
||||
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<String, OsError> { return [Bridge macOSVersion]; }
|
||||
fn GetMacOSVersion() -> Result<String, DraconisError> { return [Bridge macOSVersion]; }
|
||||
// NOLINTEND(misc-use-internal-linkage)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue