remove playerctl lol

This commit is contained in:
Mars 2024-06-06 04:14:52 -04:00
parent 500138ce67
commit 3071b23b93
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
7 changed files with 130 additions and 89 deletions

View file

@ -46,9 +46,27 @@
];
};
stdenv = pkgs.llvmPackages_18.stdenv;
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.llvmPackages_18.stdenv;
darwinPkgs = nixpkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin; [
deps = with (
if !stdenv.isDarwin
then pkgs.pkgsStatic
else pkgs
); # TODO: Remove when fixed on darwin
[
coost
fmt
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
]);
@ -66,15 +84,10 @@
pkg-config
];
propagatedBuildInputs =
[
libcpr
tomlplusplus
]
++ (lib.optionals pkgs.hostPlatform.isLinux [
glib
playerctl
]);
propagatedBuildInputs = [
libcpr
tomlplusplus
];
buildInputs = [
coost
@ -107,24 +120,16 @@
bear
clang-tools_18
meson
lldb
ninja
pkg-config
unzip
coost
fmt
glib
libcpr
tomlplusplus
]
++ (lib.optionals pkgs.hostPlatform.isLinux [playerctl])
++ darwinPkgs;
buildInputs =
[
coost
libcpr
tomlplusplus
(writeScriptBin "build" "meson compile -C build")
(writeScriptBin "clean" "meson setup build --wipe")
(writeScriptBin "run" "meson compile -C build && build/draconis++")
]
++ deps
++ darwinPkgs;
name = "C++";

View file

