From 1be868acaf5459d7d65bbf595ef6730514030204 Mon Sep 17 00:00:00 2001 From: EinTim23 Date: Tue, 5 Nov 2024 22:53:43 +0100 Subject: [PATCH] added platform specific initialization function --- CMakeLists.txt | 4 ++-- src/backend.hpp | 3 ++- src/backends/darwin.mm | 15 +++++++++++++++ src/backends/linux.cpp | 23 ++++++++++++----------- src/backends/windows.cpp | 3 +++ src/main.cpp | 4 ++++ 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 734fc52..a61dcc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ add_executable (PlayerLink ${SOURCES}) set_property(TARGET PlayerLink PROPERTY CXX_STANDARD 20) add_subdirectory("vendor") set(LIBRARIES discord-rpc libcurl_static mbedcrypto mbedx509 mbedtls wxmono) -set(INCLUDES vendor vendor/libdbus vendor/wxWidgets/include) +set(INCLUDES vendor vendor/wxWidgets/include) #use windows subsystem to disable console window and link winrt if(WIN32) @@ -35,7 +35,7 @@ elseif(APPLE) endif() elseif(UNIX AND NOT APPLE) list(APPEND LIBRARIES dbus) - list(APPEND INCLUDES "${CMAKE_BINARY_DIR}/vendor/dbus") + list(APPEND INCLUDES "${CMAKE_BINARY_DIR}/vendor/dbus" vendor/libdbus) endif() #search directories for the autogenerated wxwidgets setup.h file for all plattforms diff --git a/src/backend.hpp b/src/backend.hpp index b770593..870772b 100644 --- a/src/backend.hpp +++ b/src/backend.hpp @@ -27,8 +27,9 @@ struct MediaInfo { }; namespace backend { + bool init(); bool toggleAutostart(bool enabled); std::shared_ptr getMediaInformation(); -} +} // namespace backend #endif \ No newline at end of file diff --git a/src/backends/darwin.mm b/src/backends/darwin.mm index f9fa0f3..835a547 100644 --- a/src/backends/darwin.mm +++ b/src/backends/darwin.mm @@ -1,5 +1,6 @@ #ifdef __APPLE__ #include +#include #include #include #include @@ -27,6 +28,13 @@ )" +void hideDockIcon(bool shouldHide) { + if (shouldHide) + [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]; + else + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; +} + std::shared_ptr backend::getMediaInformation() { __block NSString *appName = nil; __block NSDictionary *playingInfo = nil; @@ -87,6 +95,7 @@ std::shared_ptr backend::getMediaInformation() { return std::make_shared(paused, songTitle, songArtist, songAlbum, appNameString, thumbnailData, durationMs, elapsedTimeMs); } + bool backend::toggleAutostart(bool enabled) { std::filesystem::path launchAgentPath = std::getenv("HOME"); launchAgentPath = launchAgentPath / "Library" / "LaunchAgents" / "PlayerLink.plist"; @@ -101,5 +110,11 @@ bool backend::toggleAutostart(bool enabled) { o.close(); return true; } + +bool backend::init() { + hideDockIcon(true); + return true; +} + #undef LAUNCH_AGENT_TEMPLATE #endif \ No newline at end of file diff --git a/src/backends/linux.cpp b/src/backends/linux.cpp index 518bf0f..e54f60d 100644 --- a/src/backends/linux.cpp +++ b/src/backends/linux.cpp @@ -4,7 +4,7 @@ #include #include "../backend.hpp" -bool initialized = false; + DBusConnection* conn = nullptr; std::string getActivePlayer(DBusConnection* conn) { @@ -202,18 +202,19 @@ void getNowPlaying(DBusConnection* conn, const std::string& player) { dbus_message_unref(reply); } -std::shared_ptr backend::getMediaInformation() { - if (!initialized) { - DBusError err; - dbus_error_init(&err); +bool backend::init() { + DBusError err; + dbus_error_init(&err); - conn = dbus_bus_get(DBUS_BUS_SESSION, &err); - if (!conn) { - dbus_error_free(&err); - return nullptr; - } - initialized = true; + conn = dbus_bus_get(DBUS_BUS_SESSION, &err); + if (!conn) { + dbus_error_free(&err); + return false; } + return true; +} + +std::shared_ptr backend::getMediaInformation() { std::string player = getActivePlayer(conn); if (player == "") return nullptr; diff --git a/src/backends/windows.cpp b/src/backends/windows.cpp index 3a5db22..8b8e3f5 100644 --- a/src/backends/windows.cpp +++ b/src/backends/windows.cpp @@ -116,5 +116,8 @@ std::shared_ptr backend::getMediaInformation() { toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()), thumbnailData, endTime, elapsedTime); } + +bool backend::init() { return true; } + #undef EM_DASH #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e632ae5..53794de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -283,6 +283,10 @@ private: wxIMPLEMENT_APP_NO_MAIN(PlayerLink); int main(int argc, char** argv) { + if (!backend::init()) { + wxMessageBox(_("Error initializing platform backend!"), _("PlayerLink"), wxOK | wxICON_ERROR); + return -1; + } std::thread rpcThread(handleRPCTasks); rpcThread.detach(); std::thread mediaThread(handleMediaTasks);