-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Settings with option to select how to display the name of the dev…
…ices
- Loading branch information
1 parent
01d9d1b
commit 18aec08
Showing
14 changed files
with
437 additions
and
39 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Flow.Launcher.Plugin.AudioDeviceSelector.Tests/AudioDevicesManagerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...er.Plugin.AudioDeviceSelector.Tests/Flow.Launcher.Plugin.AudioDeviceSelector.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Flow.Launcher.Plugin.AudioDeviceSelector/Audio/AudioDevicesManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Flow.Launcher.Plugin.AudioDeviceSelector/Components/Settings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+55.6 KB
src/Flow.Launcher.Plugin.AudioDeviceSelector/Images/speaker_resource.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.