Skip to content

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

feat(infra): add helper for getting system media information (#278)

Aryaman Marathea93f64a7cd72
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 87fb71bd..e2b235b4 100755
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
- "program": "${workspaceFolder}/GlazeWM.Bootstrapper/bin/Debug/net6.0-windows/GlazeWM.dll",
+ "program": "${workspaceFolder}/GlazeWM.Bootstrapper/bin/Debug/net6.0-windows10.0.17763.0/GlazeWM.dll",
"args": [],
"cwd": "${workspaceFolder}/GlazeWM.Bootstrapper",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
diff --git a/.vscode/settings.json b/.vscode/settings.json
index e318bd63..c70e05b1 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,7 +1,6 @@
{
// Enable Roslyn analyzers. Needed for showing linting errors.
"omnisharp.enableRoslynAnalyzers": true,
-
// XAML formatting configuration.
"[xaml]": {
"editor.defaultFormatter": "redhat.vscode-xml"
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index 56ac59eb..ec4f7dd7 100755
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -54,6 +54,17 @@
"args": [
"watch",
"run",
+ "--project",
+ "${workspaceFolder}/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj"
+ ],
+ "problemMatcher": "$msCompile"
+ },
+ {
+ "label": "publish",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "publish",
"${workspaceFolder}/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
diff --git a/GlazeWM.Bar/GlazeWM.Bar.csproj b/GlazeWM.Bar/GlazeWM.Bar.csproj
index 5b57b80d..fa47e674 100644
--- a/GlazeWM.Bar/GlazeWM.Bar.csproj
+++ b/GlazeWM.Bar/GlazeWM.Bar.csproj
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
- <TargetFramework>net6.0-windows</TargetFramework>
+ <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<UseWPF>true</UseWPF>
<DebugType>embedded</DebugType>
</PropertyGroup>
diff --git a/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj b/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
index 9e7b6a65..4f576f58 100644
--- a/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
+++ b/GlazeWM.Bootstrapper/GlazeWM.Bootstrapper.csproj
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
- <TargetFramework>net6.0-windows</TargetFramework>
+ <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<DebugType>embedded</DebugType>
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
diff --git a/GlazeWM.Domain/GlazeWM.Domain.csproj b/GlazeWM.Domain/GlazeWM.Domain.csproj
index ac29980f..18aeb41f 100644
--- a/GlazeWM.Domain/GlazeWM.Domain.csproj
+++ b/GlazeWM.Domain/GlazeWM.Domain.csproj
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
- <TargetFramework>net6.0-windows</TargetFramework>
+ <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
<StartupObject />
diff --git a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
index e16698b3..275fa60a 100644
--- a/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
+++ b/GlazeWM.Infrastructure/GlazeWM.Infrastructure.csproj
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
- <TargetFramework>net6.0-windows</TargetFramework>
+ <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<DebugType>embedded</DebugType>
</PropertyGroup>
diff --git a/GlazeWM.Infrastructure/WindowsApi/CurrentMediaChangedArgsEvent.cs b/GlazeWM.Infrastructure/WindowsApi/CurrentMediaChangedArgsEvent.cs
new file mode 100755
index 00000000..fb1006a0
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/CurrentMediaChangedArgsEvent.cs
@@ -0,0 +1,9 @@
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class CurrentMediaChangedEventArgs
+ {
+ public string AlbumTitle { get; set; }
+ public string Artist { get; set; }
+ public string Title { get; set; }
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/CurrentMediaPlaybackChangedEventArgs.cs b/GlazeWM.Infrastructure/WindowsApi/CurrentMediaPlaybackChangedEventArgs.cs
new file mode 100755
index 00000000..cbc5d89d
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/CurrentMediaPlaybackChangedEventArgs.cs
@@ -0,0 +1,9 @@
+using Windows.Media.Control;
+
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class CurrentMediaPlaybackChangedEventArgs
+ {
+ public GlobalSystemMediaTransportControlsSessionPlaybackStatus PlaybackState { get; set; }
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemMediaInformation.cs b/GlazeWM.Infrastructure/WindowsApi/SystemMediaInformation.cs
new file mode 100755
index 00000000..da1a41e0
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemMediaInformation.cs
@@ -0,0 +1,69 @@
+
+using System;
+using Windows.Media.Control;
+
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class SystemMediaInformation
+ {
+ private readonly GlobalSystemMediaTransportControlsSessionManager _gsmtcsManager;
+ private GlobalSystemMediaTransportControlsSession _currentSession;
+
+ public event EventHandler<CurrentMediaPlaybackChangedEventArgs> CurrentMediaPlaybackChanged;
+ public event EventHandler<CurrentMediaChangedEventArgs> CurrentMediaChanged;
+
+ public SystemMediaInformation()
+ {
+ _gsmtcsManager = GlobalSystemMediaTransportControlsSessionManager
+ .RequestAsync()
+ .GetAwaiter()
+ .GetResult();
+ _gsmtcsManager.CurrentSessionChanged += OnCurrentSessionChanged;
+ _currentSession = _gsmtcsManager.GetCurrentSession();
+ _currentSession.MediaPropertiesChanged += OnMediaPropertiesChanged;
+ var mediaProps = _currentSession
+ .TryGetMediaPropertiesAsync()
+ .GetAwaiter()
+ .GetResult();
+ CurrentMediaChanged?.Invoke(
+ this, new CurrentMediaChangedEventArgs
+ {
+ Title = mediaProps.Title,
+ AlbumTitle = mediaProps.AlbumTitle,
+ Artist = mediaProps.Artist
+ }
+ );
+ var currentState = _currentSession.GetPlaybackInfo().PlaybackStatus;
+ CurrentMediaPlaybackChanged?.Invoke(
+ this, new CurrentMediaPlaybackChangedEventArgs { PlaybackState = currentState }
+ );
+ }
+
+ private void OnCurrentSessionChanged(GlobalSystemMediaTransportControlsSessionManager sender, CurrentSessionChangedEventArgs args)
+ {
+ _currentSession.MediaPropertiesChanged -= OnMediaPropertiesChanged;
+ _currentSession = _gsmtcsManager.GetCurrentSession();
+ _currentSession.MediaPropertiesChanged += OnMediaPropertiesChanged;
+ var mediaProps = _currentSession
+ .TryGetMediaPropertiesAsync()
+ .GetAwaiter()
+ .GetResult();
+ CurrentMediaChanged?.Invoke(
+ this, new CurrentMediaChangedEventArgs
+ {
+ Title = mediaProps.Title,
+ AlbumTitle = mediaProps.AlbumTitle,
+ Artist = mediaProps.Artist
+ }
+ );
+ }
+
+ private void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args)
+ {
+ var newState = _currentSession.GetPlaybackInfo().PlaybackStatus;
+ CurrentMediaPlaybackChanged?.Invoke(
+ this, new CurrentMediaPlaybackChangedEventArgs { PlaybackState = newState }
+ );
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs
index 3018700d..70ba7918 100755
--- a/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemVolumeInformation.cs
@@ -11,7 +11,7 @@ namespace GlazeWM.Infrastructure.WindowsApi
private IMMDevice _defaultDevice;
private IAudioEndpointVolume _endPointVolume;
- public event EventHandler<VolumeChangedEventArgs> VolumeChangedEvent;
+ public event EventHandler<VolumeChangedEventArgs> VolumeChanged;
public SystemVolumeInformation()
{
@@ -21,6 +21,12 @@ namespace GlazeWM.Infrastructure.WindowsApi
_defaultDevice.Activate(typeof(IAudioEndpointVolume).GUID, 0, null, out var epVol);
_endPointVolume = epVol as IAudioEndpointVolume;
_endPointVolume.RegisterControlChangeNotify(this);
+
+ VolumeChanged?.Invoke(this, new VolumeChangedEventArgs
+ {
+ Volume = _endPointVolume.GetMasterVolumeLevelScalar(),
+ Muted = _endPointVolume.GetMute()
+ });
}
public HRESULT OnNotify(IntPtr pNotify)
@@ -37,14 +43,12 @@ namespace GlazeWM.Infrastructure.WindowsApi
channelVolumes[i] = Marshal.PtrToStructure<float>(firstChannelVolumesMember);
}
- VolumeChangedEvent?.Invoke(this, new VolumeChangedEventArgs
+ VolumeChanged?.Invoke(this, new VolumeChangedEventArgs
{
Volume = notificationData.fMasterVolume,
Muted = notificationData.bMuted
});
- System.Diagnostics.Debug.WriteLine($"{notificationData.fMasterVolume}");
-
return HRESULT.S_OK;
}