some more updates and stuff

This commit is contained in:
Mars 2025-04-21 01:42:24 -04:00
parent 96c6e79780
commit 203d56e06b
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
17 changed files with 231 additions and 255 deletions

View file

@ -1,6 +1,5 @@
#ifdef __FreeBSD__
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <sdbus-c++/sdbus-c++.h>
@ -48,8 +47,7 @@ fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector<string> {
const sdbus::ObjectPath dbusObjectPath = sdbus::ObjectPath("/org/freedesktop/DBus");
const char* dbusMethodListNames = "ListNames";
const std::unique_ptr<sdbus::IProxy> dbusProxy =
createProxy(connection, dbusInterface, dbusObjectPath);
const std::unique_ptr<sdbus::IProxy> dbusProxy = createProxy(connection, dbusInterface, dbusObjectPath);
std::vector<string> names;
@ -58,8 +56,7 @@ fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector<string> {
std::vector<string> mprisPlayers;
for (const std::basic_string<char>& name : names)
if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2";
name.find(mprisInterfaceName) != std::string::npos)
if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2"; name.find(mprisInterfaceName) != String::npos)
mprisPlayers.push_back(name);
return mprisPlayers;
@ -74,8 +71,7 @@ fn GetActivePlayer(const std::vector<string>& mprisPlayers) -> string {
fn GetNowPlaying() -> string {
try {
const char *playerObjectPath = "/org/mpris/MediaPlayer2",
*playerInterfaceName = "org.mpris.MediaPlayer2.Player";
const char *playerObjectPath = "/org/mpris/MediaPlayer2", *playerInterfaceName = "org.mpris.MediaPlayer2.Player";
std::unique_ptr<sdbus::IConnection> connection = sdbus::createSessionBusConnection();
@ -89,24 +85,22 @@ fn GetNowPlaying() -> string {
if (activePlayer.empty())
return "";
auto playerProxy = sdbus::createProxy(
*connection, sdbus::ServiceName(activePlayer), sdbus::ObjectPath(playerObjectPath)
);
auto playerProxy =
sdbus::createProxy(*connection, sdbus::ServiceName(activePlayer), sdbus::ObjectPath(playerObjectPath));
sdbus::Variant metadataVariant =
playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
sdbus::Variant metadataVariant = playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
if (metadataVariant.containsValueOfType<std::map<std::string, sdbus::Variant>>()) {
const auto& metadata = metadataVariant.get<std::map<std::string, sdbus::Variant>>();
if (metadataVariant.containsValueOfType<std::map<String, sdbus::Variant>>()) {
const auto& metadata = metadataVariant.get<std::map<String, sdbus::Variant>>();
auto iter = metadata.find("xesam:title");
if (iter != metadata.end() && iter->second.containsValueOfType<std::string>())
return iter->second.get<std::string>();
if (iter != metadata.end() && iter->second.containsValueOfType<String>())
return iter->second.get<String>();
}
} catch (const sdbus::Error& e) {
if (e.getName() != "com.github.altdesktop.playerctld.NoActivePlayer")
return fmt::format("Error: {}", e.what());
return std::format("Error: {}", e.what());
return "No active player";
}

View file

@ -8,7 +8,6 @@
#include <dirent.h>
#include <expected>
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <mutex>
@ -24,7 +23,6 @@
#include "os.h"
#include "src/util/macros.h"
// Minimal global using declarations needed for function signatures
using std::expected;
using std::optional;
@ -32,7 +30,6 @@ namespace fs = std::filesystem;
using namespace std::literals::string_view_literals;
namespace {
// Local using declarations for the anonymous namespace
using std::array;
using std::bit_cast;
using std::getenv;
@ -121,7 +118,7 @@ namespace {
XCloseDisplay(display);
return "Unknown (X11)"; // Changed to empty string
return "Unknown (X11)";
}
fn TrimHyprlandWrapper(const string& input) -> string {

View file

@ -1,7 +1,8 @@
#ifdef __APPLE__
#include <expected>
#include <map>
#include <flat_map>
#include <span>
#include <sys/statvfs.h>
#include <sys/sysctl.h>
@ -9,42 +10,41 @@
#include "os.h"
#include "src/util/types.h"
fn GetMemInfo() -> expected<u64, string> {
fn GetMemInfo() -> expected<u64, String> {
u64 mem = 0;
usize size = sizeof(mem);
if (sysctlbyname("hw.memsize", &mem, &size, nullptr, 0) == -1)
return std::unexpected(string("sysctlbyname failed: ") + strerror(errno));
return std::unexpected(std::format("sysctlbyname failed: {}", strerror(errno)));
return mem;
}
fn GetNowPlaying() -> expected<string, NowPlayingError> { return GetCurrentPlayingInfo(); }
fn GetNowPlaying() -> expected<String, NowPlayingError> { return GetCurrentPlayingInfo(); }
fn GetOSVersion() -> expected<string, string> { return GetMacOSVersion(); }
fn GetOSVersion() -> expected<String, String> { return GetMacOSVersion(); }
fn GetDesktopEnvironment() -> optional<string> { return std::nullopt; }
fn GetDesktopEnvironment() -> optional<String> { return std::nullopt; }
fn GetWindowManager() -> string { return "Yabai"; }
fn GetWindowManager() -> String { return "Yabai"; }
fn GetKernelVersion() -> string {
std::array<char, 256> kernelVersion;
fn GetKernelVersion() -> String {
std::array<char, 256> kernelVersion {};
usize kernelVersionLen = sizeof(kernelVersion);
sysctlbyname("kern.osrelease", kernelVersion.data(), &kernelVersionLen, nullptr, 0);
sysctlbyname("kern.osrelease", std::span(kernelVersion).data(), &kernelVersionLen, nullptr, 0);
return kernelVersion.data();
}
fn GetHost() -> string {
std::array<char, 256> hwModel;
fn GetHost() -> String {
std::array<char, 256> hwModel {};
size_t hwModelLen = sizeof(hwModel);
sysctlbyname("hw.model", hwModel.data(), &hwModelLen, nullptr, 0);
// 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
std::map<std::string, std::string> modelNameByHwModel = {
std::flat_map<std::string_view, std::string_view> modelNameByHwModel = {
// MacBook Pro
{ "MacBookPro18,3", "MacBook Pro (14-inch, 2021)" },
{ "MacBookPro18,4", "MacBook Pro (14-inch, 2021)" },
@ -193,7 +193,7 @@ fn GetHost() -> string {
{ "iMac9,1", "iMac (24/20-inch, 2009)" },
};
return modelNameByHwModel[hwModel.data()];
return String(modelNameByHwModel[hwModel.data()]);
}
// returns free/total
@ -206,6 +206,6 @@ fn GetDiskUsage() -> std::pair<u64, u64> {
return { (vfs.f_blocks - vfs.f_bfree) * vfs.f_frsize, vfs.f_blocks * vfs.f_frsize };
}
fn GetShell() -> string { return ""; }
fn GetShell() -> String { return ""; }
#endif

View file

@ -3,7 +3,6 @@
#ifdef __APPLE__
#include <expected>
#include <string>
#include "../../util/macros.h"
@ -13,14 +12,14 @@
@interface Bridge : NSObject
+ (void)fetchCurrentPlayingMetadata:(void (^)(std::expected<NSDictionary*, const char*>))completion;
+ (std::expected<string, string>)macOSVersion;
+ (std::expected<String, String>)macOSVersion;
@end
#else
extern "C++" {
fn GetCurrentPlayingInfo() -> std::expected<std::string, NowPlayingError>;
fn GetMacOSVersion() -> std::expected<std::string, const char*>;
fn GetCurrentPlayingInfo() -> std::expected<String, NowPlayingError>;
fn GetMacOSVersion() -> std::expected<String, String>;
}
#endif

View file

@ -2,8 +2,11 @@
#import <dispatch/dispatch.h>
#include <expected>
#include <functional>
#include <memory>
#import <objc/runtime.h>
#include <string>
#include <utility>
#import "bridge.h"
@ -12,47 +15,51 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
@implementation Bridge
+ (void)fetchCurrentPlayingMetadata:(void (^)(std::expected<NSDictionary*, const char*>))completion {
CFURLRef ref = CFURLCreateWithFileSystemPath(
CFURLRef urlRef = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, CFSTR("/System/Library/PrivateFrameworks/MediaRemote.framework"), kCFURLPOSIXPathStyle, false
);
if (!ref) {
if (!urlRef) {
completion(std::unexpected("Failed to create CFURL for MediaRemote framework"));
return;
}
CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, ref);
CFRelease(ref);
CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
if (!bundle) {
CFRelease(urlRef);
if (!bundleRef) {
completion(std::unexpected("Failed to create bundle for MediaRemote framework"));
return;
}
auto mrMediaRemoteGetNowPlayingInfo = std::bit_cast<MRMediaRemoteGetNowPlayingInfoFunction>(
CFBundleGetFunctionPointerForName(bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo"))
CFBundleGetFunctionPointerForName(bundleRef, CFSTR("MRMediaRemoteGetNowPlayingInfo"))
);
if (!mrMediaRemoteGetNowPlayingInfo) {
CFRelease(bundle);
CFRelease(bundleRef);
completion(std::unexpected("Failed to get MRMediaRemoteGetNowPlayingInfo function pointer"));
return;
}
std::shared_ptr<std::remove_pointer_t<CFBundleRef>> sharedBundle(bundleRef, [](CFBundleRef bundle) {
if (bundle)
CFRelease(bundle);
});
mrMediaRemoteGetNowPlayingInfo(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(NSDictionary* information) {
NSDictionary* nowPlayingInfo = information; // Immutable, no copy needed
CFRelease(bundle);
completion(
nowPlayingInfo ? std::expected<NSDictionary*, const char*>(nowPlayingInfo)
: std::unexpected("No now playing information")
information ? std::expected<NSDictionary*, const char*>(information)
: std::unexpected("No now playing information")
);
}
);
}
+ (std::expected<string, string>)macOSVersion {
+ (std::expected<String, String>)macOSVersion {
NSProcessInfo* processInfo = [NSProcessInfo processInfo];
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion];
@ -71,15 +78,15 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
NSString* versionName = versionNames[majorVersion] ? versionNames[majorVersion] : @"Unknown";
NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", versionNumber, versionName];
return std::string([fullVersion UTF8String]);
return String([fullVersion UTF8String]);
}
@end
extern "C++" {
// NOLINTBEGIN(misc-use-internal-linkage)
fn GetCurrentPlayingInfo() -> std::expected<std::string, NowPlayingError> {
__block std::expected<std::string, NowPlayingError> result;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
fn GetCurrentPlayingInfo() -> std::expected<String, NowPlayingError> {
__block std::expected<String, NowPlayingError> result;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[Bridge fetchCurrentPlayingMetadata:^(std::expected<NSDictionary*, const char*> metadataResult) {
if (!metadataResult) {
@ -95,17 +102,17 @@ extern "C++" {
return;
}
NSString* title = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
NSString* artist = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
NSString* title = metadata[@"kMRMediaRemoteNowPlayingInfoTitle"];
NSString* artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"];
if (!title && !artist)
result = std::unexpected("No metadata");
result = std::unexpected(NowPlayingError { "No metadata" });
else if (!title)
result = std::string([artist UTF8String]);
result = String([artist UTF8String]);
else if (!artist)
result = std::string([title UTF8String]);
result = String([title UTF8String]);
else
result = std::string([[NSString stringWithFormat:@"%@ - %@", title, artist] UTF8String]);
result = String([[NSString stringWithFormat:@"%@ - %@", title, artist] UTF8String]);
dispatch_semaphore_signal(semaphore);
}];
@ -114,7 +121,7 @@ extern "C++" {
return result;
}
fn GetMacOSVersion() -> std::expected<string, string> { return [Bridge macOSVersion]; }
fn GetMacOSVersion() -> std::expected<String, String> { return [Bridge macOSVersion]; }
// NOLINTEND(misc-use-internal-linkage)
}

View file

@ -10,42 +10,42 @@ using std::optional, std::expected;
/**
* @brief Get the amount of installed RAM in bytes.
*/
fn GetMemInfo() -> expected<u64, string>;
fn GetMemInfo() -> expected<u64, String>;
/**
* @brief Get the currently playing song metadata.
*/
fn GetNowPlaying() -> expected<string, NowPlayingError>;
fn GetNowPlaying() -> expected<String, NowPlayingError>;
/**
* @brief Get the OS version.
*/
fn GetOSVersion() -> expected<string, string>;
fn GetOSVersion() -> expected<String, String>;
/**
* @brief Get the current desktop environment.
*/
fn GetDesktopEnvironment() -> optional<string>;
fn GetDesktopEnvironment() -> optional<String>;
/**
* @brief Get the current window manager.
*/
fn GetWindowManager() -> string;
fn GetWindowManager() -> String;
/**
* @brief Get the current shell.
*/
fn GetShell() -> string;
fn GetShell() -> String;
/**
* @brief Get the product family
*/
fn GetHost() -> string;
fn GetHost() -> String;
/**
* @brief Get the kernel version.
*/
fn GetKernelVersion() -> string;
fn GetKernelVersion() -> String;
/**
* @brief Get the number of installed packages.