diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 5149af534..2a85b1539 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -32,7 +32,7 @@ if(NOT SAILFISHOS) add_subdirectory(sftp) endif() - if(KF5PulseAudioQt_FOUND) + if(KF5PulseAudioQt_FOUND OR WIN32) add_subdirectory(pausemusic) endif() diff --git a/plugins/pausemusic/CMakeLists.txt b/plugins/pausemusic/CMakeLists.txt index af9976376..26aa7a874 100644 --- a/plugins/pausemusic/CMakeLists.txt +++ b/plugins/pausemusic/CMakeLists.txt @@ -1,11 +1,25 @@ -kdeconnect_add_plugin(kdeconnect_pausemusic JSON kdeconnect_pausemusic.json SOURCES pausemusicplugin.cpp) +if(WIN32) + set(kdeconnect_pausemusic_SRCS + pausemusicplugin-win.cpp + ) +else() + set(kdeconnect_pausemusic_SRCS + pausemusicplugin.cpp + ) +endif() + +kdeconnect_add_plugin(kdeconnect_pausemusic JSON kdeconnect_pausemusic.json SOURCES ${kdeconnect_pausemusic_SRCS}) target_link_libraries(kdeconnect_pausemusic kdeconnectcore Qt5::Core Qt5::DBus - KF5::PulseAudioQt ) +if (NOT WIN32) + target_link_libraries(kdeconnect_pausemusic + KF5::PulseAudioQt + ) +endif() ####################################### # Config diff --git a/plugins/pausemusic/pausemusicplugin-win.cpp b/plugins/pausemusic/pausemusicplugin-win.cpp new file mode 100644 index 000000000..37e7f9672 --- /dev/null +++ b/plugins/pausemusic/pausemusicplugin-win.cpp @@ -0,0 +1,103 @@ +/** + * Copyright 2013 Albert Vaca + * Copyright 2019 Piyush Aggarwal + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "pausemusicplugin-win.h" + +#include + +K_PLUGIN_CLASS_WITH_JSON(PauseMusicPlugin, "kdeconnect_pausemusic.json") + +Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_PAUSEMUSIC, "kdeconnect.plugin.pausemusic") + +PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args) + : KdeConnectPlugin(parent, args) +{ + CoInitialize(NULL); + deviceEnumerator = NULL; + CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator); + defaultDevice = NULL; + g_guidMyContext = GUID_NULL; + + deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); + deviceEnumerator->Release(); + deviceEnumerator = NULL; + + endpointVolume = NULL; + defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume); + defaultDevice->Release(); + defaultDevice = NULL; + CoCreateGuid(&g_guidMyContext); +} + +PauseMusicPlugin::~PauseMusicPlugin() +{ + endpointVolume->Release(); + CoUninitialize(); +} + +bool PauseMusicPlugin::receivePacket(const NetworkPacket& np) +{ + + bool pauseOnlyWhenTalking = config()->get(QStringLiteral("conditionTalking"), false); + + if (pauseOnlyWhenTalking) { + if (np.get(QStringLiteral("event")) != QLatin1String("talking")) { + return true; + } + } else { + if (np.get(QStringLiteral("event")) != QLatin1String("ringing") + && np.get(QStringLiteral("event")) != QLatin1String("talking")) { + return true; + } + } + + bool pauseConditionFulfilled = !np.get(QStringLiteral("isCancel")); + + bool pause = config()->get(QStringLiteral("actionPause"), false); + bool mute = config()->get(QStringLiteral("actionMute"), true); + + if (pauseConditionFulfilled) { + + if (mute) { + qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting music"; + endpointVolume->SetMute(TRUE, &g_guidMyContext); + } + + if (pause) { + // TODO PAUSING + } + + } else { + + if (mute) { + qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting system volume"; + endpointVolume->SetMute(FALSE, &g_guidMyContext); + } + if (pause) { + // TODO UNPAUSING + } + } + + return true; + +} + +#include "pausemusicplugin-win.moc" diff --git a/plugins/pausemusic/pausemusicplugin-win.h b/plugins/pausemusic/pausemusicplugin-win.h new file mode 100644 index 000000000..cf9e26825 --- /dev/null +++ b/plugins/pausemusic/pausemusicplugin-win.h @@ -0,0 +1,58 @@ +/** + * Copyright 2013 Albert Vaca + * Copyright 2019 Piyush Aggarwal + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef PAUSEMUSICPLUGINWIN_H +#define PAUSEMUSICPLUGINWIN_H + +#include +#include +#include + +#include + +#include + +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_PAUSEMUSIC) + +class PauseMusicPlugin + : public KdeConnectPlugin +{ + Q_OBJECT + +public: + explicit PauseMusicPlugin(QObject* parent, const QVariantList& args); + ~PauseMusicPlugin(); + + bool receivePacket(const NetworkPacket& np) override; + void connected() override { } + +private: + IMMDeviceEnumerator *deviceEnumerator; + IMMDevice *defaultDevice; + GUID g_guidMyContext; + IAudioEndpointVolume *endpointVolume; + +}; + +#endif // PAUSEMUSICPLUGINWIN_H \ No newline at end of file