Skip to content

Commit

Permalink
Added ability to see active audio profile
Browse files Browse the repository at this point in the history
Added registry checks to see which audio profile is currently active.
Reflect the active profile within the UI
  • Loading branch information
SmokeyMcBong committed May 7, 2020
1 parent 86d53f4 commit c6346cf
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 89 deletions.
6 changes: 4 additions & 2 deletions MiToolz/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<Application x:Class="MiToolz.App"
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="MiToolz.App"
StartupUri="MainWindow.xaml">
<Application.Resources>

<System:String x:Key="VersionNo" xml:space="preserve"> MiToolz v2.7.0 </System:String>
<System:String x:Key="VersionDate" xml:space="preserve"> Build Date : 07-05-2020 </System:String>
</Application.Resources>
</Application>
93 changes: 93 additions & 0 deletions MiToolz/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Microsoft.Win32;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;

namespace MiToolz
{
class ConfigManager
{
readonly string Path;
readonly string EXE = Assembly.GetExecutingAssembly().GetName().Name;

[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

//ini file management >
public ConfigManager(string IniPath = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
}

public string IniRead(string Key, string Section = null)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}

public void IniWrite(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}

public void IniDeleteKey(string Key, string Section = null)
{
IniWrite(Key, null, Section ?? EXE);
}

public void IniDeleteSection(string Section = null)
{
IniWrite(null, null, Section ?? EXE);
}

public bool IniKeyExists(string Key, string Section = null)
{
return IniRead(Key, Section).Length > 0;
}

//registry key management >
public string RegReadKeyValue(string subKey, string key)

{
string str = string.Empty;

using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(subKey))

{
if (registryKey != null)
{
str = registryKey.GetValue(key).ToString();
registryKey.Close();
}
}
return str;
}

//xml file management >
public string XmlRead(string FullXmlFilePath)
{
string str = string.Empty;

//read xml entry for 'profile_name' in xml located in 'FullXmlFilePath' return profile name value

XDocument doc = XDocument.Load(FullXmlFilePath);
var selectors = from elements in doc.Elements("profile").Elements("info")
select elements;

foreach (var element in selectors)
{
str = element.Element("profile_name").Value;
//MessageBox.Show(element.Element("profile_name").Value);
}

return str;
}
}
}
51 changes: 0 additions & 51 deletions MiToolz/IniFile.cs

This file was deleted.

10 changes: 5 additions & 5 deletions MiToolz/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MiToolz" Height="260" Width="394" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
Title="MiToolz" Height="260" Width="394" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Activated="Window_Activated">
<Grid Background="Gray">
<Button x:Name="Button_Stock" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="94" Height="95" Margin="29,29,0,0" Click="Button_SetStock_Click">
<Button.Background>
Expand Down Expand Up @@ -32,7 +32,7 @@
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="84" VerticalAlignment="Top" Width="1" Margin="131,35,0,0"/>
<TextBlock x:Name="TextBlock_Stock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Stock Profile" VerticalAlignment="Top" Margin="29,32,0,0" Width="94" TextAlignment="Center"/>
<TextBlock x:Name="TextBlock_OC" HorizontalAlignment="Left" TextWrapping="Wrap" Text="OC Profile" VerticalAlignment="Top" Margin="139,32,0,0" Width="94" TextAlignment="Center"/>
<TextBlock x:Name="TextBlock_Sound" HorizontalAlignment="Left" TextWrapping="Wrap" Text="SoundBlaster" VerticalAlignment="Top" Margin="254,32,0,0" Width="94" TextAlignment="Center"/>
<TextBlock x:Name="TextBlock_Sound" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="254,32,0,0" Width="94" TextAlignment="Center" Foreground="White"/>
<Button x:Name="Button_Expand" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="18" Height="19" Margin="357,2,0,0" Click="Button_Expand_Click">
<Button.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/Image_MenuExpand.png">
Expand Down Expand Up @@ -64,7 +64,7 @@
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="189" Margin="380,179,0,0">
<TextBlock ToolTip=" Build Date : 03-05-2020 " Text=" MiToolz v2.6.1 " HorizontalAlignment="Left" TextWrapping="Wrap" Width="189" TextAlignment="Center" Margin="-1,0,-1,2" Height="16" VerticalAlignment="Center"/>
<TextBlock ToolTip="{DynamicResource VersionDate}" Text="{DynamicResource VersionNo}" HorizontalAlignment="Left" TextWrapping="Wrap" Width="189" TextAlignment="Center" Margin="-1,0,-1,2" Height="16" VerticalAlignment="Center"/>
</Border>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="1" Margin="477,84,0,0"/>
<Button x:Name="Button_SaveProfiles" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="22" Height="22" Margin="541,28,0,0" Click="Button_SaveProfiles_Click">
Expand All @@ -84,7 +84,7 @@
</Border>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Core Clock:" VerticalAlignment="Top" Margin="49,168,0,0" Width="69" TextAlignment="Center" FontSize="11"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Memory Clock:" VerticalAlignment="Top" Margin="190,168,0,0" Width="74" TextAlignment="Center" FontSize="11"/>
<TextBlock x:Name="ShowCoreMhz" HorizontalAlignment="Left" TextWrapping="Wrap" Text=" ---- " VerticalAlignment="Top" Margin="117,168,0,0" Width="50" TextAlignment="Center" Height="15" FontSize="11" Foreground="White"/>
<TextBlock x:Name="ShowMemMhz" HorizontalAlignment="Left" TextWrapping="Wrap" Text=" ---- " VerticalAlignment="Top" Margin="270,168,0,0" Width="50" TextAlignment="Center" Height="15" FontSize="11" Foreground="White"/>
<TextBlock x:Name="ShowCoreMhz" HorizontalAlignment="Left" TextWrapping="Wrap" Text=" ----- " VerticalAlignment="Top" Margin="117,168,0,0" Width="50" TextAlignment="Center" Height="15" FontSize="11" Foreground="White"/>
<TextBlock x:Name="ShowMemMhz" HorizontalAlignment="Left" TextWrapping="Wrap" Text=" ----- " VerticalAlignment="Top" Margin="270,168,0,0" Width="50" TextAlignment="Center" Height="15" FontSize="11" Foreground="White"/>
</Grid>
</Window>
89 changes: 63 additions & 26 deletions MiToolz/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ public partial class MainWindow : Window
private static string StockProfile;
private static string OCProfile;
private static string SBControl_File;
static string SBControl_ActiveProfile;
private static string MSIAB_File;
private static string IsMonitoringEnabled;
private static bool NeedRestart = false;
private static readonly IniFile MyIni = new IniFile();
private static readonly ConfigManager ConfigManager = new ConfigManager();
private static readonly Brush IndicatorReady = Brushes.Green;
private static readonly Brush IndicatorBusy = Brushes.Orange;
private static readonly int DelayS = 250;
private static readonly int DelayN = 1000;

