This commit is contained in:
Mars 2024-06-08 22:28:26 -04:00
parent 3ea546fe08
commit 05451841e8
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
8 changed files with 96 additions and 109 deletions

View file

@ -1,4 +1,3 @@
---
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: true
@ -10,9 +9,10 @@ AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium
BinPackArguments: false
BinPackParameters: false
IndentAccessModifiers: false
IndentExternBlock: Indent
Language: Cpp
NamespaceIndentation: All
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1

View file

@ -1,10 +1,12 @@
Checks: >
*,
-abseil-*,
-altera-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
-concurrency-mt-unsafe,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-type-member-init,
-fuchsia-*,
-google-*,
@ -16,6 +18,7 @@ Checks: >
-modernize-use-trailing-return-type,
-readability-braces-around-statements,
-readability-implicit-bool-conversion,
-readability-isolate-declaration,
-readability-magic-numbers
CheckOptions:
readability-identifier-naming.ClassCase: CamelCase

View file

@ -1,2 +1,2 @@
CompileFlags:
Add: [-std=c++20, -Wunused-function]
Add: [-std=c++23, -Wunused-function]

View file

@ -16,37 +16,7 @@
}:
utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
(_self: super: {
ccacheWrapper = super.ccacheWrapper.override {
extraConfig = ''
export CCACHE_COMPRESS=1
export CCACHE_DIR="/var/cache/ccache"
export CCACHE_UMASK=007
if [ ! -d "$CCACHE_DIR" ]; then
echo "====="
echo "Directory '$CCACHE_DIR' does not exist"
echo "Please create it with:"
echo " sudo mkdir -m0770 '$CCACHE_DIR'"
echo " sudo chown root:nixbld '$CCACHE_DIR'"
echo "====="
exit 1
fi
if [ ! -w "$CCACHE_DIR" ]; then
echo "====="
echo "Directory '$CCACHE_DIR' is not accessible for user $(whoami)"
echo "Please verify its access permissions"
echo "====="
exit 1
fi
'';
};
})
];
};
pkgs = import nixpkgs {inherit system;};
stdenv =
if pkgs.hostPlatform.isLinux

View file

@ -4,10 +4,10 @@ project(
default_options: [
'objc_std=c++20',
'objcpp_std=c++20',
'cpp_std=c++20',
'cpp_std=c++23',
'default_library=static',
'warning_level=everything',
'buildtype=debugoptimized',
'buildtype=debugoptimized'
]
)

View file

@ -81,7 +81,12 @@ int main() {
fmt::println("It is {}°F in {}", temp, townName);
fmt::println("{}", GetOSVersion());
const char* version = GetOSVersion();
fmt::println("{}", version);
delete[] version;
delete &config;
return 0;
}

View file

