diff --git a/.clang-format b/.clang-format index 7e1c343..f265bfb 100644 --- a/.clang-format +++ b/.clang-format @@ -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 diff --git a/.clang-tidy b/.clang-tidy index 0c7feb1..f075861 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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 diff --git a/.clangd b/.clangd index adf7a53..290fae3 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,2 @@ CompileFlags: - Add: [-std=c++20, -Wunused-function] + Add: [-std=c++23, -Wunused-function] diff --git a/flake.nix b/flake.nix index 45c8d9f..ea81a5b 100644 --- a/flake.nix +++ b/flake.nix @@ -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 diff --git a/meson.build b/meson.build index 6cbdd36..87cd266 100644 --- a/meson.build +++ b/meson.build @@ -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' ] ) diff --git a/src/main.cpp b/src/main.cpp index ada3c99..a6c2de5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } diff --git a/src/os/linux.cpp b/src/os/linux.cpp index 4d1bb45..2308756 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -1,5 +1,6 @@ #ifdef __linux__ +#include #include #include #include @@ -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 GetMprisPlayers(sdbus::IConnection& connection) { + const char *dbusInterface = "org.freedesktop.DBus", + *dbusObjectPath = "/org/freedesktop/DBus", + *dbusMethodListNames = "ListNames", + *mprisInterfaceName = "org.mpris.MediaPlayer2"; + std::unique_ptr dbusProxy = - sdbus::createProxy(connection, DBUS_INTERFACE, DBUS_OBJECT_PATH); + sdbus::createProxy(connection, dbusInterface, dbusObjectPath); std::vector names; - dbusProxy->callMethod(DBUS_METHOD_LIST_NAMES) - .onInterface(DBUS_INTERFACE) + dbusProxy->callMethod(dbusMethodListNames) + .onInterface(dbusInterface) .storeResultsTo(names); std::vector mprisPlayers; for (const std::basic_string& 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& mprisPlayers) { std::string GetNowPlaying() { try { + const char *playerObjectPath = "/org/mpris/MediaPlayer2", + *playerInterfaceName = "org.mpris.MediaPlayer2.Player"; + std::unique_ptr connection = sdbus::createSessionBusConnection(); @@ -115,10 +121,10 @@ std::string GetNowPlaying() { if (activePlayer.empty()) return ""; std::unique_ptr playerProxy = - sdbus::createProxy(*connection, activePlayer, PLAYER_OBJECT_PATH); + sdbus::createProxy(*connection, activePlayer, playerObjectPath); std::map metadata = - playerProxy->getProperty("Metadata").onInterface(PLAYER_INTERFACE_NAME); + playerProxy->getProperty("Metadata").onInterface(playerInterfaceName); auto iter = metadata.find("xesam:title"); diff --git a/src/os/macos/bridge.mm b/src/os/macos/bridge.mm index 1e645f2..e17691a 100644 --- a/src/os/macos/bridge.mm +++ b/src/os/macos/bridge.mm @@ -1,18 +1,23 @@ #ifdef __APPLE__ -#import "bridge.h" #import #import +#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( 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 *versionNames = + NSDictionary* 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