@ -1,5 +1,5 @@
project(
'draconis++', ['cpp', 'objcpp'],
'draconis++', 'cpp',
version: '0.1.0',
default_options: [
'objc_std=c++20',
@ -14,21 +14,26 @@ project(
clangtidy = find_program('clang-tidy', required: false)
cpp = meson.get_compiler('cpp')
objcpp = meson.get_compiler('objcpp')
add_project_arguments(
objcpp.get_supported_arguments([
'-Wno-c++20-compat',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
'-Wno-missing-prototypes',
'-Wno-padded',
'-Wno-pre-c++20-compat-pedantic',
'-Wno-switch-default',
'-Wunused-function',
]),
language: 'objcpp'
)
if host_machine.system() == 'darwin'
add_language('objcpp')
objcpp = meson.get_compiler('objcpp')
add_project_arguments(
objcpp.get_supported_arguments([
'-Wno-c++20-compat',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
'-Wno-missing-prototypes',
'-Wno-padded',
'-Wno-pre-c++20-compat-pedantic',
'-Wno-switch-default',
'-Wunused-function',
]),
language: 'objcpp'
)
endif
add_project_arguments(
cpp.get_supported_arguments([
@ -77,12 +82,14 @@ deps += cpp.find_library('curl')
deps += cpp.find_library('tomlplusplus')
deps += dependency('coost')
deps += dependency('fmt')
deps += dependency('Foundation')
deps += dependency('MediaPlayer')
if host_machine.system() == 'darwin'
deps += dependency('Foundation')
deps += dependency('MediaPlayer')
endif
if host_machine.system() == 'linux'
deps += dependency('playerctl')
deps += dependency('sdbus-c++')
endif
incdir = include_directories(
@ -90,11 +97,19 @@ incdir = include_directories(
is_system: true # Ignores warnings from include dir
)
objc_args = []
link_args = []
if host_machine.system() == 'darwin'
objc_args += ['-fobjc-arc']
link_args += ['-framework', 'Foundation', '-framework', 'MediaPlayer']
endif
executable(
'draconis++',
sources,
objc_args,
link_args,
dependencies: deps,
include_directories: incdir,
objc_args: ['-fobjc-arc'],
link_args: ['-framework', 'Foundation', '-framework', 'MediaPlayer']
)

View file

@ -45,8 +45,6 @@ DateNum ParseDate(string const& input) {
int main(int argc, char** argv) {
flag::parse(argc, argv);
LOG << "hello " << 23;
const Config& config = Config::getInstance();
if (config.getNowPlaying().getEnabled())

View file

@ -2,12 +2,22 @@
#include <fmt/core.h>
#include <fstream>
#include <playerctl/playerctl.h>
#include <iostream>
#include <sdbus-c++/sdbus-c++.h>
#include <string>
#include <vector>
#include "os.h"
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";
uint64_t ParseLineAsNumber(const string& input) {
// Find the first number
string::size_type start = 0;
@ -44,46 +54,59 @@ uint64_t GetMemInfo() {
return MeminfoParse(std::ifstream("/proc/meminfo")) * 1024;
}
PlayerctlPlayer* InitPlayerctl() {
// Create a player manager
PlayerctlPlayerManager* playerManager = playerctl_player_manager_new(nullptr);
std::vector<std::string> GetMprisPlayers(sdbus::IConnection& connection) {
auto dbusProxy =
sdbus::createProxy(connection, DBUS_INTERFACE, DBUS_OBJECT_PATH);
std::vector<std::string> names;
dbusProxy->callMethod(DBUS_METHOD_LIST_NAMES)
.onInterface(DBUS_INTERFACE)
.storeResultsTo(names);
// Create an empty player list
GList* availablePlayers = nullptr;
// Get the list of available players and put it in the player list
g_object_get(playerManager, "player-names", &availablePlayers, nullptr);
// If no players are available, return nullptr
if (!availablePlayers)
return nullptr;
// Get the first player
PlayerctlPlayerName* playerName =
static_cast<PlayerctlPlayerName*>(availablePlayers->data);
// Create the player
PlayerctlPlayer* const currentPlayer =
playerctl_player_new_from_name(playerName, nullptr);
// If no player is available, return nullptr
if (!currentPlayer)
return nullptr;
// Manage the player
playerctl_player_manager_manage_player(playerManager, currentPlayer);
// Unref the player
g_object_unref(currentPlayer);
return currentPlayer;
std::vector<std::string> mprisPlayers;
for (const auto& name : names) {
if (name.find(MPRIS_INTERFACE_NAME) != std::string::npos) {
mprisPlayers.push_back(name);
}
}
return mprisPlayers;
}
string GetNowPlaying() {
if (PlayerctlPlayer* currentPlayer = InitPlayerctl())
return playerctl_player_get_title(currentPlayer, nullptr);
std::string GetActivePlayer(const std::vector<std::string>& mprisPlayers) {
if (!mprisPlayers.empty()) {
return mprisPlayers.front();
}
return "";
}
return "Could not get now playing info";
std::string GetNowPlaying() {
try {
auto connection = sdbus::createSessionBusConnection();
auto mprisPlayers = GetMprisPlayers(*connection);
if (mprisPlayers.empty())
return "";
std::string activePlayer = GetActivePlayer(mprisPlayers);
if (activePlayer.empty())
return "";
auto playerProxy =
sdbus::createProxy(*connection, activePlayer, PLAYER_OBJECT_PATH);
std::map<std::string, sdbus::Variant> metadata =
playerProxy->getProperty("Metadata").onInterface(PLAYER_INTERFACE_NAME);
auto iter = metadata.find("xesam:title");
if (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;
}
return "";
}
#endif

View file

@ -16,7 +16,7 @@ uint64_t GetMemInfo() {
}
std::string GetNowPlaying() {
return getCurrentPlayingTitle();
return GetCurrentPlayingTitle();
}
#endif

View file

@ -8,7 +8,7 @@
@end
#else
extern "C" {
const char* getCurrentPlayingTitle();
const char* getCurrentPlayingArtist();
const char* GetCurrentPlayingTitle();
const char* GetCurrentPlayingArtist();
}
#endif

View file

@ -62,7 +62,7 @@ typedef void (*MRMediaRemoteGetNowPlayingInfoFunction)(
extern "C" {
const char *getCurrentPlayingTitle() {
const char *GetCurrentPlayingTitle() {
NSDictionary *metadata = [NowPlayingBridge currentPlayingMetadata];
if (metadata == nil) {
return nullptr;
@ -75,7 +75,7 @@ const char *getCurrentPlayingTitle() {
return nullptr;
}
const char *getCurrentPlayingArtist() {
const char *GetCurrentPlayingArtist() {
NSDictionary *metadata = [NowPlayingBridge currentPlayingMetadata];
if (metadata == nil) {
return nullptr;