2023-09-16 15:34:45 -06:00
|
|
|
|
using System;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using BarRaider.SdTools;
|
|
|
|
|
using System.Drawing;
|
2023-09-16 15:34:45 -06:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-10-03 23:21:19 -06:00
|
|
|
|
using FocusVolumeControl.UI;
|
2023-10-04 09:29:52 -06:00
|
|
|
|
using BitFaster.Caching.Lru;
|
2023-10-04 22:24:31 -06:00
|
|
|
|
using FocusVolumeControl.AudioSession;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
|
|
|
|
namespace FocusVolumeControl.AudioSessions;
|
|
|
|
|
|
2024-04-20 21:19:44 -06:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public sealed class ActiveAudioSessionWrapper : IAudioSession
|
2023-08-20 20:52:48 -06:00
|
|
|
|
{
|
2024-04-20 21:19:44 -06:00
|
|
|
|
public string DisplayName { get; set; } = null!;
|
2023-09-16 15:34:45 -06:00
|
|
|
|
private List<IAudioSessionControl2> Sessions { get; } = new List<IAudioSessionControl2>();
|
|
|
|
|
private IEnumerable<ISimpleAudioVolume> Volume => Sessions.Cast<ISimpleAudioVolume>();
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-10-04 22:24:31 -06:00
|
|
|
|
public IconWrapper? IconWrapper { get; set; }
|
2024-02-24 23:46:00 -07:00
|
|
|
|
public IEnumerable<int> Pids => Sessions.Select(x =>
|
|
|
|
|
{
|
|
|
|
|
x.GetProcessId(out var pid); return pid;
|
|
|
|
|
});
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
|
|
|
|
public string GetIcon()
|
|
|
|
|
{
|
2023-10-04 09:29:52 -06:00
|
|
|
|
try
|
2023-08-20 20:52:48 -06:00
|
|
|
|
{
|
2023-10-04 22:24:31 -06:00
|
|
|
|
return IconWrapper?.GetIconData() ?? IconWrapper.FallbackIconData;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
2023-10-04 09:29:52 -06:00
|
|
|
|
catch
|
|
|
|
|
{
|
2023-10-04 22:24:31 -06:00
|
|
|
|
return IconWrapper.FallbackIconData;
|
2023-10-04 09:29:52 -06:00
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Any()
|
|
|
|
|
{
|
|
|
|
|
return Volume.Any();
|
|
|
|
|
}
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public int Count => Sessions.Count;
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
public void AddSession(IAudioSessionControl2 session)
|
2023-08-20 20:52:48 -06:00
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
Sessions.Add(session);
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ToggleMute()
|
|
|
|
|
{
|
|
|
|
|
//when all volumes are muted, Volume.All will return true
|
|
|
|
|
//so we swap from muted to false (opposite of Volume.All)
|
|
|
|
|
|
|
|
|
|
//when any volumes are unmuted, Volume.All will return false
|
|
|
|
|
//so we set muted to true (opposite of Volume.All)
|
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var muted = IsMuted();
|
2023-08-20 20:52:48 -06:00
|
|
|
|
|
2023-09-16 15:34:45 -06:00
|
|
|
|
foreach(var v in Volume)
|
|
|
|
|
{
|
|
|
|
|
var guid = Guid.Empty;
|
|
|
|
|
v.SetMute(!muted, ref guid);
|
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMuted()
|
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
return Volume.All(x =>
|
|
|
|
|
{
|
|
|
|
|
x.GetMute(out var mute);
|
|
|
|
|
return mute;
|
|
|
|
|
});
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void IncrementVolumeLevel(int step, int ticks)
|
|
|
|
|
{
|
|
|
|
|
//if you have more than one volume. they will all get set based on the first volume control
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var volume = Volume.FirstOrDefault();
|
|
|
|
|
var level = 0f;
|
|
|
|
|
if (volume != null)
|
|
|
|
|
{
|
|
|
|
|
volume.GetMasterVolume(out level);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-10 22:06:38 -06:00
|
|
|
|
level = VolumeHelpers.GetAdjustedVolume(level, step, ticks);
|
2023-09-16 15:34:45 -06:00
|
|
|
|
|
|
|
|
|
foreach(var v in Volume)
|
|
|
|
|
{
|
|
|
|
|
var guid = Guid.Empty;
|
|
|
|
|
v.SetMasterVolume(level, ref guid);
|
|
|
|
|
}
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetVolumeLevel()
|
|
|
|
|
{
|
2023-09-16 15:34:45 -06:00
|
|
|
|
var volume = Volume.FirstOrDefault();
|
|
|
|
|
var level = 0f;
|
|
|
|
|
if(volume != null)
|
|
|
|
|
{
|
|
|
|
|
volume.GetMasterVolume(out level);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-10 22:06:38 -06:00
|
|
|
|
return VolumeHelpers.GetVolumePercentage(level);
|
2023-08-20 20:52:48 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-20 21:19:44 -06:00
|
|
|
|
#nullable restore
|