129 lines
3.2 KiB
C#
129 lines
3.2 KiB
C#
using FocusVolumeControl;
|
|
using FocusVolumeControl.AudioHelpers;
|
|
using FocusVolumeControl.AudioSessions;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
namespace SoundBrowser;
|
|
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
|
|
AudioHelper _audioHelper;
|
|
Native.WinEventDelegate _delegate;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
_audioHelper = new AudioHelper();
|
|
|
|
//normally you can just pass a lambda, but for some reason, that seems to get garbage collected
|
|
_delegate = new Native.WinEventDelegate(WinEventProc);
|
|
Native.RegisterForForegroundWindowChangedEvent(_delegate);
|
|
}
|
|
|
|
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
|
{
|
|
SetupCurrentAppFields();
|
|
SetupAllSessionFields();
|
|
}
|
|
|
|
private void SetupCurrentAppFields()
|
|
{
|
|
var handle = Native.GetForegroundWindow();
|
|
var sb = new StringBuilder();
|
|
|
|
if (handle != IntPtr.Zero)
|
|
{
|
|
//use this in debug to help there be less events
|
|
|
|
/*
|
|
Native.GetWindowThreadProcessId(handle, out var fpid);
|
|
var fp = Process.GetProcessById(fpid);
|
|
|
|
if(!fp.ProcessName.Contains("FSD"))
|
|
{
|
|
return;
|
|
}
|
|
*/
|
|
|
|
var processes = _audioHelper.GetPossibleProcesses();
|
|
var session = _audioHelper.FindSession(processes);
|
|
|
|
foreach (var p in processes)
|
|
{
|
|
var displayName = (new NameAndIconHelper()).GetProcessInfo(p);
|
|
|
|
sb.AppendLine($"pid: {p.Id}");
|
|
sb.AppendLine($"\tprocessName: {p.ProcessName}");
|
|
sb.AppendLine($"\tDisplayName: {displayName}");
|
|
|
|
}
|
|
|
|
sb.AppendLine();
|
|
if (session != null)
|
|
{
|
|
sb.AppendLine("picked the following best match");
|
|
sb.AppendLine($"\tsession: {session.DisplayName}");
|
|
sb.AppendLine($"\tvolume: {session.GetVolumeLevel()}");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine("No Match");
|
|
}
|
|
}
|
|
|
|
_tf.Text = sb.ToString();
|
|
}
|
|
|
|
private void SetupAllSessionFields()
|
|
{
|
|
_tf2.Text = "";
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("-------------------------------------------------------------------------------");
|
|
|
|
var deviceEnumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
|
|
|
|
deviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active, out var deviceCollection);
|
|
deviceCollection.GetCount(out var num);
|
|
|
|
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
deviceCollection.Item(i, out var device);
|
|
//todo: put the device name in the output
|
|
sb.AppendLine($"----");
|
|
|
|
|
|
Guid iid = typeof(IAudioSessionManager2).GUID;
|
|
device.Activate(ref iid, CLSCTX.ALL, IntPtr.Zero, out var m);
|
|
var manager = (IAudioSessionManager2)m;
|
|
|
|
|
|
manager.GetSessionEnumerator(out var sessionEnumerator);
|
|
|
|
|
|
sessionEnumerator.GetCount(out var count);
|
|
for (int s = 0; s < count; s++)
|
|
{
|
|
sessionEnumerator.GetSession(s, out var session);
|
|
|
|
session.GetProcessId(out var processId);
|
|
session.GetIconPath(out var path);
|
|
var audioProcess = Process.GetProcessById(processId);
|
|
|
|
var displayName = (new NameAndIconHelper()).GetProcessInfo(audioProcess);
|
|
sb.AppendLine($"pid: {audioProcess.Id}\t\t processName: {displayName}");
|
|
}
|
|
|
|
_tf2.Text = sb.ToString();
|
|
}
|
|
}
|
|
|
|
}
|