ref: 38c6960e62514a0ec67bc48661b911999ef513e5
parent: c3a0c733a20c3fddb9f2f8afcf0714eaf2da8643
author: Snesrev <[email protected]>
date: Tue Sep 6 09:17:29 EDT 2022
Fix code style of platform/win32/volume_control.c - It would call Release on invalid pointers - Don't call CoUnitialize with COM objects still being used - Formatting
--- a/platform/win32/volume_control.c
+++ b/platform/win32/volume_control.c
@@ -10,78 +10,63 @@
#include <audiopolicy.h>
#include <mmdeviceapi.h>
-HRESULT GetSimpleAudioVolume(ISimpleAudioVolume **simple_audio_volume);
+static ISimpleAudioVolume *GetSimpleAudioVolume();
DEFINE_GUID(IID_IMMDeviceEnumerator, 0XA95664D2, 0X9614, 0X4F35, 0XA7, 0X46, 0XDE, 0X8D, 0XB6, 0X36, 0X17, 0XE6);
DEFINE_GUID(CLSID_MMDeviceEnumerator, 0XBCDE0395, 0XE52F, 0X467C, 0X8E, 0X3D, 0XC4, 0X57, 0X92, 0X91, 0X69, 0X2E);
DEFINE_GUID(IID_IAudioSessionManager, 0X77AA99A0, 0X1BD6, 0X484F, 0X8B, 0XC7, 0X2C, 0X65, 0X4C, 0X9A, 0X9B, 0X6F);
+static void InitializeCom() {
+ static bool com_initialized;
+ if (!com_initialized)
+ com_initialized = SUCCEEDED(CoInitialize(NULL));
+}
+
bool SetApplicationVolume(int volume_level) {
- ISimpleAudioVolume *simple_audio_volume = NULL;
- HRESULT result = GetSimpleAudioVolume(&simple_audio_volume);
- if (FAILED(result) || simple_audio_volume == NULL) {
+ ISimpleAudioVolume *simple_audio_volume = GetSimpleAudioVolume();
+ if (!simple_audio_volume)
return false;
- }
-
- result = ISimpleAudioVolume_SetMasterVolume(simple_audio_volume, (float)(volume_level / 100.0), NULL);
-
+ HRESULT result = ISimpleAudioVolume_SetMasterVolume(simple_audio_volume, (float)(volume_level / 100.0), NULL);
ISimpleAudioVolume_Release(simple_audio_volume);
-
return SUCCEEDED(result);
}
bool SetApplicationMuted(bool muted) {
- ISimpleAudioVolume *simple_audio_volume = NULL;
- HRESULT result = GetSimpleAudioVolume(&simple_audio_volume);
- if (FAILED(result) || simple_audio_volume == NULL) {
+ ISimpleAudioVolume *simple_audio_volume = GetSimpleAudioVolume();
+ if (!simple_audio_volume)
return false;
- }
-
- result = ISimpleAudioVolume_SetMute(simple_audio_volume, muted, NULL);
-
+ HRESULT result = ISimpleAudioVolume_SetMute(simple_audio_volume, muted, NULL);
ISimpleAudioVolume_Release(simple_audio_volume);
-
return SUCCEEDED(result);
}
-HRESULT GetSimpleAudioVolume(ISimpleAudioVolume **simple_audio_volume) {
- HRESULT result = S_OK;
-
- result = CoInitialize(NULL);
-
+static ISimpleAudioVolume *GetSimpleAudioVolume() {
+ HRESULT result;
IMMDeviceEnumerator *device_enumerator = NULL;
- result = CoCreateInstance(&CLSID_MMDeviceEnumerator,
- NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, &device_enumerator);
+ IMMDevice *device = NULL;
+ IAudioSessionManager *audio_session_manager = NULL;
+ ISimpleAudioVolume *simple_audio_volume = NULL;
- if (FAILED(result) || device_enumerator == NULL) {
+ InitializeCom();
+
+ result = CoCreateInstance(&CLSID_MMDeviceEnumerator,
+ NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, &device_enumerator);
+ if (FAILED(result) || device_enumerator == NULL)
goto done;
- }
-
- IMMDevice *device = NULL;
+
result = IMMDeviceEnumerator_GetDefaultAudioEndpoint(device_enumerator, eRender, eConsole, &device);
-
- if (FAILED(result) || device == NULL) {
+ if (FAILED(result) || device == NULL)
goto done;
- }
-
- IAudioSessionManager *audio_session_manager = NULL;
+
result = IMMDevice_Activate(device, &IID_IAudioSessionManager, CLSCTX_INPROC_SERVER, NULL, &audio_session_manager);
-
- if (FAILED(result) || audio_session_manager == NULL) {
+ if (FAILED(result) || audio_session_manager == NULL)
goto done;
- }
+ result = IAudioSessionManager_GetSimpleAudioVolume(audio_session_manager, &GUID_NULL, 0, &simple_audio_volume);
- result = IAudioSessionManager_GetSimpleAudioVolume(audio_session_manager, &GUID_NULL, 0, simple_audio_volume);
-
done:
-#pragma warning(push)
-#pragma warning(disable: 6001)
if (device_enumerator) IMMDeviceEnumerator_Release(device_enumerator);
if (device) IMMDevice_Release(device);
if (audio_session_manager) IAudioSessionManager_Release(audio_session_manager);
-#pragma warning(pop)
- CoUninitialize();
-
- return SUCCEEDED(result);
+ return simple_audio_volume;
}
--- a/zelda3.vcxproj
+++ b/zelda3.vcxproj
@@ -155,6 +155,7 @@
<ClCompile Include="nmi.c" />
<ClCompile Include="overlord.c" />
<ClCompile Include="overworld.c" />
+ <ClCompile Include="platform\win32\volume_control.c" />
<ClCompile Include="player.c" />
<ClCompile Include="player_oam.c" />
<ClCompile Include="poly.c" />
@@ -224,6 +225,7 @@
<ClInclude Include="nmi.h" />
<ClInclude Include="overlord.h" />
<ClInclude Include="overworld.h" />
+ <ClInclude Include="platform\win32\volume_control.h" />
<ClInclude Include="player.h" />
<ClInclude Include="player_oam.h" />
<ClInclude Include="poly.h" />
--- a/zelda3.vcxproj.filters
+++ b/zelda3.vcxproj.filters
@@ -8,6 +8,9 @@
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
+ <Filter Include="Platform">
+ <UniqueIdentifier>{be379862-b248-4de9-8553-bb51e2e72141}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="snes\apu.c">
@@ -115,6 +118,9 @@
<ClCompile Include="config.c">
<Filter>Zelda</Filter>
</ClCompile>
+ <ClCompile Include="platform\win32\volume_control.c">
+ <Filter>Platform</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="snes\apu.h">
@@ -230,6 +236,9 @@
</ClInclude>
<ClInclude Include="snes\snes_regs.h">
<Filter>Snes</Filter>
+ </ClInclude>
+ <ClInclude Include="platform\win32\volume_control.h">
+ <Filter>Platform</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>