/* * Strawberry Music Player * Copyright 2023, Jonas Kvinge * * Strawberry 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 3 of the License, or * (at your option) any later version. * * Strawberry 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 Strawberry. If not, see . * */ #include #include #include #include #include #include #include #include #include #include "AsyncOperations.h" #include "uwpdevicefinder.h" #include "core/logging.h" using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace ABI::Windows::Foundation; using namespace ABI::Windows::Foundation::Collections; using namespace ABI::Windows::Devices::Enumeration; UWPDeviceFinder::UWPDeviceFinder() : DeviceFinder("uwpdevice", { "wasapi2sink" }) {} namespace { static std::string wstring_to_stdstring(const std::wstring &wstr) { std::wstring_convert, wchar_t> converter; return converter.to_bytes(wstr.c_str()); } static std::string hstring_to_stdstring(HString *hstr) { if (!hstr) { return std::string(); } const wchar_t *raw_hstr = hstr->GetRawBuffer(nullptr); if (!raw_hstr) { return std::string(); } return wstring_to_stdstring(std::wstring(raw_hstr)); } } // namespace DeviceFinder::DeviceList UWPDeviceFinder::ListDevices() { ComPtr device_info_statics; HRESULT hr = ABI::Windows::Foundation::GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Enumeration_DeviceInformation).Get(), &device_info_statics); if (FAILED(hr)) { return DeviceList(); } ComPtr> async_op; hr = device_info_statics->FindAllAsyncDeviceClass(DeviceClass::DeviceClass_AudioRender, &async_op); device_info_statics.Reset(); if (FAILED(hr)) { return DeviceList(); } hr = SyncWait(async_op.Get()); if (FAILED(hr)) { return DeviceList(); } ComPtr> device_list; hr = async_op->GetResults(&device_list); async_op.Reset(); if (FAILED(hr)) { return DeviceList(); } unsigned int count = 0; hr = device_list->get_Size(&count); if (FAILED(hr)) { return DeviceList(); } DeviceList devices; { Device default_device; default_device.description = "Default device"; default_device.iconname = GuessIconName(default_device.description); devices.append(default_device); } for (unsigned int i = 0; i < count; i++) { ComPtr device_info; hr = device_list->GetAt(i, &device_info); if (FAILED(hr)) { continue; } boolean enabled; hr = device_info->get_IsEnabled(&enabled); if (FAILED(hr) || !enabled) { continue; } HString id; hr = device_info->get_Id(id.GetAddressOf()); if (FAILED(hr) || !id.IsValid()) { continue; } HString name; hr = device_info->get_Name(name.GetAddressOf()); if (FAILED(hr) || !name.IsValid()) { continue; } Device device; device.value = QString::fromStdString(hstring_to_stdstring(&id)); device.description = QString::fromStdString(hstring_to_stdstring(&name)); device.iconname = GuessIconName(device.description); devices.append(device); } return devices; }