├── docs ├── SBOutputController_Main.png └── SBOutputController_Setup.png ├── SBOutputController ├── output_toggle_icon.ico ├── Resources │ ├── output_toggle_icon.ico │ ├── baseline_error_black_36dp.png │ ├── baseline_error_white_36dp.png │ ├── baseline_headset_black_36dp.png │ ├── baseline_headset_white_36dp.png │ ├── baseline_speaker_black_36dp.png │ ├── baseline_speaker_white_36dp.png │ ├── headphones_speaker_toggle_black_36p.png │ └── headphones_speaker_toggle_white_36p.png ├── App.xaml.cs ├── App.xaml ├── EqualizerAPO.cs ├── HotKeyEditorControl.xaml ├── FileBrowseControl.xaml ├── SBOutputController.csproj.user ├── Properties │ ├── Settings.settings │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── app.manifest │ ├── Resources.resx │ └── Settings.Designer.cs ├── App.config ├── HotKeyEditorControl.xaml.cs ├── DependenciesSetup.xaml.cs ├── FileBrowseControl.xaml.cs ├── KeyboardHook.cs ├── HotKey.cs ├── DependenciesSetup.xaml ├── MainWindow.xaml ├── SBOutputController.csproj ├── SBController.cs └── MainWindow.xaml.cs ├── appveyor.yml ├── LICENSE ├── README.md ├── SBOutputController.sln └── .gitignore /docs/SBOutputController_Main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/docs/SBOutputController_Main.png -------------------------------------------------------------------------------- /docs/SBOutputController_Setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/docs/SBOutputController_Setup.png -------------------------------------------------------------------------------- /SBOutputController/output_toggle_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/output_toggle_icon.ico -------------------------------------------------------------------------------- /SBOutputController/Resources/output_toggle_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/output_toggle_icon.ico -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_error_black_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_error_black_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_error_white_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_error_white_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_headset_black_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_headset_black_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_headset_white_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_headset_white_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_speaker_black_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_speaker_black_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/baseline_speaker_white_36dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/baseline_speaker_white_36dp.png -------------------------------------------------------------------------------- /SBOutputController/Resources/headphones_speaker_toggle_black_36p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/headphones_speaker_toggle_black_36p.png -------------------------------------------------------------------------------- /SBOutputController/Resources/headphones_speaker_toggle_white_36p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heftyy/sb-output-controller/HEAD/SBOutputController/Resources/headphones_speaker_toggle_white_36p.png -------------------------------------------------------------------------------- /SBOutputController/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace SBOutputController 4 | { 5 | public partial class App : Application 6 | { 7 | public App() 8 | { 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /SBOutputController/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /SBOutputController/EqualizerAPO.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace SBOutputController 4 | { 5 | class EqualizerAPO 6 | { 7 | public static void UpdateEqualizer(DeviceOutputModes output_mode) 8 | { 9 | string source_path = output_mode == DeviceOutputModes.Headphones ? Properties.Settings.Default.EqualizerFilePathHeadphones : Properties.Settings.Default.EqualizerFilePathSpeakers; 10 | string target_path = Properties.Settings.Default.EqualizerFilePathConfig; 11 | 12 | string source_content = File.ReadAllText(source_path); 13 | File.WriteAllText(target_path, source_content); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: build-{build} 2 | configuration: Release 3 | skip_non_tags: true 4 | platform: x86 5 | before_build: 6 | - cmd: echo %APPVEYOR_REPO_TAG_NAME% > SBOutputController\version.txt 7 | build: 8 | verbosity: minimal 9 | after_build: 10 | - cmd: 7z a sb_output_controller_%APPVEYOR_REPO_TAG_NAME%.zip %APPVEYOR_BUILD_FOLDER%\SBOutputController\bin\x86\Release\* 11 | artifacts: 12 | - path: sb_output_controller_%APPVEYOR_REPO_TAG_NAME%.zip 13 | name: sb_output_controller 14 | deploy: 15 | - provider: GitHub 16 | auth_token: 17 | secure: NmYiWU0cS/AkZpWQ9O12oJsMbB30T0fpylZlc3yK60Lbn+ZIpaPDAsDIZ4QAFqY/ 18 | artifact: sb_output_controller 19 | on: 20 | branch: main 21 | -------------------------------------------------------------------------------- /SBOutputController/HotKeyEditorControl.xaml: -------------------------------------------------------------------------------- 1 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Szymon Nowik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /SBOutputController/FileBrowseControl.xaml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 85 | 90 | 95 | 96 | 97 | 98 | 99 | 100 | 102 | 103 | 105 | 106 | 109 | 112 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /SBOutputController/SBOutputController.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {BEE89AB0-22F1-432C-8A11-16251BF2686F} 8 | WinExe 9 | SBOutputController 10 | SBOutputController 11 | v4.7.2 12 | 512 13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 4 15 | true 16 | true 17 | false 18 | E:\SBOutputController\ 19 | true 20 | Disk 21 | false 22 | Foreground 23 | 7 24 | Days 25 | false 26 | false 27 | true 28 | en-001 29 | SBOutputController 30 | SZNO 31 | SBOutputController 32 | true 33 | 0 34 | 1.0.0.%2a 35 | false 36 | true 37 | 38 | 39 | AnyCPU 40 | true 41 | full 42 | false 43 | bin\Debug\ 44 | DEBUG;TRACE 45 | prompt 46 | 4 47 | 48 | 49 | false 50 | true 51 | 52 | 53 | AnyCPU 54 | pdbonly 55 | true 56 | bin\Release\ 57 | TRACE 58 | prompt 59 | 4 60 | 61 | 62 | true 63 | bin\x86\Debug\ 64 | DEBUG;TRACE 65 | full 66 | x86 67 | 7.3 68 | prompt 69 | MinimumRecommendedRules.ruleset 70 | true 71 | 72 | 73 | bin\x86\Release\ 74 | TRACE 75 | true 76 | pdbonly 77 | x86 78 | 7.3 79 | prompt 80 | MinimumRecommendedRules.ruleset 81 | true 82 | 83 | 84 | false 85 | 86 | 87 | true 88 | 89 | 90 | false 91 | 92 | 93 | SBOutputController.App 94 | 95 | 96 | output_toggle_icon.ico 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 4.0 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | MSBuild:Compile 123 | Designer 124 | 125 | 126 | 127 | FileBrowseControl.xaml 128 | 129 | 130 | 131 | HotKeyEditorControl.xaml 132 | 133 | 134 | 135 | True 136 | True 137 | Resources.resx 138 | 139 | 140 | 141 | DependenciesSetup.xaml 142 | 143 | 144 | Designer 145 | MSBuild:Compile 146 | 147 | 148 | Designer 149 | MSBuild:Compile 150 | 151 | 152 | MSBuild:Compile 153 | Designer 154 | 155 | 156 | App.xaml 157 | Code 158 | 159 | 160 | MainWindow.xaml 161 | Code 162 | 163 | 164 | Designer 165 | MSBuild:Compile 166 | 167 | 168 | 169 | 170 | Code 171 | 172 | 173 | True 174 | Settings.settings 175 | True 176 | 177 | 178 | SettingsSingleFileGenerator 179 | Settings.Designer.cs 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | False 188 | Microsoft .NET Framework 4.7.2 %28x86 and x64%29 189 | true 190 | 191 | 192 | False 193 | .NET Framework 3.5 SP1 194 | false 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | PreserveNewest 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | ResXFileCodeGenerator 222 | Resources.Designer.cs 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | -------------------------------------------------------------------------------- /SBOutputController/SBController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace SBOutputController 8 | { 9 | public class DeviceWrapper 10 | { 11 | public DeviceWrapper(dynamic device) 12 | { 13 | NativeDevice = device; 14 | DeviceName = device.DeviceName; 15 | } 16 | 17 | public string DeviceName { get; } 18 | public dynamic NativeDevice { get; } 19 | } 20 | 21 | class NoDevicesFound : Exception 22 | { 23 | public NoDevicesFound(string message) 24 | : base(message) 25 | { } 26 | } 27 | 28 | class MethodNotFound : Exception 29 | { 30 | public MethodNotFound(string message) 31 | : base(message) 32 | { } 33 | } 34 | 35 | enum DeviceOutputModes : uint 36 | { 37 | Speakers = 2U, 38 | Headphones = 4U 39 | } 40 | 41 | enum DirectModeStates : uint 42 | { 43 | Off = 0U, 44 | On = 1U 45 | } 46 | 47 | class OutputModeChangedEventArgs : EventArgs 48 | { 49 | public OutputModeChangedEventArgs(DeviceOutputModes output_mode) 50 | { 51 | OutputMode = output_mode; 52 | } 53 | 54 | public DeviceOutputModes OutputMode { get; } 55 | } 56 | 57 | public enum DLLLoadedStatus 58 | { 59 | Missing, 60 | Registered 61 | } 62 | 63 | public readonly struct SBRequiredDLL 64 | { 65 | public SBRequiredDLL(string cls_id, string relative_path, string full_path, DLLLoadedStatus dll_status) 66 | { 67 | ClsId = cls_id; 68 | RelativePath = relative_path; 69 | FullPath = full_path; 70 | Status = dll_status; 71 | } 72 | 73 | public string ClsId { get; } 74 | public string RelativePath { get; } 75 | public string FullPath { get; } 76 | public DLLLoadedStatus Status { get; } 77 | } 78 | 79 | class SBController 80 | { 81 | public static readonly Dictionary RequiredDllsList = new Dictionary() 82 | { 83 | { "{495E4C24-85ED-4f19-885E-C2D01D7EA26C}", @"Platform\SndCrUSB.dll" } 84 | }; 85 | 86 | public static string SbConnectExecutable = @"Creative.SBCommand.exe"; 87 | public static string SbConnectPath = @"C:\Program Files (x86)\Creative\Sound Blaster Command\"; 88 | 89 | public event EventHandler OutputModeChangedEvent; 90 | 91 | public SBController(string sb_directory) 92 | { 93 | _sbDirectory = sb_directory; 94 | _activeDevice = null; 95 | 96 | Initialize(); 97 | } 98 | 99 | private void Initialize() 100 | { 101 | string log4net_dll_path = Path.Combine(_sbDirectory, "Package", "log4net.dll"); 102 | string devices_dll_path = Path.Combine(_sbDirectory, "Platform", "Creative.Platform.Devices.dll"); 103 | 104 | Assembly.LoadFrom(log4net_dll_path); 105 | _sbDevicesDLL = Assembly.LoadFrom(devices_dll_path); 106 | Type device_manager_type = _sbDevicesDLL.GetType("Creative.Platform.Devices.Models.DeviceManager", true); 107 | Type device_endpoint_selection_service_type = _sbDevicesDLL.GetType("Creative.Platform.Devices.Selections.DeviceEndpointSelectionService", true); 108 | 109 | MethodInfo device_manager_instance_method = device_manager_type.GetMethod("get_Instance"); 110 | if (device_manager_instance_method == null) 111 | { 112 | throw new MethodNotFound("Failed to find DeviceManager.Instance"); 113 | } 114 | _deviceManager = device_manager_instance_method.Invoke(null, null); 115 | _deviceManager.Initialize(); 116 | 117 | if (_deviceManager.DiscoveredDevices.Count == 0) 118 | { 119 | throw new NoDevicesFound("No devices found"); 120 | } 121 | 122 | MethodInfo endpoint_service_instance_method = device_endpoint_selection_service_type.GetMethod("get_Instance"); 123 | if (endpoint_service_instance_method == null) 124 | { 125 | throw new MethodNotFound("Failed to find DeviceEndpointSelectionService.Instance"); 126 | } 127 | _deviceEndpointSelectionService = endpoint_service_instance_method.Invoke(null, null); 128 | } 129 | 130 | public static bool VerifySetup(string exe_path) 131 | { 132 | if (!VerifyExecutablePath(exe_path)) 133 | return false; 134 | 135 | string sb_directory = Path.GetDirectoryName(exe_path); 136 | List required_dll_status = GetRequiredDLLs(sb_directory); 137 | 138 | return required_dll_status.All(dll => dll.Status == DLLLoadedStatus.Registered); 139 | } 140 | 141 | public static bool VerifyExecutablePath(string exe_path) 142 | { 143 | return exe_path != null && exe_path.EndsWith(SbConnectExecutable) && File.Exists(exe_path); 144 | } 145 | 146 | public static List GetRequiredDLLs(string sb_directory) 147 | { 148 | var result = new List(); 149 | 150 | foreach(var item in RequiredDllsList) 151 | { 152 | using (var classes_root_key = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, Microsoft.Win32.RegistryView.Default)) 153 | { 154 | string cls_id = item.Key; 155 | 156 | var cls_id_key = 157 | classes_root_key.OpenSubKey(@"Wow6432Node\CLSID\" + cls_id) ?? 158 | classes_root_key.OpenSubKey(@"CLSID\" + cls_id); 159 | 160 | string expected_dll_full_path = Path.Combine(sb_directory, item.Value); 161 | 162 | if (cls_id_key == null) 163 | { 164 | result.Add(new SBRequiredDLL(cls_id, item.Value, expected_dll_full_path, DLLLoadedStatus.Missing)); 165 | continue; 166 | } 167 | 168 | var inproc_key = cls_id_key.OpenSubKey("InprocServer32"); 169 | cls_id_key.Dispose(); 170 | if (inproc_key == null) 171 | { 172 | result.Add(new SBRequiredDLL(cls_id, item.Value, expected_dll_full_path, DLLLoadedStatus.Missing)); 173 | continue; 174 | } 175 | 176 | string reg_dll_full_path = inproc_key.GetValue(null).ToString(); 177 | inproc_key.Dispose(); 178 | 179 | if (reg_dll_full_path != null && string.Compare(reg_dll_full_path, expected_dll_full_path, true) == 0) 180 | { 181 | result.Add(new SBRequiredDLL(cls_id, item.Value, expected_dll_full_path, DLLLoadedStatus.Registered)); 182 | } 183 | else 184 | { 185 | result.Add(new SBRequiredDLL(cls_id, item.Value, expected_dll_full_path, DLLLoadedStatus.Missing)); 186 | } 187 | } 188 | } 189 | 190 | return result; 191 | } 192 | 193 | public void SetActiveDevice(DeviceWrapper device_wrapper) 194 | { 195 | if (_activeDevice == null || _activeDevice.NativeDevice != device_wrapper.NativeDevice) 196 | { 197 | _activeDevice = device_wrapper; 198 | _outputModeFeature = _deviceEndpointSelectionService.GetAggregatedFeature(device_wrapper.NativeDevice, "MultiplexOutputFeatureId"); 199 | _stereoDirectFeature = _deviceEndpointSelectionService.GetAggregatedFeature(device_wrapper.NativeDevice, "StereoDirectFeatureId"); 200 | 201 | Type value_changed_delegate_type = _sbDevicesDLL.GetType("Creative.Platform.Devices.Selections.EffectParameterValueChangedDelegate"); 202 | dynamic value_changed_delegate = Delegate.CreateDelegate(value_changed_delegate_type, this, "OnOutputModeChanged"); 203 | // EffectParameterValueChangedHanlder is the "correct" spelling.... Thanks Creative 204 | _outputModeFeature.EffectParameterValueChangedHanlder += value_changed_delegate; 205 | } 206 | } 207 | 208 | public List GetDevices() 209 | { 210 | List devices = new List(); 211 | 212 | foreach (var device in _deviceManager.DiscoveredDevices) 213 | { 214 | devices.Add(new DeviceWrapper(device)); 215 | } 216 | 217 | return devices; 218 | } 219 | 220 | public void OnOutputModeChanged(object parameters) 221 | { 222 | if (_activeDevice != null) 223 | { 224 | DeviceOutputModes output_mode = GetOutputModeForDevice(_activeDevice); 225 | OutputModeChangedEvent(this, new OutputModeChangedEventArgs(output_mode)); 226 | } 227 | } 228 | 229 | public DeviceOutputModes GetOutputModeForDevice(DeviceWrapper device_wrapper) 230 | { 231 | SetActiveDevice(device_wrapper); 232 | 233 | uint output_mode_value = _outputModeFeature.GetValue("MultiplexOutputParameterId"); 234 | return (DeviceOutputModes)output_mode_value; 235 | } 236 | 237 | public void SwitchToOutputMode(DeviceWrapper device_wrapper, DeviceOutputModes requested_output_mode_enum) 238 | { 239 | SetActiveDevice(device_wrapper); 240 | 241 | uint requested_output_mode = (uint)requested_output_mode_enum; 242 | uint output_mode_value = _outputModeFeature.GetValue("MultiplexOutputParameterId"); 243 | if (output_mode_value != requested_output_mode) 244 | { 245 | _outputModeFeature.SetValue(requested_output_mode, "MultiplexOutputParameterId"); 246 | } 247 | } 248 | 249 | public void SwitchDirectMode(DeviceWrapper device_wrapper, DirectModeStates requested_direct_mode_enum) 250 | { 251 | SetActiveDevice(device_wrapper); 252 | 253 | uint requested_direct_mode = (uint)requested_direct_mode_enum; 254 | uint direct_mode_value = _stereoDirectFeature.GetValue("StereoDirectParameterId"); 255 | if (direct_mode_value != requested_direct_mode) 256 | { 257 | _stereoDirectFeature.SetValue(requested_direct_mode, "StereoDirectParameterId"); 258 | } 259 | } 260 | 261 | private Assembly _sbDevicesDLL; 262 | 263 | private dynamic _deviceManager; 264 | private dynamic _deviceEndpointSelectionService; 265 | private dynamic _outputModeFeature; 266 | private dynamic _stereoDirectFeature; 267 | 268 | private readonly string _sbDirectory; 269 | 270 | private DeviceWrapper _activeDevice; 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /SBOutputController/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.IO; 5 | using System.Reflection; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Forms; 9 | using System.Windows.Media; 10 | using ContextMenu = System.Windows.Forms.ContextMenu; 11 | 12 | namespace SBOutputController 13 | { 14 | public partial class MainWindow 15 | { 16 | private SBController _sbConnectApi = null; 17 | private readonly KeyboardHook _keyboardHook = new KeyboardHook(); 18 | private readonly NotifyIcon _notifyIcon = new NotifyIcon(); 19 | private readonly ContextMenu _notifyIconContextMenu = new ContextMenu(); 20 | 21 | private string SBDirectory 22 | { 23 | get => Path.GetDirectoryName(Properties.Settings.Default.SBExecutablePath); 24 | } 25 | 26 | static readonly private Color StatusSuccess = Color.FromRgb(85, 189, 90); 27 | static readonly private Color StatusFailure = Color.FromRgb(189, 92, 92); 28 | 29 | public MainWindow() 30 | { 31 | InitializeComponent(); 32 | 33 | string version = "Version unknown"; 34 | Assembly this_assembly = Assembly.GetExecutingAssembly(); 35 | Stream stream = this_assembly.GetManifestResourceStream(this_assembly.GetName().Name + ".version.txt"); 36 | if (stream != null) 37 | { 38 | using (stream) 39 | using (StreamReader reader = new StreamReader(stream)) 40 | { 41 | version = reader.ReadToEnd(); 42 | } 43 | } 44 | 45 | Title += " - " + version; 46 | 47 | _keyboardHook.KeyPressed += KeyboardHook_HotKeyPressed; 48 | Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; 49 | 50 | HotKeyOutputHeadphones.HotKey = Properties.Settings.Default.HotKeyOutputHeadphones; 51 | HotKeyOutputSpeakers.HotKey = Properties.Settings.Default.HotKeyOutputSpeakers; 52 | HotKeyOutputToggle.HotKey = Properties.Settings.Default.HotKeyOutputToggle; 53 | 54 | var show_menu_item = new System.Windows.Forms.MenuItem 55 | { 56 | Index = 0, 57 | Text = "&Show", 58 | }; 59 | show_menu_item.Click += Window_Show; 60 | 61 | var exit_menu_item = new System.Windows.Forms.MenuItem 62 | { 63 | Index = 0, 64 | Text = "E&xit" 65 | }; 66 | exit_menu_item.Click += Window_Shutdown; 67 | 68 | _notifyIconContextMenu.MenuItems.Add(show_menu_item); 69 | _notifyIconContextMenu.MenuItems.Add(exit_menu_item); 70 | 71 | _notifyIcon.Icon = Properties.Resources.output_toggle_icon; 72 | _notifyIcon.Visible = false; 73 | _notifyIcon.Text = "SB Output Controller"; 74 | _notifyIcon.DoubleClick += Window_Show; 75 | _notifyIcon.ContextMenu = _notifyIconContextMenu; 76 | 77 | CheckboxEqualizerAPO_Changed(this, null); 78 | } 79 | 80 | private void InitializeSBConnect() 81 | { 82 | Directory.SetCurrentDirectory(SBDirectory); 83 | 84 | _sbConnectApi = new SBController(SBDirectory); 85 | _sbConnectApi.OutputModeChangedEvent += SBConnect_OutputModeChanged; 86 | 87 | ListDevices.Items.Clear(); 88 | foreach (DeviceWrapper device_wrapper in _sbConnectApi.GetDevices()) 89 | { 90 | ListDevices.Items.Add(device_wrapper); 91 | if (device_wrapper.DeviceName == Properties.Settings.Default.LastSelectedDevice) 92 | { 93 | ListDevices.SelectedItem = device_wrapper; 94 | } 95 | } 96 | } 97 | 98 | private void Window_ContentRendered(object sender, EventArgs e) 99 | { 100 | if (!SBController.VerifySetup(Properties.Settings.Default.SBExecutablePath)) 101 | { 102 | ButtonOpenSetup_Click(this, null); 103 | } 104 | else 105 | { 106 | SetupChanged(Properties.Settings.Default.SBExecutablePath); 107 | } 108 | } 109 | 110 | private void Window_Show(object sender, EventArgs args) 111 | { 112 | _notifyIcon.Visible = false; 113 | 114 | Show(); 115 | Activate(); 116 | WindowState = WindowState.Normal; 117 | } 118 | 119 | private void Window_Closing(object sender, CancelEventArgs e) 120 | { 121 | _notifyIcon.Visible = true; 122 | 123 | e.Cancel = true; 124 | Hide(); 125 | } 126 | 127 | private void Window_Shutdown(object sender, EventArgs args) 128 | { 129 | _notifyIcon.Dispose(); 130 | Properties.Settings.Default.Save(); 131 | System.Windows.Application.Current.Shutdown(); 132 | } 133 | 134 | private void KeyboardHook_HotKeyPressed(object sender, KeyPressedEventArgs e) 135 | { 136 | HotKey hotkey_pressed = e.HotKey; 137 | if (hotkey_pressed == Properties.Settings.Default.HotKeyOutputHeadphones) 138 | { 139 | ButtonSwitchToHeadphones_Click(sender, null); 140 | } 141 | else if (hotkey_pressed == Properties.Settings.Default.HotKeyOutputSpeakers) 142 | { 143 | ButtonSwitchToSpeakers_Click(sender, null); 144 | } 145 | else if (hotkey_pressed == Properties.Settings.Default.HotKeyOutputToggle) 146 | { 147 | ButtonToggleOutput_Click(sender, null); 148 | } 149 | } 150 | 151 | private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e) 152 | { 153 | if (e.Mode == Microsoft.Win32.PowerModes.Resume) 154 | { 155 | // Seems like soundblaster needs a little bit of a delay after waking up to be active 156 | // For now I set it to 5s to be on the safe side 157 | System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); 158 | timer.Tick += (_, __) => 159 | { 160 | timer.Stop(); 161 | InitializeSBConnect(); 162 | }; 163 | timer.Interval = new TimeSpan(0, 0, 5); 164 | timer.Start(); 165 | } 166 | } 167 | 168 | private void ButtonOpenSetup_Click(object sender, RoutedEventArgs e) 169 | { 170 | DependenciesSetup setup_window = new DependenciesSetup(Properties.Settings.Default.SBExecutablePath) 171 | { 172 | Owner = this 173 | }; 174 | setup_window.ShowDialog(); 175 | 176 | string exe_path = setup_window.FileBrowserSBExecutablePath.FilePath; 177 | SetupChanged(exe_path); 178 | } 179 | 180 | private void SetupChanged(string exe_path) 181 | { 182 | TextBoxSetupStatus.Background = new SolidColorBrush(StatusFailure); 183 | PanelSBConnect.IsEnabled = false; 184 | if (SBController.VerifySetup(exe_path)) 185 | { 186 | Properties.Settings.Default.SBExecutablePath = exe_path; 187 | 188 | try 189 | { 190 | InitializeSBConnect(); 191 | 192 | TextBoxSetupStatus.Text = string.Format("Setup Status"); 193 | TextBoxSetupStatus.Background = new SolidColorBrush(StatusSuccess); 194 | PanelSBConnect.IsEnabled = true; 195 | } 196 | catch (Exception ex) 197 | { 198 | TextBoxSetupStatus.Text = string.Format("Setup Status - {0}", ex.Message); 199 | } 200 | } 201 | } 202 | 203 | private void ListDevices_SelectionChanged(object sender, SelectionChangedEventArgs e) 204 | { 205 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 206 | if (device_wrapper == null || device_wrapper.DeviceName != "G6") 207 | { 208 | GridOutputButtonsAndHotKeys.IsEnabled = false; 209 | return; 210 | } 211 | 212 | DeviceOutputModes output_mode = _sbConnectApi.GetOutputModeForDevice(device_wrapper); 213 | UpdateDeviceOuputModeView(output_mode); 214 | GridOutputButtonsAndHotKeys.IsEnabled = true; 215 | Properties.Settings.Default.LastSelectedDevice = device_wrapper.DeviceName; 216 | } 217 | 218 | private void SBConnect_OutputModeChanged(object sender, OutputModeChangedEventArgs e) 219 | { 220 | UpdateDeviceOuputModeView(e.OutputMode); 221 | EqualizerConfig_FilePathChanged(this, null); 222 | UpdateDirectMode(); 223 | } 224 | 225 | private void UpdateDeviceOuputModeView(DeviceOutputModes output_mode) 226 | { 227 | List output_mode_images = new List 228 | { 229 | OutputModeHeadphonesImage, 230 | OutputModeSpeakersImage, 231 | OutputModeErrorImage 232 | }; 233 | 234 | void set_visible_image(Image visible_image) 235 | { 236 | foreach (Image image in output_mode_images) 237 | { 238 | image.Visibility = image == visible_image ? Visibility.Visible : Visibility.Hidden; 239 | } 240 | } 241 | 242 | switch (output_mode) 243 | { 244 | case DeviceOutputModes.Headphones: 245 | set_visible_image(OutputModeHeadphonesImage); 246 | break; 247 | case DeviceOutputModes.Speakers: 248 | set_visible_image(OutputModeSpeakersImage); 249 | break; 250 | default: 251 | set_visible_image(OutputModeErrorImage); 252 | break; 253 | } 254 | } 255 | 256 | private void CheckboxEqualizerAPO_Changed(object sender, RoutedEventArgs e) 257 | { 258 | if (!IsInitialized) 259 | return; 260 | 261 | bool is_checked = CheckboxEqualizerAPO.IsChecked == true; 262 | FileBrowserHeadphonesConfig.IsEnabled = is_checked; 263 | FileBrowserSpeakersConfig.IsEnabled = is_checked; 264 | FileBrowserTargetConfig.IsEnabled = is_checked; 265 | 266 | EqualizerConfig_FilePathChanged(this, null); 267 | } 268 | 269 | private void ButtonSwitchToHeadphones_Click(object sender, RoutedEventArgs e) 270 | { 271 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 272 | if (device_wrapper != null) 273 | { 274 | _sbConnectApi.SwitchToOutputMode(device_wrapper, DeviceOutputModes.Headphones); 275 | } 276 | } 277 | 278 | private void ButtonSwitchToSpeakers_Click(object sender, RoutedEventArgs e) 279 | { 280 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 281 | if (device_wrapper != null) 282 | { 283 | _sbConnectApi.SwitchToOutputMode(device_wrapper, DeviceOutputModes.Speakers); 284 | } 285 | } 286 | 287 | private void ButtonToggleOutput_Click(object sender, RoutedEventArgs e) 288 | { 289 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 290 | if (device_wrapper != null) 291 | { 292 | DeviceOutputModes output_mode = _sbConnectApi.GetOutputModeForDevice(device_wrapper); 293 | DeviceOutputModes desired_output_mode = output_mode == DeviceOutputModes.Headphones ? DeviceOutputModes.Speakers : DeviceOutputModes.Headphones; 294 | _sbConnectApi.SwitchToOutputMode(device_wrapper, desired_output_mode); 295 | } 296 | } 297 | 298 | private void HotKeyHeadphonesOutput_HotKeyChanged(object sender, RoutedEventArgs e) 299 | { 300 | _keyboardHook.UnregisterHotKey(Properties.Settings.Default.HotKeyOutputHeadphones); 301 | _keyboardHook.RegisterHotKey(HotKeyOutputHeadphones.HotKey); 302 | Properties.Settings.Default.HotKeyOutputHeadphones = HotKeyOutputHeadphones.HotKey; 303 | ListDevices.Focus(); 304 | } 305 | private void HotKeySpeakersOutput_HotKeyChanged(object sender, RoutedEventArgs e) 306 | { 307 | _keyboardHook.UnregisterHotKey(Properties.Settings.Default.HotKeyOutputSpeakers); 308 | _keyboardHook.RegisterHotKey(HotKeyOutputSpeakers.HotKey); 309 | Properties.Settings.Default.HotKeyOutputSpeakers = HotKeyOutputSpeakers.HotKey; 310 | ListDevices.Focus(); 311 | } 312 | private void HotKeyToggleOutput_HotKeyChanged(object sender, RoutedEventArgs e) 313 | { 314 | _keyboardHook.UnregisterHotKey(Properties.Settings.Default.HotKeyOutputToggle); 315 | _keyboardHook.RegisterHotKey(HotKeyOutputToggle.HotKey); 316 | Properties.Settings.Default.HotKeyOutputToggle = HotKeyOutputToggle.HotKey; 317 | ListDevices.Focus(); 318 | } 319 | 320 | private void EqualizerConfig_FilePathChanged(object sender, RoutedEventArgs e) 321 | { 322 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 323 | if (device_wrapper == null || CheckboxEqualizerAPO.IsChecked == false) 324 | { 325 | return; 326 | } 327 | 328 | try 329 | { 330 | EqualizerAPO.UpdateEqualizer(_sbConnectApi.GetOutputModeForDevice(device_wrapper)); 331 | FileBrowserTargetConfig.ToolTip = "Target config - config.txt by default"; 332 | FileBrowserTargetConfig.TextBoxFilePath.Background = new SolidColorBrush(Colors.Transparent); 333 | FileBrowserTargetConfig.TextBoxFilePath.Foreground = new SolidColorBrush(Colors.SlateGray); 334 | } 335 | catch (Exception ex) 336 | { 337 | FileBrowserTargetConfig.ToolTip = string.Format("Target config - config.txt by default\n\nUpdating the EQ config file failed.\nError: {0}", ex.Message); 338 | FileBrowserTargetConfig.TextBoxFilePath.Background = new SolidColorBrush(StatusFailure); 339 | FileBrowserTargetConfig.TextBoxFilePath.Foreground = new SolidColorBrush(Colors.White); 340 | } 341 | } 342 | 343 | private void UpdateDirectMode() 344 | { 345 | DeviceWrapper device_wrapper = (DeviceWrapper)ListDevices.SelectedItem; 346 | if (device_wrapper != null) 347 | { 348 | DeviceOutputModes output_mode = _sbConnectApi.GetOutputModeForDevice(device_wrapper); 349 | switch (output_mode) 350 | { 351 | case DeviceOutputModes.Headphones: 352 | { 353 | bool direct_mode_enabled = Properties.Settings.Default.HeadphonesDirectMode; 354 | _sbConnectApi.SwitchDirectMode(device_wrapper, direct_mode_enabled ? DirectModeStates.On : DirectModeStates.Off); 355 | break; 356 | } 357 | case DeviceOutputModes.Speakers: 358 | { 359 | bool direct_mode_enabled = Properties.Settings.Default.SpeakersDirectMode; 360 | _sbConnectApi.SwitchDirectMode(device_wrapper, direct_mode_enabled ? DirectModeStates.On : DirectModeStates.Off); 361 | break; 362 | } 363 | default: 364 | break; 365 | } 366 | } 367 | } 368 | 369 | private void CheckboxHeadphonesDirectMode_Changed(object sender, RoutedEventArgs e) 370 | { 371 | Properties.Settings.Default.HeadphonesDirectMode = HeadphonesDirectMode.IsChecked == true; 372 | UpdateDirectMode(); 373 | } 374 | 375 | private void CheckboxSpeakersDirectMode_Changed(object sender, RoutedEventArgs e) 376 | { 377 | Properties.Settings.Default.SpeakersDirectMode = SpeakersDirectMode.IsChecked == true; 378 | UpdateDirectMode(); 379 | } 380 | } 381 | } 382 | --------------------------------------------------------------------------------