Skip to content

Commit

Permalink
Add Settings with option to select how to display the name of the dev…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
attilakapostyak committed Mar 2, 2023
1 parent 01d9d1b commit 18aec08
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Flow.Launcher.Plugin.AudioDeviceSelector.Audio;
using System;
using Xunit;

namespace Flow.Launcher.Plugin.AudioDeviceSelector.Tests
{
public class AudioDevicesManagerTests
{
[Theory]
[InlineData("", TitleTypeSettings.FriendlyName, "")]
[InlineData("Speakers (SRS-XB13 Stereo)", TitleTypeSettings.FriendlyName, "Speakers (SRS-XB13 Stereo)")]
[InlineData("Speakers", TitleTypeSettings.DeviceName, "Speakers")]
[InlineData("SRS-XB13 Stereo", TitleTypeSettings.DeviceDescription, "SRS-XB13 Stereo")]
[InlineData("Speakers (SRS-XB13 Stereo)", TitleTypeSettings.DeviceName, "Speakers")]
[InlineData("Speakers (SRS-XB13 Stereo)", TitleTypeSettings.DeviceDescription, "SRS-XB13 Stereo")]
[InlineData("Speakers(SRS-XB13 Stereo)", TitleTypeSettings.DeviceName, "Speakers")]
[InlineData("Speakers(SRS-XB13 Stereo)", TitleTypeSettings.DeviceDescription, "SRS-XB13 Stereo")]
public void Test_GetDeviceTitle(string friendlyName, TitleTypeSettings titleType, string expectedResult)
{
var obj = new AudioDevicesManager();

var result = obj.GetDeviceTitle(friendlyName, titleType);

Assert.Equal(result, expectedResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<ProjectGuid>{B7882135-1891-4ED4-B116-8CD4144E5408}</ProjectGuid>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Flow.Launcher.Plugin.AudioDeviceSelector.Tests</RootNamespace>
<AssemblyName>Flow.Launcher.Plugin.AudioDeviceSelector.Tests</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
<UseWpf>true</UseWpf>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Flow.Launcher.Plugin.AudioDeviceSelector\Flow.Launcher.Plugin.AudioDeviceSelector.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Flow.Launcher.Plugin.AudioDeviceSelector.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flow.Launcher.Plugin.AudioDeviceSelector", "Flow.Launcher.Plugin.AudioDeviceSelector\Flow.Launcher.Plugin.AudioDeviceSelector.csproj", "{03FFA443-5F50-48D5-8869-F3DF316803AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flow.Launcher.Plugin.AudioDeviceSelector.Tests", "Flow.Launcher.Plugin.AudioDeviceSelector.Tests\Flow.Launcher.Plugin.AudioDeviceSelector.Tests.csproj", "{B7882135-1891-4ED4-B116-8CD4144E5408}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{03FFA443-5F50-48D5-8869-F3DF316803AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03FFA443-5F50-48D5-8869-F3DF316803AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03FFA443-5F50-48D5-8869-F3DF316803AB}.Release|Any CPU.Build.0 = Release|Any CPU
{B7882135-1891-4ED4-B116-8CD4144E5408}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7882135-1891-4ED4-B116-8CD4144E5408}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7882135-1891-4ED4-B116-8CD4144E5408}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7882135-1891-4ED4-B116-8CD4144E5408}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Flow.Launcher.Plugin.AudioDeviceSelector.Audio.Interop;
using NAudio.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Flow.Launcher.Plugin.AudioDeviceSelector.Audio
{
public class AudioDevicesManager
{
private List<MMDevice> devices = null;
private DateTime lastDeviceUpdateTimeStamp = DateTime.Now;
private int updateIntervalSeconds = 5;
private MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();

public List<MMDevice> Devices
{
get {
if (devices == null)
devices = GetDevices();

return devices;
}
}
public List<MMDevice> GetDevices()
{
var datetime1 = DateTime.Now;
var devices = new List<MMDevice>();

var endpoints = deviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
foreach (var endpoint in endpoints)
{
devices.Add(endpoint);
}

return devices;
}

public void UpdateDevices()
{
DateTime currentTime = DateTime.Now;
if (devices == null || (currentTime - lastDeviceUpdateTimeStamp).TotalSeconds > updateIntervalSeconds)
{
devices = GetDevices();
lastDeviceUpdateTimeStamp = currentTime;
}
}

public bool SetDevice(string deviceFriendlyName)
{
var devices = GetDevices();
var device = devices.Find(d => d.FriendlyName == deviceFriendlyName);
if (device == null)
return false;

var policy = new PolicyConfigClientWin7();
policy.SetDefaultEndpoint(device.ID, ERole.eMultimedia);

return true;
}

public string GetDeviceTitle(string friendyName, TitleTypeSettings titleType)
{
Regex regex = new Regex("^(?<deviceName>.*?)\\s?(?:\\((?<description>.*?)\\))?$");
MatchCollection matches = regex.Matches(friendyName);

if (matches.Count > 0)
{
var match = matches[0];

if (match.Success)
{
switch (titleType)
{
case TitleTypeSettings.FriendlyName:
return friendyName;
case TitleTypeSettings.DeviceName:
var title = match.Groups["deviceName"].Value;
if (!string.IsNullOrEmpty(title))
{
return match.Groups["deviceName"].Value;
}
break;
case TitleTypeSettings.DeviceDescription:
title = match.Groups["description"].Value;
if (string.IsNullOrEmpty(title))
{
return match.Groups["deviceName"].Value;
}

return title;
}
}
}

return friendyName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace Flow.Launcher.Plugin.AudioDeviceSelector.Components
{
public class Settings : INotifyPropertyChanged
{
private bool displayFriendlyName;
private bool displayDeviceName;
private bool displayDeviceDescription;

public Settings()
{
}

public bool DisplayFriendlyName
{
get => displayFriendlyName;
set
{
displayFriendlyName = value;

OnPropertyChanged();
}
}

public bool DisplayDeviceName
{
get => displayDeviceName;
set
{
displayDeviceName = value;

OnPropertyChanged();
}
}

public bool DisplayDeviceDescription
{
get => displayDeviceDescription;
set
{
displayDeviceDescription = value;

OnPropertyChanged();
}
}

private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public event PropertyChangedEventHandler? PropertyChanged;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
<BaseOutputPath>C:\Users\attila\AppData\Local\FlowLauncher\app-1.9.5\Plugins\Flow.Launcher.Plugin.AudioDeviceSelector</BaseOutputPath>
<UseWpf>true</UseWpf>
<BaseOutputPath>C:\Users\attila\AppData\Local\FlowLauncher\app-1.9.5\Plugins\Flow.Launcher.Plugin.AudioDeviceSelector\\</BaseOutputPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -22,6 +23,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="Images\speaker.png" />
<None Remove="Images\speaker_resource.png" />
<Resource Include="Images\speaker_resource.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Content Include="Images\speaker.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
<system:String x:Key="plugin_audiodeviceselector_device_not_found">Device '{0}' not found</system:String>
<system:String x:Key="plugin_audiodeviceselector_error_while_changing_device">An error occurred while changing the device.</system:String>

<system:String x:Key="plugin_audiodeviceselector_settings_devicename_displaymode">Display Mode:</system:String>

<system:String x:Key="plugin_audiodeviceselector_settings_friendlynameoption">Friendly Name</system:String>
<system:String x:Key="plugin_audiodeviceselector_settings_devicenameoption">Device Name</system:String>
<system:String x:Key="plugin_audiodeviceselector_settings_devicedescriptionoption">Device Description</system:String>

</ResourceDictionary>
Loading

0 comments on commit 18aec08

Please sign in to comment.