Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(infra): abstract over WASAPI COM interfaces (#241)

Aryaman Marathec7069beafd7b
diff --git a/.vscode/launch.json b/.vscode/launch.json
old mode 100644
new mode 100755
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
old mode 100644
new mode 100755
diff --git a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
index fb8ccdcd..b3e989be 100644
--- a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
+++ b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
@@ -7,20 +7,16 @@
</PropertyGroup>
<ItemGroup>
- <PackageReference
- Include="Microsoft.Extensions.Hosting"
- Version="6.0.1" />
- <PackageReference
- Include="System.Reactive"
- Version="5.0.0" />
- <PackageReference
- Include="YamlDotNet"
- Version="8.1.0" />
- <PackageReference
- Include="Roslynator.Analyzers"
- Version="4.2.0" />
- <PackageReference
- Include="Microsoft.CodeAnalysis.NetAnalyzers"
- Version="7.0.0" />
+ <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
+ <PackageReference Include="System.Reactive" Version="5.0.0" />
+ <PackageReference Include="Vanara.Core" Version="3.4.13" />
+ <PackageReference Include="Vanara.PInvoke.CoreAudio" Version="3.4.13" />
+ <PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.13" />
+ <PackageReference Include="Vanara.PInvoke.Kernel32" Version="3.4.13" />
+ <PackageReference Include="Vanara.PInvoke.SHCore" Version="3.4.13" />
+ <PackageReference Include="Vanara.PInvoke.User32" Version="3.4.13" />
+ <PackageReference Include="YamlDotNet" Version="8.1.0" />
+ <PackageReference Include="Roslynator.Analyzers" Version="4.2.0" />
+ <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0" />
</ItemGroup>
</Project>
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs
new file mode 100755
index 00000000..3018700d
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Runtime.InteropServices;
+using Vanara.PInvoke;
+using static Vanara.PInvoke.CoreAudio;
+
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class SystemVolumeInformation : IAudioEndpointVolumeCallback, IMMNotificationClient
+ {
+ private readonly IMMDeviceEnumerator _deviceEnum = new();
+ private IMMDevice _defaultDevice;
+ private IAudioEndpointVolume _endPointVolume;
+
+ public event EventHandler<VolumeChangedEventArgs> VolumeChangedEvent;
+
+ public SystemVolumeInformation()
+ {
+ _deviceEnum.RegisterEndpointNotificationCallback(this);
+ _defaultDevice = _deviceEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
+
+ _defaultDevice.Activate(typeof(IAudioEndpointVolume).GUID, 0, null, out var epVol);
+ _endPointVolume = epVol as IAudioEndpointVolume;
+ _endPointVolume.RegisterControlChangeNotify(this);
+ }
+
+ public HRESULT OnNotify(IntPtr pNotify)
+ {
+ var notificationData = Marshal.PtrToStructure<AUDIO_VOLUME_NOTIFICATION_DATA>(pNotify);
+
+ var channelVolumesOffset = Marshal.OffsetOf<AUDIO_VOLUME_NOTIFICATION_DATA>("afChannelVolumes");
+ var firstChannelVolumesMember = (IntPtr)((long)pNotify + (long)channelVolumesOffset);
+
+ var channelVolumes = new float[notificationData.nChannels];
+
+ for (var i = 0; i < notificationData.nChannels; i++)
+ {
+ channelVolumes[i] = Marshal.PtrToStructure<float>(firstChannelVolumesMember);
+ }
+
+ VolumeChangedEvent?.Invoke(this, new VolumeChangedEventArgs
+ {
+ Volume = notificationData.fMasterVolume,
+ Muted = notificationData.bMuted
+ });
+
+ System.Diagnostics.Debug.WriteLine($"{notificationData.fMasterVolume}");
+
+ return HRESULT.S_OK;
+ }
+
+ public HRESULT OnDefaultDeviceChanged(EDataFlow flow, ERole role, string pwstrDefaultDeviceId)
+ {
+ if (role.Equals(ERole.eMultimedia))
+ {
+ _endPointVolume.UnregisterControlChangeNotify(this);
+
+ _defaultDevice = _deviceEnum.GetDevice(pwstrDefaultDeviceId);
+ _defaultDevice.Activate(typeof(IAudioEndpointVolume).GUID, 0, null, out var epVol);
+
+ _endPointVolume = epVol as IAudioEndpointVolume;
+ _endPointVolume.RegisterControlChangeNotify(this);
+ }
+ return HRESULT.S_OK;
+ }
+
+ public HRESULT OnDeviceStateChanged(string pwstrDeviceId, DEVICE_STATE dwNewState)
+ {
+ return HRESULT.S_OK;
+ }
+
+ public HRESULT OnDeviceAdded(string pwstrDeviceId)
+ {
+ return HRESULT.S_OK;
+ }
+
+ public HRESULT OnDeviceRemoved(string pwstrDeviceId)
+ {
+ return HRESULT.S_OK;
+ }
+
+ public HRESULT OnPropertyValueChanged(string pwstrDeviceId, Ole32.PROPERTYKEY key)
+ {
+ return HRESULT.S_OK;
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs b/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs
new file mode 100755
index 00000000..22d0c7d4
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/VolumeChangedEventArgs.cs
@@ -0,0 +1,8 @@
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class VolumeChangedEventArgs
+ {
+ public float Volume { get; set; }
+ public bool Muted { get; set; }
+ }
+}