mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Sound (Windows): add support
This commit is contained in:
parent
11634b9a67
commit
dd9047c4e9
@ -502,7 +502,7 @@ elseif(WIN32)
|
||||
src/detection/packages/packages_windows.c
|
||||
src/detection/poweradapter/poweradapter_nosupport.c
|
||||
src/detection/processes/processes_windows.cpp
|
||||
src/detection/sound/sound_nosupport.c
|
||||
src/detection/sound/sound_windows.cpp
|
||||
src/detection/swap/swap_windows.cpp
|
||||
src/detection/terminalfont/terminalfont_windows.c
|
||||
src/detection/terminalshell/terminalshell_windows.cpp
|
||||
|
87
src/detection/sound/sound_windows.cpp
Normal file
87
src/detection/sound/sound_windows.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
extern "C" {
|
||||
#include "sound.h"
|
||||
#include "util/windows/unicode.h"
|
||||
}
|
||||
#include "util/windows/com.hpp"
|
||||
|
||||
#include <initguid.h>
|
||||
#include <mmdeviceapi.h>
|
||||
#include <endpointvolume.h>
|
||||
#include <functiondiscoverykeys_devpkey.h>
|
||||
|
||||
const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_UNUSED FFlist* devices /* List of FFSoundDevice */)
|
||||
{
|
||||
const char* error = ffInitCom();
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
IMMDeviceEnumerator* FF_AUTO_RELEASE_COM_OBJECT pEnum = NULL;
|
||||
|
||||
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void **)&pEnum)))
|
||||
return "CoCreateInstance(__uuidof(MMDeviceEnumerator)) failed";
|
||||
|
||||
LPWSTR mainDeviceId = NULL;
|
||||
|
||||
{
|
||||
IMMDevice* FF_AUTO_RELEASE_COM_OBJECT pDefaultDevice = NULL;
|
||||
|
||||
if (FAILED(pEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDefaultDevice)))
|
||||
return "GetDefaultAudioEndpoint() failed";
|
||||
|
||||
if (FAILED(pDefaultDevice->GetId(&mainDeviceId)))
|
||||
return "pDefaultDevice->GetId() failed";
|
||||
}
|
||||
|
||||
IMMDeviceCollection* FF_AUTO_RELEASE_COM_OBJECT pDevices = NULL;
|
||||
|
||||
if (FAILED(pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDevices)))
|
||||
return "EnumAudioEndpoints() failed";
|
||||
|
||||
uint32_t deviceCount;
|
||||
if (FAILED(pDevices->GetCount(&deviceCount)))
|
||||
return "pDevices->GetCount() failed";
|
||||
|
||||
for (uint32_t deviceIdx = 0; deviceIdx < deviceCount; ++deviceIdx)
|
||||
{
|
||||
IMMDevice* FF_AUTO_RELEASE_COM_OBJECT immDevice = NULL;
|
||||
if (FAILED(pDevices->Item(deviceIdx, &immDevice)))
|
||||
continue;
|
||||
|
||||
LPWSTR immDeviceId = NULL;
|
||||
if (FAILED(immDevice->GetId(&immDeviceId)))
|
||||
continue;
|
||||
|
||||
IPropertyStore* FF_AUTO_RELEASE_COM_OBJECT immPropStore;
|
||||
if (FAILED(immDevice->OpenPropertyStore(STGM_READ, &immPropStore)))
|
||||
continue;
|
||||
|
||||
FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices);
|
||||
device->main = wcscmp(mainDeviceId, immDeviceId) == 0;
|
||||
device->volume = 0;
|
||||
ffStrbufInit(&device->name);
|
||||
ffStrbufInit(&device->manufacturer);
|
||||
|
||||
{
|
||||
PROPVARIANT __attribute__((__cleanup__(PropVariantClear))) friendlyName;
|
||||
PropVariantInit(&friendlyName);
|
||||
if (SUCCEEDED(immPropStore->GetValue(PKEY_Device_DeviceDesc, &friendlyName)))
|
||||
ffStrbufSetWS(&device->name, friendlyName.pwszVal);
|
||||
if (SUCCEEDED(immPropStore->GetValue(PKEY_DeviceInterface_FriendlyName, &friendlyName)))
|
||||
ffStrbufSetWS(&device->manufacturer, friendlyName.pwszVal);
|
||||
}
|
||||
|
||||
IAudioEndpointVolume* FF_AUTO_RELEASE_COM_OBJECT immEndpointVolume;
|
||||
if(SUCCEEDED(immDevice->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**) &immEndpointVolume)))
|
||||
{
|
||||
BOOL muted;
|
||||
if (FAILED(immEndpointVolume->GetMute(&muted)) || !muted)
|
||||
{
|
||||
FLOAT volume;
|
||||
if (SUCCEEDED(immEndpointVolume->GetMasterVolumeLevelScalar(&volume)))
|
||||
device->volume = (uint8_t) (volume * 100 + 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user