@ -1,5 +1,6 @@
#ifdef __linux__
#include <cstring>
#include <fstream>
#include <iostream>
#include <sdbus-c++/sdbus-c++.h>
@ -9,13 +10,6 @@
using std::string;
static const char *DBUS_INTERFACE = "org.freedesktop.DBus",
*DBUS_OBJECT_PATH = "/org/freedesktop/DBus",
*DBUS_METHOD_LIST_NAMES = "ListNames",
*MPRIS_INTERFACE_NAME = "org.mpris.MediaPlayer2",
*PLAYER_OBJECT_PATH = "/org/mpris/MediaPlayer2",
*PLAYER_INTERFACE_NAME = "org.mpris.MediaPlayer2.Player";
u64 ParseLineAsNumber(const string& input) {
// Find the first number
string::size_type start = 0;
@ -48,8 +42,6 @@ u64 MeminfoParse(std::ifstream input) {
u64 GetMemInfo() { return MeminfoParse(std::ifstream("/proc/meminfo")) * 1024; }
const char* GetOSVersion() {
static std::string prettyName;
std::ifstream file("/etc/os-release");
if (!file.is_open()) {
@ -57,40 +49,51 @@ const char* GetOSVersion() {
return nullptr;
}
std::string line;
std::string line;
const std::string prefix = "PRETTY_NAME=";
while (std::getline(file, line)) {
if (line.find("PRETTY_NAME=") == 0) {
// Remove the "PRETTY_NAME=" part and any surrounding quotes
prettyName = line.substr(12);
if (line.find(prefix) == 0) {
std::string prettyName = line.substr(prefix.size());
if (!prettyName.empty() && prettyName.front() == '"' &&
prettyName.back() == '"')
// Remove surrounding quotes if present
// clang-format off
if (!prettyName.empty() &&
prettyName.front() == '"' &&
prettyName.back() == '"')
prettyName = prettyName.substr(1, prettyName.size() - 2);
// clang-format on
file.close();
return prettyName.c_str();
// Allocate memory for the C-string and copy the content
char* cstr = new char[prettyName.size() + 1];
std::strcpy(cstr, prettyName.c_str());
return cstr;
}
}
file.close();
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";
std::unique_ptr<sdbus::IProxy> dbusProxy =
sdbus::createProxy(connection, DBUS_INTERFACE, DBUS_OBJECT_PATH);
sdbus::createProxy(connection, dbusInterface, dbusObjectPath);
std::vector<std::string> names;
dbusProxy->callMethod(DBUS_METHOD_LIST_NAMES)
.onInterface(DBUS_INTERFACE)
dbusProxy->callMethod(dbusMethodListNames)
.onInterface(dbusInterface)
.storeResultsTo(names);
std::vector<std::string> mprisPlayers;
for (const std::basic_string<char>& name : names)
if (name.find(MPRIS_INTERFACE_NAME) != std::string::npos)
mprisPlayers.push_back(name);
if (name.contains(mprisInterfaceName)) mprisPlayers.push_back(name);
return mprisPlayers;
}
@ -103,6 +106,9 @@ std::string GetActivePlayer(const std::vector<std::string>& mprisPlayers) {
std::string GetNowPlaying() {
try {
const char *playerObjectPath = "/org/mpris/MediaPlayer2",
*playerInterfaceName = "org.mpris.MediaPlayer2.Player";
std::unique_ptr<sdbus::IConnection> connection =
sdbus::createSessionBusConnection();
@ -115,10 +121,10 @@ std::string GetNowPlaying() {
if (activePlayer.empty()) return "";
std::unique_ptr<sdbus::IProxy> playerProxy =
sdbus::createProxy(*connection, activePlayer, PLAYER_OBJECT_PATH);
sdbus::createProxy(*connection, activePlayer, playerObjectPath);
std::map<std::string, sdbus::Variant> metadata =
playerProxy->getProperty("Metadata").onInterface(PLAYER_INTERFACE_NAME);
playerProxy->getProperty("Metadata").onInterface(playerInterfaceName);
auto iter = metadata.find("xesam:title");

View file

@ -1,18 +1,23 @@
#ifdef __APPLE__
#import "bridge.h"
#import <dispatch/dispatch.h>
#import <objc/runtime.h>
#import "bridge.h"
using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
dispatch_queue_t queue, void (^handler)(NSDictionary *information));
dispatch_queue_t queue,
void (^handler)(NSDictionary* information)
);
@implementation Bridge
+ (NSDictionary *)currentPlayingMetadata {
+ (NSDictionary*)currentPlayingMetadata {
CFURLRef ref = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
CFSTR("/System/Library/PrivateFrameworks/MediaRemote.framework"),
kCFURLPOSIXPathStyle, false);
kCFURLPOSIXPathStyle,
false
);
if (!ref) {
NSLog(@"Failed to load MediaRemote framework");
@ -30,7 +35,9 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
MRMediaRemoteGetNowPlayingInfoFunction mrMediaRemoteGetNowPlayingInfo =
reinterpret_cast<MRMediaRemoteGetNowPlayingInfoFunction>(
CFBundleGetFunctionPointerForName(
bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo")));
bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo")
)
);
if (!mrMediaRemoteGetNowPlayingInfo) {
NSLog(@"Failed to get function pointer for MRMediaRemoteGetNowPlayingInfo");
@ -38,15 +45,16 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
return nil;
}
__block NSDictionary *nowPlayingInfo = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSDictionary* nowPlayingInfo = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
mrMediaRemoteGetNowPlayingInfo(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(NSDictionary *information) {
^(NSDictionary* information) {
nowPlayingInfo = [information copy];
dispatch_semaphore_signal(semaphore);
});
}
);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
@ -54,26 +62,26 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
return nowPlayingInfo;
}
+ (NSString *)macOSVersion {
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
+ (NSString*)macOSVersion {
NSProcessInfo* processInfo = [NSProcessInfo processInfo];
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion];
NSString *version = [NSString
stringWithFormat:@"%ld.%ld.%ld", osVersion.majorVersion,
osVersion.minorVersion, osVersion.patchVersion];
NSString* version = [NSString stringWithFormat:@"%ld.%ld.%ld",
osVersion.majorVersion,
osVersion.minorVersion,
osVersion.patchVersion];
// Dictionary to map macOS versions to their respective names
NSDictionary<NSNumber *, NSString *> *versionNames =
NSDictionary<NSNumber*, NSString*>* versionNames =
@{@11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma"};
NSNumber *majorVersionNumber = @(osVersion.majorVersion);
NSString *versionName = versionNames[majorVersionNumber];
NSNumber* majorVersionNumber = @(osVersion.majorVersion);
NSString* versionName = versionNames[majorVersionNumber];
if (versionName == nil)
versionName = @"Unknown";
if (versionName == nil) versionName = @"Unknown";
NSString *fullVersion =
NSString* fullVersion =
[NSString stringWithFormat:@"macOS %@ %@", version, versionName];
return fullVersion;
@ -81,44 +89,39 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
@end
extern "C" {
const char *GetCurrentPlayingTitle() {
NSDictionary *metadata = [Bridge currentPlayingMetadata];
const char* GetCurrentPlayingTitle() {
NSDictionary* metadata = [Bridge currentPlayingMetadata];
if (metadata == nil) return nullptr;
NSString* title =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
if (title) return strdup([title UTF8String]);
if (metadata == nil)
return nullptr;
}
NSString *title =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
const char* GetCurrentPlayingArtist() {
NSDictionary* metadata = [Bridge currentPlayingMetadata];
if (title)
return strdup([title UTF8String]);
if (metadata == nil) return nullptr;
return nullptr;
}
NSString* artist =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
const char *GetCurrentPlayingArtist() {
NSDictionary *metadata = [Bridge currentPlayingMetadata];
if (artist) return strdup([artist UTF8String]);
if (metadata == nil)
return nullptr;
}
NSString *artist =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
const char* GetMacOSVersion() {
NSString* version = [Bridge macOSVersion];
if (artist)
return strdup([artist UTF8String]);
if (version) return strdup([version UTF8String]);
return nullptr;
}
const char *GetMacOSVersion() {
NSString *version = [Bridge macOSVersion];
if (version)
return strdup([version UTF8String]);
return nullptr;
}
return nullptr;
}
}
#endif