From 41200459e52d811cded90623bc7b94b5098487a9 Mon Sep 17 00:00:00 2001 From: Mars Date: Thu, 6 Jun 2024 21:04:32 -0400 Subject: [PATCH] blegh --- .clang-format | 1 + flake.nix | 36 +++++++++++++++++++------------- meson.build | 8 +++---- src/os/linux.cpp | 14 +++++++------ src/os/macos.cpp | 5 ++++- src/os/macos/NowPlayingBridge.h | 4 ++-- src/os/macos/NowPlayingBridge.mm | 29 +++++++++++++------------ 7 files changed, 57 insertions(+), 40 deletions(-) diff --git a/.clang-format b/.clang-format index 1508db4..391e65c 100644 --- a/.clang-format +++ b/.clang-format @@ -6,6 +6,7 @@ NamespaceIndentation: All SpaceBeforeCpp11BracedList: true SpacesBeforeTrailingComments: 1 AlignConsecutiveAssignments: true +IndentExternBlock: Indent IncludeBlocks: Regroup IncludeCategories: - Regex: '".*"' diff --git a/flake.nix b/flake.nix index 274d988..e3d1dd3 100644 --- a/flake.nix +++ b/flake.nix @@ -46,7 +46,10 @@ ]; }; - stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.llvmPackages_18.stdenv; + stdenv = + if pkgs.hostPlatform.isLinux + then pkgs.stdenvAdapters.useMoldLinker pkgs.llvmPackages_18.stdenv + else pkgs.llvmPackages_18.stdenv; deps = with ( if !stdenv.isDarwin @@ -60,15 +63,16 @@ glib libcpr tomlplusplus - ] - ++ (with pkgs; (lib.optionals hostPlatform.isLinux [ - sdbus-cpp - valgrind - ])); + ]; - darwinPkgs = nixpkgs.lib.optionals stdenv.isDarwin (with pkgs.darwin; [ - apple_sdk.frameworks.CoreFoundation - apple_sdk.frameworks.MediaPlayer + linuxPkgs = nixpkgs.lib.optionals stdenv.isLinux (with pkgs.llvmPackages_18; [ + sdbus-cpp + valgrind + ]); + + darwinPkgs = nixpkgs.lib.optionals stdenv.isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ + Foundation + MediaPlayer ]); in with pkgs; { @@ -89,10 +93,13 @@ tomlplusplus ]; - buildInputs = [ - coost - fmt - ]; + buildInputs = + [ + coost + fmt + ] + ++ darwinPkgs + ++ linuxPkgs; configurePhase = '' meson setup build @@ -130,7 +137,8 @@ (writeScriptBin "run" "meson compile -C build && build/draconis++") ] ++ deps - ++ darwinPkgs; + ++ darwinPkgs + ++ linuxPkgs; name = "C++"; }; diff --git a/meson.build b/meson.build index af4f1e4..d95c069 100644 --- a/meson.build +++ b/meson.build @@ -7,7 +7,7 @@ project( 'cpp_std=c++20', 'default_library=static', 'warning_level=everything', - 'buildtype=debugoptimized' + 'buildtype=debugoptimized', ] ) @@ -16,7 +16,7 @@ clangtidy = find_program('clang-tidy', required: false) cpp = meson.get_compiler('cpp') if host_machine.system() == 'darwin' - add_language('objcpp') + add_languages('objcpp') objcpp = meson.get_compiler('objcpp') @@ -108,8 +108,8 @@ endif executable( 'draconis++', sources, - objc_args, - link_args, + objc_args: objc_args, + link_args: link_args, dependencies: deps, include_directories: incdir, ) diff --git a/src/os/linux.cpp b/src/os/linux.cpp index b251080..e12377e 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -57,24 +57,26 @@ uint64_t GetMemInfo() { std::vector GetMprisPlayers(sdbus::IConnection& connection) { auto dbusProxy = sdbus::createProxy(connection, DBUS_INTERFACE, DBUS_OBJECT_PATH); + std::vector names; + dbusProxy->callMethod(DBUS_METHOD_LIST_NAMES) .onInterface(DBUS_INTERFACE) .storeResultsTo(names); std::vector mprisPlayers; - for (const auto& name : names) { - if (name.find(MPRIS_INTERFACE_NAME) != std::string::npos) { + + for (const auto& name : names) + if (name.find(MPRIS_INTERFACE_NAME) != std::string::npos) mprisPlayers.push_back(name); - } - } + return mprisPlayers; } std::string GetActivePlayer(const std::vector& mprisPlayers) { - if (!mprisPlayers.empty()) { + if (!mprisPlayers.empty()) return mprisPlayers.front(); - } + return ""; } diff --git a/src/os/macos.cpp b/src/os/macos.cpp index 1fd1c5b..2f72c5e 100644 --- a/src/os/macos.cpp +++ b/src/os/macos.cpp @@ -16,7 +16,10 @@ uint64_t GetMemInfo() { } std::string GetNowPlaying() { - return GetCurrentPlayingTitle(); + if (const char* title = GetCurrentPlayingTitle()) + return title; + + return "No song playing"; } #endif diff --git a/src/os/macos/NowPlayingBridge.h b/src/os/macos/NowPlayingBridge.h index b75e86b..a592547 100644 --- a/src/os/macos/NowPlayingBridge.h +++ b/src/os/macos/NowPlayingBridge.h @@ -8,7 +8,7 @@ @end #else extern "C" { -const char* GetCurrentPlayingTitle(); -const char* GetCurrentPlayingArtist(); + const char* GetCurrentPlayingTitle(); + const char* GetCurrentPlayingArtist(); } #endif diff --git a/src/os/macos/NowPlayingBridge.mm b/src/os/macos/NowPlayingBridge.mm index 040a98c..5868a0f 100644 --- a/src/os/macos/NowPlayingBridge.mm +++ b/src/os/macos/NowPlayingBridge.mm @@ -5,7 +5,7 @@ #import #import -typedef void (*MRMediaRemoteGetNowPlayingInfoFunction)( +using MRMediaRemoteGetNowPlayingInfoFunction = void (*)( dispatch_queue_t queue, void (^handler)(NSDictionary *information)); @implementation NowPlayingBridge @@ -30,13 +30,13 @@ typedef void (*MRMediaRemoteGetNowPlayingInfoFunction)( #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wold-style-cast" - MRMediaRemoteGetNowPlayingInfoFunction MRMediaRemoteGetNowPlayingInfo = + auto mrMediaRemoteGetNowPlayingInfo = (MRMediaRemoteGetNowPlayingInfoFunction)CFBundleGetFunctionPointerForName( bundle, CFSTR("MRMediaRemoteGetNowPlayingInfo")); #pragma clang diagnostic pop - if (!MRMediaRemoteGetNowPlayingInfo) { + if (!mrMediaRemoteGetNowPlayingInfo) { NSLog(@"Failed to get function pointer for MRMediaRemoteGetNowPlayingInfo"); CFRelease(bundle); return nil; @@ -45,7 +45,7 @@ typedef void (*MRMediaRemoteGetNowPlayingInfoFunction)( __block NSDictionary *nowPlayingInfo = nil; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - MRMediaRemoteGetNowPlayingInfo( + mrMediaRemoteGetNowPlayingInfo( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(NSDictionary *information) { nowPlayingInfo = [information copy]; @@ -61,30 +61,33 @@ typedef void (*MRMediaRemoteGetNowPlayingInfoFunction)( @end extern "C" { - const char *GetCurrentPlayingTitle() { NSDictionary *metadata = [NowPlayingBridge currentPlayingMetadata]; - if (metadata == nil) { + + if (metadata == nil) return nullptr; - } + NSString *title = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"]; - if (title) { + + if (title) return strdup([title UTF8String]); - } + return nullptr; } const char *GetCurrentPlayingArtist() { NSDictionary *metadata = [NowPlayingBridge currentPlayingMetadata]; - if (metadata == nil) { + + if (metadata == nil) return nullptr; - } + NSString *artist = [metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"]; - if (artist) { + + if (artist) return strdup([artist UTF8String]); - } + return nullptr; } }