added platform specific initialization function

This commit is contained in:
EinTim23 2024-11-05 22:53:43 +01:00
parent 9e13a8f4fe
commit 1be868acaf
6 changed files with 38 additions and 14 deletions

View File

@ -18,7 +18,7 @@ add_executable (PlayerLink ${SOURCES})
set_property(TARGET PlayerLink PROPERTY CXX_STANDARD 20) set_property(TARGET PlayerLink PROPERTY CXX_STANDARD 20)
add_subdirectory("vendor") add_subdirectory("vendor")
set(LIBRARIES discord-rpc libcurl_static mbedcrypto mbedx509 mbedtls wxmono) 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 #use windows subsystem to disable console window and link winrt
if(WIN32) if(WIN32)
@ -35,7 +35,7 @@ elseif(APPLE)
endif() endif()
elseif(UNIX AND NOT APPLE) elseif(UNIX AND NOT APPLE)
list(APPEND LIBRARIES dbus) list(APPEND LIBRARIES dbus)
list(APPEND INCLUDES "${CMAKE_BINARY_DIR}/vendor/dbus") list(APPEND INCLUDES "${CMAKE_BINARY_DIR}/vendor/dbus" vendor/libdbus)
endif() endif()
#search directories for the autogenerated wxwidgets setup.h file for all plattforms #search directories for the autogenerated wxwidgets setup.h file for all plattforms

View File

@ -27,8 +27,9 @@ struct MediaInfo {
}; };
namespace backend { namespace backend {
bool init();
bool toggleAutostart(bool enabled); bool toggleAutostart(bool enabled);
std::shared_ptr<MediaInfo> getMediaInformation(); std::shared_ptr<MediaInfo> getMediaInformation();
} } // namespace backend
#endif #endif

View File

@ -1,5 +1,6 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#include <Cocoa/Cocoa.h>
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
#include <dispatch/dispatch.h> #include <dispatch/dispatch.h>
#include <filesystem> #include <filesystem>
@ -27,6 +28,13 @@
</dict> </dict>
</plist>)" </plist>)"
void hideDockIcon(bool shouldHide) {
if (shouldHide)
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
else
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}
std::shared_ptr<MediaInfo> backend::getMediaInformation() { std::shared_ptr<MediaInfo> backend::getMediaInformation() {
__block NSString *appName = nil; __block NSString *appName = nil;
__block NSDictionary *playingInfo = nil; __block NSDictionary *playingInfo = nil;
@ -87,6 +95,7 @@ std::shared_ptr<MediaInfo> backend::getMediaInformation() {
return std::make_shared<MediaInfo>(paused, songTitle, songArtist, songAlbum, appNameString, thumbnailData, return std::make_shared<MediaInfo>(paused, songTitle, songArtist, songAlbum, appNameString, thumbnailData,
durationMs, elapsedTimeMs); durationMs, elapsedTimeMs);
} }
bool backend::toggleAutostart(bool enabled) { bool backend::toggleAutostart(bool enabled) {
std::filesystem::path launchAgentPath = std::getenv("HOME"); std::filesystem::path launchAgentPath = std::getenv("HOME");
launchAgentPath = launchAgentPath / "Library" / "LaunchAgents" / "PlayerLink.plist"; launchAgentPath = launchAgentPath / "Library" / "LaunchAgents" / "PlayerLink.plist";
@ -101,5 +110,11 @@ bool backend::toggleAutostart(bool enabled) {
o.close(); o.close();
return true; return true;
} }
bool backend::init() {
hideDockIcon(true);
return true;
}
#undef LAUNCH_AGENT_TEMPLATE #undef LAUNCH_AGENT_TEMPLATE
#endif #endif

View File

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#include "../backend.hpp" #include "../backend.hpp"
bool initialized = false;
DBusConnection* conn = nullptr; DBusConnection* conn = nullptr;
std::string getActivePlayer(DBusConnection* conn) { std::string getActivePlayer(DBusConnection* conn) {
@ -202,18 +202,19 @@ void getNowPlaying(DBusConnection* conn, const std::string& player) {
dbus_message_unref(reply); dbus_message_unref(reply);
} }
std::shared_ptr<MediaInfo> backend::getMediaInformation() { bool backend::init() {
if (!initialized) {
DBusError err; DBusError err;
dbus_error_init(&err); dbus_error_init(&err);
conn = dbus_bus_get(DBUS_BUS_SESSION, &err); conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (!conn) { if (!conn) {
dbus_error_free(&err); dbus_error_free(&err);
return nullptr; return false;
} }
initialized = true; return true;
} }
std::shared_ptr<MediaInfo> backend::getMediaInformation() {
std::string player = getActivePlayer(conn); std::string player = getActivePlayer(conn);
if (player == "") if (player == "")
return nullptr; return nullptr;

View File

@ -116,5 +116,8 @@ std::shared_ptr<MediaInfo> backend::getMediaInformation() {
toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()), toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()),
thumbnailData, endTime, elapsedTime); thumbnailData, endTime, elapsedTime);
} }
bool backend::init() { return true; }
#undef EM_DASH #undef EM_DASH
#endif #endif

View File

@ -283,6 +283,10 @@ private:
wxIMPLEMENT_APP_NO_MAIN(PlayerLink); wxIMPLEMENT_APP_NO_MAIN(PlayerLink);
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (!backend::init()) {
wxMessageBox(_("Error initializing platform backend!"), _("PlayerLink"), wxOK | wxICON_ERROR);
return -1;
}
std::thread rpcThread(handleRPCTasks); std::thread rpcThread(handleRPCTasks);
rpcThread.detach(); rpcThread.detach();
std::thread mediaThread(handleMediaTasks); std::thread mediaThread(handleMediaTasks);