From 42255b6569e0500ac0caf20e2fc5059ce3db65cb Mon Sep 17 00:00:00 2001 From: EinTim23 Date: Tue, 5 Nov 2024 23:31:38 +0100 Subject: [PATCH] implemented linux autostart --- src/backends/linux.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/backends/linux.cpp b/src/backends/linux.cpp index e54f60d..e122315 100644 --- a/src/backends/linux.cpp +++ b/src/backends/linux.cpp @@ -1,12 +1,20 @@ #if !defined(_WIN32) && !defined(__APPLE__) #include +#include +#include #include #include "../backend.hpp" DBusConnection* conn = nullptr; +std::string getExecutablePath() { + char result[PATH_MAX]; + ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); + return (count != -1) ? std::string(result, count) : std::string(); +} + std::string getActivePlayer(DBusConnection* conn) { DBusMessage* msg; DBusMessageIter args; @@ -219,8 +227,40 @@ std::shared_ptr backend::getMediaInformation() { if (player == "") return nullptr; getNowPlaying(conn, player); + std::cout << "isPaused: " << isPlayerPaused(conn, player) << " \n"; return nullptr; } -bool backend::toggleAutostart(bool enabled) { return false; } +bool backend::toggleAutostart(bool enabled) { + const char* xdgHome = std::getenv("XDG_CONFIG_HOME"); + + const char* home = std::getenv("HOME"); + + if (!xdgHome && !home) + return false; + + std::string realxdgHome = xdgHome ? xdgHome : home; + std::filesystem::path xdgAutostartPath = realxdgHome; + if (!xdgHome) + xdgAutostartPath = xdgAutostartPath / ".config"; + xdgAutostartPath = xdgAutostartPath / "autostart"; + + std::filesystem::create_directories(xdgAutostartPath); + + std::filesystem::path desktopPath = xdgAutostartPath / "PlayerLink.desktop"; + + if (!enabled && std::filesystem::exists(desktopPath)) { + std::filesystem::remove(desktopPath); + return true; + } + + std::string formattedPlist = "[Desktop Entry]\nType=Application\nName=PlayerLink\nExec=" + getExecutablePath() + + "\nX-GNOME-Autostart-enabled=true\n"; + std::ofstream o(desktopPath); + o.write(formattedPlist.c_str(), formattedPlist.size()); + o.close(); + return true; +} + +#undef XDG_AUTOSTART_TEMPLATE #endif \ No newline at end of file