public MainWindow()
{
StartupSetup();
InitializeComponent();

//run startup checks, read all config settings and show on UI elements
StartupSetup();
SetComboLists();
ShowActiveSoundProfile();
ShowActiveMSIabProfile();
Indicator.Background = IndicatorReady;

//add MainWindow close event handler
Expand All @@ -47,9 +52,6 @@ public MainWindow()
GPUEnabled = true
};

//show which profile is currently active
ShowActiveProfile();
//check and start frequency monitoring if enabled
CheckStartMonitor();
}

Expand All @@ -63,20 +65,20 @@ private void StartupSetup()
}

//check for ini file, if not found then create new file and write default values to it
string MyIniFile = Properties.Resources.MyIniFile;
string MyConfigManager = Properties.Resources.MyConfigManager;
string SBControl_FilePath = Properties.Resources.SBControl_FilePath;
string MSIAB_FilePath = Properties.Resources.MSIAB_FilePath;
string DefaultStockProfile = Properties.Resources.DefaultStockProfile;
string DefaultOCProfile = Properties.Resources.DefaultOCProfile;
IsMonitoringEnabled = "1";

if (!File.Exists(MyIniFile))
if (!File.Exists(MyConfigManager))
{
MyIni.Write("StockProfile", DefaultStockProfile);
MyIni.Write("OCProfile", DefaultOCProfile);
MyIni.Write("SBControl_File", SBControl_FilePath);
MyIni.Write("MSIAB_File", MSIAB_FilePath);
MyIni.Write("IsMonitoringEnabled", IsMonitoringEnabled);
ConfigManager.IniWrite("StockProfile", DefaultStockProfile);
ConfigManager.IniWrite("OCProfile", DefaultOCProfile);
ConfigManager.IniWrite("SBControl_File", SBControl_FilePath);
ConfigManager.IniWrite("MSIAB_File", MSIAB_FilePath);
ConfigManager.IniWrite("IsMonitoringEnabled", IsMonitoringEnabled);
ReadSettings();
}
else
Expand All @@ -88,11 +90,32 @@ private void StartupSetup()
//read in values from ini file
private void ReadSettings()
{
StockProfile = MyIni.Read("StockProfile");
OCProfile = MyIni.Read("OCProfile");
SBControl_File = MyIni.Read("SBControl_File");
MSIAB_File = MyIni.Read("MSIAB_File");
IsMonitoringEnabled = MyIni.Read("IsMonitoringEnabled");
StockProfile = ConfigManager.IniRead("StockProfile");
OCProfile = ConfigManager.IniRead("OCProfile");
SBControl_File = ConfigManager.IniRead("SBControl_File");
MSIAB_File = ConfigManager.IniRead("MSIAB_File");
IsMonitoringEnabled = ConfigManager.IniRead("IsMonitoringEnabled");

string SBControl_ProfileFilePath = Properties.Resources.SBControl_ProfileFilePath;
string SBControl_ProfileRegPath = Properties.Resources.SBControl_ProfileRegPath;

//full path to profile folder
string WinUname = Environment.UserName;
string SBPAth = @"C:\Users\" + WinUname + @"\" + SBControl_ProfileFilePath;

//get folder name of subfolder (HDAUDIO_VEN_10EC_DEV_0899_SUBSYS_11020041 etc)
string[] SBGetIDDir = Directory.GetDirectories(SBPAth, "HDAUDIO*", SearchOption.TopDirectoryOnly);
string SBDeviceIDPath = string.Join("", SBGetIDDir);
string SBDeviceID = SBDeviceIDPath.Substring(SBDeviceIDPath.LastIndexOf(@"\") + 1);

//get registry key value for active profile
string SBRegKeyName = SBControl_ProfileRegPath + SBDeviceID;
string SBGetValue = ConfigManager.RegReadKeyValue(SBRegKeyName, "Profile");
string SBRegKeyValue = SBGetValue.Substring(SBGetValue.LastIndexOf(@"\") + 1);

//read profile xml and extract profile_name value
string FullXmlFilePath = SBDeviceIDPath + @"\" + SBRegKeyValue;
SBControl_ActiveProfile = ConfigManager.XmlRead(FullXmlFilePath);
}

//set if frequency monitoring is enabled and show if true
Expand All @@ -116,8 +139,14 @@ private void SetComboLists()
Combo_OC.SelectedIndex = int.Parse(OCProfile) - 1;
}

//show which Audio profile is active
private void ShowActiveSoundProfile()
{
TextBlock_Sound.Text = SBControl_ActiveProfile;
}

//determin which profile is active by checking if power.limit is greater than defaul_power.limit
private void ShowActiveProfile()
private void ShowActiveMSIabProfile()
{
Process ShowProfile_Process = new Process
{
Expand Down Expand Up @@ -276,7 +305,7 @@ private void ApplyProfile(string Profile)
{
//change indicator elements on the UI thread.
Indicator.Background = IndicatorReady;
ShowActiveProfile();
ShowActiveMSIabProfile();
});
});
}
Expand Down Expand Up @@ -359,16 +388,16 @@ private void Button_SaveProfiles_Click(object sender, RoutedEventArgs e)

var GetComboStockValue = Combo_Stock.SelectedIndex + 1;
var GetComboOCValue = Combo_OC.SelectedIndex + 1;
MyIni.Write("StockProfile", GetComboStockValue.ToString());
MyIni.Write("OCProfile", GetComboOCValue.ToString());
ConfigManager.IniWrite("StockProfile", GetComboStockValue.ToString());
ConfigManager.IniWrite("OCProfile", GetComboOCValue.ToString());

if (Checkbox_EnableMonitor.IsChecked == false)
{
MyIni.Write("IsMonitoringEnabled", "0");
ConfigManager.IniWrite("IsMonitoringEnabled", "0");
}
if (Checkbox_EnableMonitor.IsChecked == true)
{
MyIni.Write("IsMonitoringEnabled", "1");
ConfigManager.IniWrite("IsMonitoringEnabled", "1");
}

if (NeedRestart == false)
Expand Down Expand Up @@ -410,6 +439,12 @@ private void Button_OpenMSIAB_Click(object sender, RoutedEventArgs e)
MSIAB_Process.Start();
}

//set restart app flag when checkbox has been clicked/changed
private void Checkbox_EnableMonitor_Clicked(object sender, RoutedEventArgs e)
{
NeedRestart = true;
}

//MainWindow close application event handler
void MainWindow_Closed(object sender, EventArgs e)
{
Expand All @@ -418,10 +453,12 @@ void MainWindow_Closed(object sender, EventArgs e)
Close();
}

//set restart app flag when checkbox has been clicked/changed
private void Checkbox_EnableMonitor_Clicked(object sender, RoutedEventArgs e)
//reflect updated settings in UI when window is re-focused
private void Window_Activated(object sender, EventArgs e)
{
NeedRestart = true;
ReadSettings();
SetComboLists();
ShowActiveSoundProfile();
}
}
}
2 changes: 1 addition & 1 deletion MiToolz/MiToolz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="IniFile.cs" />
<Compile Include="ConfigManager.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down
2 changes: 1 addition & 1 deletion MiToolz/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("2.6.1.0")]
[assembly: AssemblyFileVersion("2.7.0.0")]
Loading

0 comments on commit c6346cf

Please sign in to comment.