diff --git a/rpc.exe b/rpc.exe deleted file mode 100644 index d155135..0000000 Binary files a/rpc.exe and /dev/null differ diff --git a/src/backend.hpp b/src/backend.hpp index 4f7f3f7..b770593 100644 --- a/src/backend.hpp +++ b/src/backend.hpp @@ -27,6 +27,7 @@ struct MediaInfo { }; namespace backend { + bool toggleAutostart(bool enabled); std::shared_ptr getMediaInformation(); } diff --git a/src/backends/darwin.cpp b/src/backends/darwin.cpp index 99a8a1a..1098a03 100644 --- a/src/backends/darwin.cpp +++ b/src/backends/darwin.cpp @@ -1,5 +1,5 @@ #ifdef __APPLE__ #include "../backend.hpp" std::shared_ptr backend::getMediaInformation() { return nullptr; } - +bool backend::toggleAutostart(bool enabled) { return false, } #endif \ No newline at end of file diff --git a/src/backends/linux.cpp b/src/backends/linux.cpp index 2d34e5d..2bad487 100644 --- a/src/backends/linux.cpp +++ b/src/backends/linux.cpp @@ -1,5 +1,5 @@ #if !defined(_WIN32) && !defined(__APPLE__) #include "../backend.hpp" std::shared_ptr backend::getMediaInformation() { return nullptr; } - +bool backend::toggleAutostart(bool enabled) { return false, } #endif \ No newline at end of file diff --git a/src/backends/windows.cpp b/src/backends/windows.cpp index 0cd1e1c..6d73010 100644 --- a/src/backends/windows.cpp +++ b/src/backends/windows.cpp @@ -1,10 +1,15 @@ #ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#include #include #include #include #include #include +#include #include "../backend.hpp" #include "../utils.hpp" @@ -13,12 +18,54 @@ using namespace winrt; using namespace Windows::Media::Control; using namespace Windows::Storage::Streams; #define EM_DASH "\xE2\x80\x94" -// codecvt is deprecated, but there is no good portable way to do this, I could technically use the winapi as this is the windows backend tho +// codecvt is deprecated, but there is no good portable way to do this, I could technically use the winapi as this is +// the windows backend tho std::string toStdString(winrt::hstring in) { std::wstring_convert> converter; return converter.to_bytes(in.c_str()); } +bool CreateShortcut(std::string source, std::string target) { + CoInitialize(nullptr); + WCHAR src[MAX_PATH]; + IShellLinkW* pShellLink = nullptr; + HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, + reinterpret_cast(&pShellLink)); + + if (SUCCEEDED(hr) && pShellLink) { + MultiByteToWideChar(CP_ACP, 0, source.c_str(), -1, src, MAX_PATH); + pShellLink->SetPath(src); + + IPersistFile* pPersistFile = nullptr; + hr = pShellLink->QueryInterface(IID_IPersistFile, reinterpret_cast(&pPersistFile)); + + if (SUCCEEDED(hr) && pPersistFile) { + WCHAR dst[MAX_PATH]; + MultiByteToWideChar(CP_ACP, 0, target.c_str(), -1, dst, MAX_PATH); + hr = pPersistFile->Save(dst, TRUE); + pPersistFile->Release(); + } + + pShellLink->Release(); + } + + CoUninitialize(); + return SUCCEEDED(hr); +} + +bool backend::toggleAutostart(bool enabled) { + std::filesystem::path shortcutPath = std::getenv("APPDATA"); + shortcutPath = shortcutPath / "Microsoft" / "Windows" / "Start Menu" / "Programs" / "Startup" / "PlayerLink.lnk"; + if (!enabled && std::filesystem::exists(shortcutPath)) { + std::filesystem::remove(shortcutPath); + return true; + } + char binaryPath[MAX_PATH]{}; + GetModuleFileNameA(NULL, binaryPath, MAX_PATH); + bool result = CreateShortcut(binaryPath, shortcutPath.string()); + return result; +} + std::shared_ptr backend::getMediaInformation() { auto sessionManager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get(); auto currentSession = sessionManager.GetCurrentSession(); diff --git a/src/main.cpp b/src/main.cpp index 5495bc8..e5f1ed2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -239,6 +239,7 @@ public: bool isChecked = this->autostartCheckbox->IsChecked(); auto settings = utils::getSettings(); settings.autoStart = isChecked; + backend::toggleAutostart(isChecked); utils::saveSettings(settings); });