This commit is contained in:
Mars 2024-06-16 00:13:15 -04:00
parent f668a2eb4c
commit 50083503cf
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
224 changed files with 16150 additions and 19488 deletions

View file

@ -10,7 +10,7 @@
using std::string;
u64 ParseLineAsNumber(const string& input) {
fn ParseLineAsNumber(const string& input) -> u64 {
// Find the first number
string::size_type start = 0;
@ -27,7 +27,7 @@ u64 ParseLineAsNumber(const string& input) {
return std::stoul(input.substr(start, end - start));
}
u64 MeminfoParse(std::ifstream input) {
fn MeminfoParse(std::ifstream input) -> u64 {
string line;
// Skip every line before the one that starts with "MemTotal"
@ -39,9 +39,9 @@ u64 MeminfoParse(std::ifstream input) {
return num;
}
u64 GetMemInfo() { return MeminfoParse(std::ifstream("/proc/meminfo")) * 1024; }
fn GetMemInfo() -> u64 { return MeminfoParse(std::ifstream("/proc/meminfo")) * 1024; }
const char* GetOSVersion() {
fn GetOSVersion() -> const char* {
std::ifstream file("/etc/os-release");
if (!file.is_open()) {
@ -57,12 +57,8 @@ const char* GetOSVersion() {
std::string prettyName = line.substr(prefix.size());
// Remove surrounding quotes if present
// clang-format off
if (!prettyName.empty() &&
prettyName.front() == '"' &&
prettyName.back() == '"')
if (!prettyName.empty() && prettyName.front() == '"' && prettyName.back() == '"')
prettyName = prettyName.substr(1, prettyName.size() - 2);
// clang-format on
// Allocate memory for the C-string and copy the content
char* cstr = new char[prettyName.size() + 1];
@ -75,44 +71,40 @@ const char* GetOSVersion() {
return nullptr;
}
std::vector<std::string> GetMprisPlayers(sdbus::IConnection& connection) {
const char *dbusInterface = "org.freedesktop.DBus",
*dbusObjectPath = "/org/freedesktop/DBus",
*dbusMethodListNames = "ListNames",
*mprisInterfaceName = "org.mpris.MediaPlayer2";
fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector<std::string> {
const char *dbusInterface = "org.freedesktop.DBus", *dbusObjectPath = "/org/freedesktop/DBus",
*dbusMethodListNames = "ListNames";
std::unique_ptr<sdbus::IProxy> dbusProxy =
sdbus::createProxy(connection, dbusInterface, dbusObjectPath);
const std::unique_ptr<sdbus::IProxy> dbusProxy =
createProxy(connection, dbusInterface, dbusObjectPath);
std::vector<std::string> names;
dbusProxy->callMethod(dbusMethodListNames)
.onInterface(dbusInterface)
.storeResultsTo(names);
dbusProxy->callMethod(dbusMethodListNames).onInterface(dbusInterface).storeResultsTo(names);
std::vector<std::string> mprisPlayers;
for (const std::basic_string<char>& name : names)
if (name.contains(mprisInterfaceName))
if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2";
name.contains(mprisInterfaceName))
mprisPlayers.push_back(name);
return mprisPlayers;
}
std::string GetActivePlayer(const std::vector<std::string>& mprisPlayers) {
fn GetActivePlayer(const std::vector<std::string>& mprisPlayers) -> std::string {
if (!mprisPlayers.empty())
return mprisPlayers.front();
return "";
}
std::string GetNowPlaying() {
fn GetNowPlaying() -> std::string {
try {
const char *playerObjectPath = "/org/mpris/MediaPlayer2",
*playerInterfaceName = "org.mpris.MediaPlayer2.Player";
std::unique_ptr<sdbus::IConnection> connection =
sdbus::createSessionBusConnection();
std::unique_ptr<sdbus::IConnection> connection = sdbus::createSessionBusConnection();
std::vector<std::string> mprisPlayers = GetMprisPlayers(*connection);
@ -125,19 +117,16 @@ std::string GetNowPlaying() {
return "";
std::unique_ptr<sdbus::IProxy> playerProxy =
sdbus::createProxy(*connection, activePlayer, playerObjectPath);
sdbus::createProxy(*connection, activePlayer, playerObjectPath);
std::map<std::string, sdbus::Variant> metadata =
playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
auto iter = metadata.find("xesam:title");
if (const auto iter = metadata.find("xesam:title");
if (iter != metadata.end() &&
iter->second.containsValueOfType<std::string>())
iter != metadata.end() && iter->second.containsValueOfType<std::string>())
return iter->second.get<std::string>();
} catch (const sdbus::Error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
} catch (const sdbus::Error& e) { std::cerr << "Error: " << e.what() << '\n'; }
return "";
}

View file

@ -15,8 +15,7 @@ fn GetMemInfo() -> u64 {
}
fn GetNowPlaying() -> std::string {
if (const char* title = GetCurrentPlayingTitle();
const char* artist = GetCurrentPlayingArtist())
if (const char* title = GetCurrentPlayingTitle(); const char* artist = GetCurrentPlayingArtist())
return "Now Playing: " + std::string(artist) + " - " + std::string(title);
return "No song playing";

View file

@ -5,18 +5,16 @@
#import "bridge.h"
using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
dispatch_queue_t queue,
void (^handler)(NSDictionary* information)
);
using MRMediaRemoteGetNowPlayingInfoFunction =
void (*)(dispatch_queue_t queue, void (^handler)(NSDictionary* information));
@implementation Bridge
+ (NSDictionary*)currentPlayingMetadata {
CFURLRef ref = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
CFSTR("/System/Library/PrivateFrameworks/MediaRemote.framework"),
kCFURLPOSIXPathStyle,
false
kCFAllocatorDefault,
CFSTR("/System/Library/PrivateFrameworks/MediaRemote.framework"),
kCFURLPOSIXPathStyle,
false
);
if (!ref) {
@ -33,11 +31,9 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
}
MRMediaRemoteGetNowPlayingInfoFunction mrMediaRemoteGetNowPlayingInfo =
reinterpret_cast<MRMediaRemoteGetNowPlayingInfoFunction>(
CFBundleGetFunctionPointerForName(
bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo")
)
);
reinterpret_cast<MRMediaRemoteGetNowPlayingInfoFunction>(
CFBundleGetFunctionPointerForName(bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo"))
);
if (!mrMediaRemoteGetNowPlayingInfo) {
NSLog(@"Failed to get function pointer for MRMediaRemoteGetNowPlayingInfo");
@ -49,11 +45,11 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
mrMediaRemoteGetNowPlayingInfo(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(NSDictionary* information) {
nowPlayingInfo = [information copy];
dispatch_semaphore_signal(semaphore);
}
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(NSDictionary* information) {
nowPlayingInfo = [information copy];
dispatch_semaphore_signal(semaphore);
}
);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
@ -69,9 +65,8 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
NSString* version;
if (osVersion.patchVersion == 0) {
version = [NSString stringWithFormat:@"%ld.%ld",
osVersion.majorVersion,
osVersion.minorVersion];
version =
[NSString stringWithFormat:@"%ld.%ld", osVersion.majorVersion, osVersion.minorVersion];
} else {
version = [NSString stringWithFormat:@"%ld.%ld.%ld",
osVersion.majorVersion,
@ -80,13 +75,8 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
}
// Dictionary to map macOS versions to their respective names
NSDictionary<NSNumber*, NSString*>* versionNames = @{
@11 : @"Big Sur",
@12 : @"Monterey",
@13 : @"Ventura",
@14 : @"Sonoma",
@15 : @"Sequoia"
};
NSDictionary<NSNumber*, NSString*>* versionNames =
@{ @11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma", @15 : @"Sequoia" };
NSNumber* majorVersionNumber = @(osVersion.majorVersion);
NSString* versionName = versionNames[majorVersionNumber];
@ -94,8 +84,7 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
if (versionName == nil)
versionName = @"Unknown";
NSString* fullVersion =
[NSString stringWithFormat:@"macOS %@ %@", version, versionName];
NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", version, versionName];
return fullVersion;
}
@ -110,8 +99,7 @@ extern "C" {
if (metadata == nil)
return nullptr;
NSString* title =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
NSString* title = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
if (title)
return strdup([title UTF8String]);
@ -125,8 +113,7 @@ extern "C" {
if (metadata == nil)
return nullptr;
NSString* artist =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
NSString* artist = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
if (artist)
return strdup([artist UTF8String]);