2019-02-03 00:50:36 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2018 Jun Bo Bi <jambonmcyeah@gmail.com>
|
2019-02-03 00:50:36 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-02-03 00:50:36 +00:00
|
|
|
*/
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2019-02-03 00:50:36 +00:00
|
|
|
#include "systemvolumeplugin-win.h"
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
2019-02-03 00:50:36 +00:00
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2018-11-20 00:14:17 +00:00
|
|
|
#include <Functiondiscoverykeys_devpkey.h>
|
|
|
|
|
2019-02-03 00:50:36 +00:00
|
|
|
#include <core/device.h>
|
|
|
|
|
2020-11-24 14:58:33 +00:00
|
|
|
#include "PolicyConfig.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "plugin_systemvolume_debug.h"
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(SystemvolumePlugin, "kdeconnect_systemvolume.json")
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
// Private classes of SystemvolumePlugin
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
class SystemvolumePlugin::CAudioEndpointVolumeCallback : public IAudioEndpointVolumeCallback
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
2020-11-19 19:54:25 +00:00
|
|
|
LONG _cRef;
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
public:
|
2022-09-10 22:23:52 +01:00
|
|
|
CAudioEndpointVolumeCallback(SystemvolumePlugin &x, QString sinkName)
|
|
|
|
: enclosing(x)
|
|
|
|
, name(std::move(sinkName))
|
|
|
|
, _cRef(1)
|
|
|
|
{
|
|
|
|
}
|
2024-06-04 18:02:58 +01:00
|
|
|
~CAudioEndpointVolumeCallback() {};
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
// IUnknown methods -- AddRef, Release, and QueryInterface
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE AddRef() override
|
|
|
|
{
|
|
|
|
return InterlockedIncrement(&_cRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE Release() override
|
|
|
|
{
|
|
|
|
ULONG ulRef = InterlockedDecrement(&_cRef);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (ulRef == 0) {
|
2018-11-20 00:14:17 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return ulRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (IID_IUnknown == riid) {
|
2018-11-20 00:14:17 +00:00
|
|
|
AddRef();
|
|
|
|
*ppvInterface = (IUnknown *)this;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else if (__uuidof(IMMNotificationClient) == riid) {
|
2018-11-20 00:14:17 +00:00
|
|
|
AddRef();
|
|
|
|
*ppvInterface = (IMMNotificationClient *)this;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else {
|
2018-11-20 00:14:17 +00:00
|
|
|
*ppvInterface = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
// Callback method for endpoint-volume-change notifications.
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) override
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
2020-11-19 19:54:25 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_SYSTEMVOLUME);
|
|
|
|
np.set<int>(QStringLiteral("volume"), (int)(pNotify->fMasterVolume * 100));
|
|
|
|
np.set<bool>(QStringLiteral("muted"), pNotify->bMuted);
|
|
|
|
np.set<QString>(QStringLiteral("name"), name);
|
|
|
|
enclosing.sendPacket(np);
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
private:
|
2018-11-20 00:14:17 +00:00
|
|
|
SystemvolumePlugin &enclosing;
|
2020-11-19 19:54:25 +00:00
|
|
|
QString name;
|
2018-11-20 00:14:17 +00:00
|
|
|
};
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
class SystemvolumePlugin::CMMNotificationClient : public IMMNotificationClient
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
2020-11-19 19:54:25 +00:00
|
|
|
public:
|
2022-09-10 22:23:52 +01:00
|
|
|
CMMNotificationClient(SystemvolumePlugin &x)
|
|
|
|
: enclosing(x)
|
2024-06-04 18:02:58 +01:00
|
|
|
, _cRef(1) {};
|
2020-11-19 19:54:25 +00:00
|
|
|
|
2024-06-04 18:02:58 +01:00
|
|
|
~CMMNotificationClient() {};
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
// IUnknown methods -- AddRef, Release, and QueryInterface
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE AddRef() override
|
|
|
|
{
|
|
|
|
return InterlockedIncrement(&_cRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG STDMETHODCALLTYPE Release() override
|
|
|
|
{
|
|
|
|
ULONG ulRef = InterlockedDecrement(&_cRef);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (ulRef == 0) {
|
2018-11-20 00:14:17 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return ulRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) override
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (IID_IUnknown == riid) {
|
2018-11-20 00:14:17 +00:00
|
|
|
AddRef();
|
|
|
|
*ppvInterface = (IUnknown *)this;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else if (__uuidof(IMMNotificationClient) == riid) {
|
2018-11-20 00:14:17 +00:00
|
|
|
AddRef();
|
|
|
|
*ppvInterface = (IMMNotificationClient *)this;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else {
|
2018-11-20 00:14:17 +00:00
|
|
|
*ppvInterface = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
// Callback methods for device-event notifications.
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) override
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (flow == eRender) {
|
2020-11-19 19:54:25 +00:00
|
|
|
enclosing.sendSinkList();
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) override
|
|
|
|
{
|
|
|
|
enclosing.sendSinkList();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RemovedDeviceThreadData {
|
|
|
|
QString qDeviceId;
|
2022-09-10 22:23:52 +01:00
|
|
|
SystemvolumePlugin *plugin;
|
2020-11-19 19:54:25 +00:00
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static DWORD WINAPI releaseRemovedDevice(_In_ LPVOID lpParameter)
|
|
|
|
{
|
|
|
|
auto *data = static_cast<RemovedDeviceThreadData *>(lpParameter);
|
2020-11-19 19:54:25 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!data->plugin->sinkList.empty()) {
|
2020-11-19 19:54:25 +00:00
|
|
|
auto idToNameIterator = data->plugin->idToNameMap.find(data->qDeviceId);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (idToNameIterator == data->plugin->idToNameMap.end())
|
|
|
|
return 0;
|
2020-11-19 19:54:25 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString &sinkName = idToNameIterator.value();
|
2020-11-19 19:54:25 +00:00
|
|
|
|
|
|
|
auto sinkListIterator = data->plugin->sinkList.find(sinkName);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (sinkListIterator == data->plugin->sinkList.end())
|
|
|
|
return 0;
|
2020-11-19 19:54:25 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
auto &sink = sinkListIterator.value();
|
2020-11-19 19:54:25 +00:00
|
|
|
|
|
|
|
sink.first->UnregisterControlChangeNotify(sink.second);
|
|
|
|
sink.first->Release();
|
|
|
|
sink.second->Release();
|
|
|
|
|
|
|
|
data->plugin->idToNameMap.remove(data->qDeviceId);
|
|
|
|
data->plugin->sinkList.remove(sinkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->plugin->sendSinkList();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) override
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
static RemovedDeviceThreadData data{};
|
2020-11-19 19:54:25 +00:00
|
|
|
data.qDeviceId = QString::fromWCharArray(pwstrDeviceId);
|
|
|
|
data.plugin = &enclosing;
|
|
|
|
|
|
|
|
DWORD threadId;
|
|
|
|
HANDLE threadHandle = CreateThread(NULL, 0, releaseRemovedDevice, &data, 0, &threadId);
|
|
|
|
CloseHandle(threadHandle);
|
|
|
|
|
2023-05-25 15:59:41 +01:00
|
|
|
enclosing.sendSinkList();
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) override
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (dwNewState == DEVICE_STATE_UNPLUGGED)
|
|
|
|
return OnDeviceRemoved(pwstrDeviceId);
|
2020-11-19 19:54:25 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) override
|
|
|
|
{
|
2023-05-25 15:59:41 +01:00
|
|
|
// This callback is supper spammy. Care only about name and description changes.
|
|
|
|
if (IsEqualPropertyKey(key, PKEY_Device_FriendlyName)) {
|
|
|
|
enclosing.sendSinkList();
|
|
|
|
}
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
else if (IsEqualPropertyKey(key, PKEY_Device_DeviceDesc)) {
|
|
|
|
enclosing.sendSinkList();
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-20 00:14:17 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:54:25 +00:00
|
|
|
private:
|
|
|
|
LONG _cRef;
|
2018-11-20 00:14:17 +00:00
|
|
|
SystemvolumePlugin &enclosing;
|
|
|
|
};
|
|
|
|
|
|
|
|
SystemvolumePlugin::SystemvolumePlugin(QObject *parent, const QVariantList &args)
|
2022-09-10 22:23:52 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
, sinkList()
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
|
|
|
CoInitialize(nullptr);
|
|
|
|
deviceEnumerator = nullptr;
|
|
|
|
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&(deviceEnumerator));
|
|
|
|
valid = (hr == S_OK);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!valid) {
|
2018-11-20 00:14:17 +00:00
|
|
|
qWarning("Initialization failed: Failed to create MMDeviceEnumerator");
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemvolumePlugin::~SystemvolumePlugin()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (valid) {
|
2023-05-25 12:02:16 +01:00
|
|
|
for (auto &entry : sinkList) {
|
|
|
|
entry.first->UnregisterControlChangeNotify(entry.second);
|
|
|
|
entry.second->Release();
|
|
|
|
entry.first->Release();
|
|
|
|
}
|
2018-11-20 00:14:17 +00:00
|
|
|
deviceEnumerator->UnregisterEndpointNotificationCallback(deviceCallback);
|
|
|
|
deviceEnumerator->Release();
|
|
|
|
deviceEnumerator = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemvolumePlugin::sendSinkList()
|
|
|
|
{
|
|
|
|
if (!valid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QJsonDocument document;
|
|
|
|
QJsonArray array;
|
|
|
|
|
|
|
|
IMMDeviceCollection *devices = nullptr;
|
2020-11-19 19:54:25 +00:00
|
|
|
HRESULT hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (hr != S_OK) {
|
2018-11-20 00:14:17 +00:00
|
|
|
qWarning("Failed to Enumumerate AudioEndpoints");
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned int deviceCount;
|
|
|
|
devices->GetCount(&deviceCount);
|
2020-11-23 18:44:47 +00:00
|
|
|
|
2022-09-17 13:54:11 +01:00
|
|
|
if (!deviceCount) {
|
|
|
|
qWarning("No audio devices detected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-23 18:44:47 +00:00
|
|
|
IMMDevice *defaultDevice = nullptr;
|
|
|
|
deviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &defaultDevice);
|
|
|
|
|
2022-09-17 13:54:11 +01:00
|
|
|
if (!defaultDevice) {
|
|
|
|
qWarning("No default audio device detected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-23 18:44:47 +00:00
|
|
|
LPWSTR defaultId = NULL;
|
|
|
|
defaultDevice->GetId(&defaultId);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (unsigned int i = 0; i < deviceCount; i++) {
|
2018-11-20 00:14:17 +00:00
|
|
|
IMMDevice *device = nullptr;
|
|
|
|
|
|
|
|
IPropertyStore *deviceProperties = nullptr;
|
|
|
|
PROPVARIANT deviceProperty;
|
|
|
|
QString name;
|
|
|
|
QString desc;
|
2020-11-19 19:54:25 +00:00
|
|
|
LPWSTR deviceId;
|
2018-11-20 00:14:17 +00:00
|
|
|
float volume;
|
|
|
|
BOOL muted;
|
|
|
|
|
|
|
|
IAudioEndpointVolume *endpoint = nullptr;
|
|
|
|
CAudioEndpointVolumeCallback *callback;
|
|
|
|
|
|
|
|
// Get Properties
|
|
|
|
devices->Item(i, &device);
|
|
|
|
device->OpenPropertyStore(STGM_READ, &deviceProperties);
|
|
|
|
|
|
|
|
deviceProperties->GetValue(PKEY_Device_FriendlyName, &deviceProperty);
|
|
|
|
name = QString::fromWCharArray(deviceProperty.pwszVal);
|
2023-05-25 15:06:41 +01:00
|
|
|
PropVariantClear(&deviceProperty);
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2019-04-30 18:09:26 +01:00
|
|
|
#ifndef __MINGW32__
|
2018-11-20 00:14:17 +00:00
|
|
|
deviceProperties->GetValue(PKEY_Device_DeviceDesc, &deviceProperty);
|
|
|
|
desc = QString::fromWCharArray(deviceProperty.pwszVal);
|
2023-05-25 15:06:41 +01:00
|
|
|
PropVariantClear(&deviceProperty);
|
2021-06-21 06:48:53 +01:00
|
|
|
#else
|
|
|
|
desc = name;
|
2019-04-30 18:09:26 +01:00
|
|
|
#endif
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
QJsonObject sinkObject;
|
2019-06-10 15:40:28 +01:00
|
|
|
sinkObject.insert(QStringLiteral("name"), name);
|
|
|
|
sinkObject.insert(QStringLiteral("description"), desc);
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void **)&endpoint);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (hr != S_OK) {
|
2018-11-20 00:14:17 +00:00
|
|
|
qWarning() << "Failed to create IAudioEndpointVolume for device:" << name;
|
|
|
|
qWarning("Error Code: %lx", hr);
|
|
|
|
|
|
|
|
device->Release();
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-19 19:54:25 +00:00
|
|
|
|
|
|
|
device->GetId(&deviceId);
|
2018-11-20 00:14:17 +00:00
|
|
|
endpoint->GetMasterVolumeLevelScalar(&volume);
|
|
|
|
endpoint->GetMute(&muted);
|
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
sinkObject.insert(QStringLiteral("muted"), (bool)muted);
|
|
|
|
sinkObject.insert(QStringLiteral("volume"), (qint64)(volume * 100));
|
|
|
|
sinkObject.insert(QStringLiteral("maxVolume"), (qint64)100);
|
2020-11-23 18:44:47 +00:00
|
|
|
sinkObject.insert(QStringLiteral("enabled"), lstrcmpW(deviceId, defaultId) == 0);
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
// Register Callback
|
2020-11-24 15:07:37 +00:00
|
|
|
if (!sinkList.contains(name)) {
|
2020-11-19 19:54:25 +00:00
|
|
|
callback = new CAudioEndpointVolumeCallback(*this, name);
|
|
|
|
endpoint->RegisterControlChangeNotify(callback);
|
|
|
|
sinkList[name] = qMakePair(endpoint, callback);
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:07:37 +00:00
|
|
|
QString qDeviceId = QString::fromWCharArray(deviceId);
|
2020-11-19 19:54:25 +00:00
|
|
|
idToNameMap[qDeviceId] = name;
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
device->Release();
|
|
|
|
array.append(sinkObject);
|
|
|
|
}
|
|
|
|
devices->Release();
|
2020-11-23 18:44:47 +00:00
|
|
|
defaultDevice->Release();
|
2018-11-20 00:14:17 +00:00
|
|
|
|
|
|
|
document.setArray(array);
|
|
|
|
|
|
|
|
NetworkPacket np(PACKET_TYPE_SYSTEMVOLUME);
|
|
|
|
np.set<QJsonDocument>(QStringLiteral("sinkList"), document);
|
|
|
|
sendPacket(np);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemvolumePlugin::connected()
|
|
|
|
{
|
|
|
|
if (!valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
deviceCallback = new CMMNotificationClient(*this);
|
|
|
|
deviceEnumerator->RegisterEndpointNotificationCallback(deviceCallback);
|
|
|
|
sendSinkList();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static HRESULT setDefaultAudioPlaybackDevice(PCWSTR deviceId)
|
|
|
|
{
|
|
|
|
if (deviceId == nullptr)
|
|
|
|
return ERROR_BAD_UNIT;
|
2020-11-24 14:58:33 +00:00
|
|
|
|
|
|
|
IPolicyConfigVista *pPolicyConfig;
|
2022-09-10 22:23:52 +01:00
|
|
|
HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient), NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID *)&pPolicyConfig);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
2020-11-24 14:58:33 +00:00
|
|
|
hr = pPolicyConfig->SetDefaultEndpoint(deviceId, eMultimedia);
|
|
|
|
pPolicyConfig->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
HRESULT SystemvolumePlugin::setDefaultAudioPlaybackDevice(QString &name, bool enabled)
|
2020-11-24 14:58:33 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!enabled)
|
|
|
|
return S_OK;
|
2020-11-24 14:58:33 +00:00
|
|
|
|
|
|
|
PWSTR deviceId = nullptr;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (auto &entry : idToNameMap.toStdMap()) {
|
2020-11-24 14:58:33 +00:00
|
|
|
if (entry.second == name) {
|
|
|
|
deviceId = new WCHAR[entry.first.length()];
|
|
|
|
wcscpy(deviceId, entry.first.toStdWString().data());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (deviceId == nullptr)
|
|
|
|
return ERROR_BAD_UNIT;
|
2020-11-24 14:58:33 +00:00
|
|
|
|
|
|
|
HRESULT hr = ::setDefaultAudioPlaybackDevice(deviceId);
|
|
|
|
|
|
|
|
delete[] deviceId;
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void SystemvolumePlugin::receivePacket(const NetworkPacket &np)
|
2018-11-20 00:14:17 +00:00
|
|
|
{
|
|
|
|
if (!valid)
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2018-11-20 00:14:17 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("requestSinks"))) {
|
2023-07-31 08:25:45 +01:00
|
|
|
sendSinkList();
|
2022-09-10 22:23:52 +01:00
|
|
|
} else {
|
2018-11-20 00:14:17 +00:00
|
|
|
QString name = np.get<QString>(QStringLiteral("name"));
|
|
|
|
|
2023-05-25 12:08:10 +01:00
|
|
|
auto sinkListIterator = this->sinkList.find(name);
|
|
|
|
if (sinkListIterator != this->sinkList.end()) {
|
2021-06-20 17:52:08 +01:00
|
|
|
auto &sink = sinkListIterator.value();
|
2023-05-25 12:08:10 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("volume"))) {
|
2021-06-20 17:52:08 +01:00
|
|
|
float requestedVolume = (float)np.get<int>(QStringLiteral("volume"), 100) / 100;
|
2023-05-25 14:56:52 +01:00
|
|
|
sinkList[name].first->SetMasterVolumeLevelScalar(requestedVolume, NULL);
|
2018-11-20 00:14:17 +00:00
|
|
|
}
|
2021-06-20 17:52:08 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("muted"))) {
|
2021-06-20 17:52:08 +01:00
|
|
|
BOOL requestedMuteStatus = np.get<bool>(QStringLiteral("muted"), false);
|
2023-05-25 14:56:52 +01:00
|
|
|
sinkList[name].first->SetMute(requestedMuteStatus, NULL);
|
2018-11-20 00:14:17 +00:00
|
|
|
}
|
2021-06-20 17:52:08 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("enabled"))) {
|
2021-06-20 17:52:08 +01:00
|
|
|
// get the current default device ID
|
|
|
|
IMMDevice *defaultDevice = nullptr;
|
|
|
|
deviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &defaultDevice);
|
|
|
|
LPWSTR defaultId = NULL;
|
|
|
|
defaultDevice->GetId(&defaultId);
|
|
|
|
defaultDevice->Release();
|
|
|
|
// get current sink's device ID
|
|
|
|
QString qDefaultId = QString::fromWCharArray(defaultId);
|
|
|
|
QString currentDeviceId = idToNameMap.key(name);
|
2023-05-25 15:20:37 +01:00
|
|
|
if (qDefaultId != currentDeviceId) {
|
2021-06-20 17:52:08 +01:00
|
|
|
setDefaultAudioPlaybackDevice(name, np.get<bool>(QStringLiteral("enabled")));
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 00:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_systemvolumeplugin-win.cpp"
|
2019-02-03 00:50:36 +00:00
|
|
|
#include "systemvolumeplugin-win.moc"
|