2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
33 using System.Windows.Forms;
35 using System.Collections.Generic;
36 using LibCECTray.Properties;
37 using Microsoft.Win32;
39 namespace LibCECTray.settings
43 private const string RegistryCompanyName = "Pulse-Eight";
44 private const string RegistryApplicationName = "libCECTray";
47 public static string KeyHDMIPort = "global_hdmi_port";
48 public static string KeyConnectedToHDMIDevice = "global_connected_to_hdmi_device";
49 public static string KeyActivateSource = "global_activate_source";
50 public static string KeyAdvancedMode = "global_advanced_mode";
51 public static string KeyPhysicalAddress = "global_physical_address";
52 public static string KeyOverridePhysicalAddress = "global_override_physical_address";
53 public static string KeyDeviceType = "global_device_type";
54 public static string KeyTVVendor = "global_tv_vendor";
55 public static string KeyOverrideTVVendor = "global_override_tv_vendor";
56 public static string KeyWakeDevices = "global_wake_devices";
57 public static string KeyPowerOffDevices = "global_standby_devices";
58 public static string KeyStartHidden = "global_start_hidden";
61 public CECSettings(CECSetting.SettingChangedHandler changedHandler)
63 _changedHandler = changedHandler;
68 /// Resets all settings to their default values
70 public void SetDefaultValues()
72 foreach (var setting in _settings.Values)
73 setting.ResetDefaultValue();
77 /// Loads all known settings from the registry
81 using (var subRegKey = Registry.CurrentUser.OpenSubKey(RegistryKeyName, true))
83 if (subRegKey == null) return;
84 foreach (var setting in _settings.Values)
85 if (!setting.KeyName.StartsWith("global_"))
86 setting.Load(subRegKey);
92 /// Load a setting value from the registry
94 /// <param name="setting">The setting to load</param>
95 public void Load(CECSetting setting)
97 using (var subRegKey = Registry.CurrentUser.OpenSubKey(RegistryKeyName, true))
99 if (subRegKey == null) return;
100 setting.Load(subRegKey);
103 setting.SettingChanged += OnSettingChanged;
107 /// Persist all settings in the registry
109 /// <returns>True when persisted, false otherwise</returns>
110 public bool Persist()
112 if (CreateRegistryKey())
114 using (var subRegKey = Registry.CurrentUser.OpenSubKey(RegistryKeyName, true))
116 if (subRegKey != null)
118 foreach (var setting in _settings.Values)
119 setting.Persist(subRegKey);
128 private bool EnableHDMIPortSetting(CECSetting setting, bool value)
130 return value && !OverridePhysicalAddress.Value;
133 private bool EnablePhysicalAddressSetting(CECSetting setting, bool value)
135 return value && OverridePhysicalAddress.Value;
138 private bool EnableSettingTVVendor(CECSetting setting, bool value)
140 return value && OverrideTVVendor.Value;
143 private void OnSettingChanged(CECSetting setting, object oldValue, object newValue)
145 if (SettingChanged != null)
146 SettingChanged(setting, oldValue, newValue);
149 #region Global settings
150 public CECSettingByte HDMIPort
154 if (!_settings.ContainsKey(KeyHDMIPort))
156 CECSettingByte setting = new CECSettingByte(KeyHDMIPort, "HDMI port", 1, _changedHandler) { LowerLimit = 1, UpperLimit = 15, EnableSetting = EnableHDMIPortSetting };
157 setting.Format += delegate(object sender, ListControlConvertEventArgs args)
160 if (ushort.TryParse((string)args.Value, out tmp))
161 args.Value = "HDMI " + args.Value;
165 _settings[KeyHDMIPort] = setting;
167 return _settings[KeyHDMIPort].AsSettingByte;
171 public CECSettingLogicalAddress ConnectedDevice
175 if (!_settings.ContainsKey(KeyConnectedToHDMIDevice))
177 CecLogicalAddresses allowedMask = new CecLogicalAddresses();
178 allowedMask.Set(CecLogicalAddress.Tv); allowedMask.Set(CecLogicalAddress.AudioSystem);
179 CECSettingLogicalAddress setting = new CECSettingLogicalAddress(KeyConnectedToHDMIDevice,
180 Resources.global_connected_to_hdmi_device,
181 CecLogicalAddress.Tv, _changedHandler)
183 AllowedAddressMask = allowedMask,
185 EnableSetting = EnableHDMIPortSetting
188 _settings[KeyConnectedToHDMIDevice] = setting;
190 return _settings[KeyConnectedToHDMIDevice].AsSettingLogicalAddress;
194 public CECSettingBool ActivateSource
198 if (!_settings.ContainsKey(KeyActivateSource))
200 CECSettingBool setting = new CECSettingBool(KeyActivateSource, Resources.global_activate_source, true,
201 _changedHandler) {Enabled = false};
203 _settings[KeyActivateSource] = setting;
205 return _settings[KeyActivateSource].AsSettingBool;
209 public CECSettingBool AdvancedMode
213 if (!_settings.ContainsKey(KeyAdvancedMode))
215 CECSettingBool setting = new CECSettingBool(KeyAdvancedMode, Resources.global_advanced_mode, false,
216 _changedHandler) {Enabled = false};
218 _settings[KeyAdvancedMode] = setting;
220 return _settings[KeyAdvancedMode].AsSettingBool;
224 public CECSettingUShort PhysicalAddress
228 if (!_settings.ContainsKey(KeyPhysicalAddress))
230 CECSettingUShort setting = new CECSettingUShort(KeyPhysicalAddress, Resources.global_physical_address, 0xFFFF, _changedHandler) { Enabled = false, EnableSetting = EnablePhysicalAddressSetting };
232 _settings[KeyPhysicalAddress] = setting;
234 return _settings[KeyPhysicalAddress].AsSettingUShort;
238 public CECSettingBool OverridePhysicalAddress
242 if (!_settings.ContainsKey(KeyAdvancedMode))
244 CECSettingBool setting = new CECSettingBool(KeyOverridePhysicalAddress,
245 Resources.global_override_physical_address, false, _changedHandler);
247 _settings[KeyOverridePhysicalAddress] = setting;
249 return _settings[KeyOverridePhysicalAddress].AsSettingBool;
253 public CECSettingDeviceType DeviceType
257 if (!_settings.ContainsKey(KeyDeviceType))
259 CecDeviceTypeList allowedTypes = new CecDeviceTypeList();
260 allowedTypes.Types[(int)CecDeviceType.RecordingDevice] = CecDeviceType.RecordingDevice;
261 allowedTypes.Types[(int)CecDeviceType.PlaybackDevice] = CecDeviceType.PlaybackDevice;
263 CECSettingDeviceType setting = new CECSettingDeviceType(KeyDeviceType, Resources.global_device_type,
264 CecDeviceType.RecordingDevice, _changedHandler) { Enabled = false, AllowedTypeMask = allowedTypes };
266 _settings[KeyDeviceType] = setting;
268 return _settings[KeyDeviceType].AsSettingDeviceType;
272 public CECSettingVendorId TVVendor
276 if (!_settings.ContainsKey(KeyTVVendor))
278 CECSettingVendorId setting = new CECSettingVendorId(KeyTVVendor, Resources.global_tv_vendor,
279 CecVendorId.Unknown, _changedHandler)
280 {Enabled = false, EnableSetting = EnableSettingTVVendor};
282 _settings[KeyTVVendor] = setting;
284 return _settings[KeyTVVendor].AsSettingVendorId;
288 public CECSettingBool OverrideTVVendor
292 if (!_settings.ContainsKey(KeyOverrideTVVendor))
294 CECSettingBool setting = new CECSettingBool(KeyOverrideTVVendor, Resources.global_override_tv_vendor, false,
295 _changedHandler) {Enabled = false};
297 _settings[KeyOverrideTVVendor] = setting;
299 return _settings[KeyOverrideTVVendor].AsSettingBool;
303 public CECSettingLogicalAddresses WakeDevices
307 if (!_settings.ContainsKey(KeyWakeDevices))
309 CecLogicalAddresses defaultDeviceList = new CecLogicalAddresses();
310 defaultDeviceList.Set(CecLogicalAddress.Tv);
311 CECSettingLogicalAddresses setting = new CECSettingLogicalAddresses(KeyWakeDevices,
312 Resources.global_wake_devices,
313 defaultDeviceList, _changedHandler)
316 _settings[KeyWakeDevices] = setting;
318 return _settings[KeyWakeDevices].AsSettingLogicalAddresses;
322 public CECSettingLogicalAddresses PowerOffDevices
326 if (!_settings.ContainsKey(KeyPowerOffDevices))
328 CecLogicalAddresses defaultDeviceList = new CecLogicalAddresses();
329 defaultDeviceList.Set(CecLogicalAddress.Tv);
330 CECSettingLogicalAddresses setting = new CECSettingLogicalAddresses(KeyPowerOffDevices,
331 Resources.global_standby_devices,
333 _changedHandler) {Enabled = false};
335 _settings[KeyPowerOffDevices] = setting;
337 return _settings[KeyPowerOffDevices].AsSettingLogicalAddresses;
341 public CECSettingBool StartHidden
345 if (!_settings.ContainsKey(KeyStartHidden))
347 CECSettingBool setting = new CECSettingBool(KeyStartHidden, Resources.global_start_hidden, false, _changedHandler);
349 _settings[KeyStartHidden] = setting;
351 return _settings[KeyStartHidden].AsSettingBool;
356 public bool ContainsKey(string key)
358 return _settings.ContainsKey(key);
361 public void SetVendorName(CecLogicalAddress address, CecVendorId vendorId, string vendorName)
363 VendorNames[(int)address] = vendorName;
365 if (address == CecLogicalAddress.Tv && vendorId == CecVendorId.Panasonic)
367 DeviceType.AllowedTypeMask = new CecDeviceTypeList {Types = new[] {CecDeviceType.PlaybackDevice}};
370 foreach (var setting in _settings)
372 if (setting.Value.SettingType == CECSettingType.LogicalAddress)
373 setting.Value.AsSettingLogicalAddress.ResetItems(true);
381 CECSetting[] settings = new CECSetting[_settings.Count + 10];
382 _settings.Values.CopyTo(settings, 0);
383 foreach (var setting in settings)
385 setting.Enabled = value;
390 foreach (var setting in _settings)
391 enabled &= setting.Value.Enabled;
397 /// Read or write one of the settings, given it's key
399 /// <param name="key">The key of the setting</param>
400 /// <returns>The setting</returns>
401 public CECSetting this[string key]
403 get { return _settings.ContainsKey(key) ? _settings[key] : null; }
404 set {_settings[key] = value; }
408 /// Create the registry key that holds all settings.
410 /// <returns>True when created (or already existing), false otherwise</returns>
411 private static bool CreateRegistryKey()
413 using (var regKey = Registry.CurrentUser.OpenSubKey("Software", true))
417 regKey.CreateSubKey(RegistryCompanyName);
425 using (var regKey = Registry.CurrentUser.OpenSubKey("Software\\" + RegistryCompanyName, true))
429 regKey.CreateSubKey(RegistryApplicationName);
440 private readonly Dictionary<string, CECSetting> _settings = new Dictionary<string, CECSetting>();
441 private static string RegistryKeyName
443 get { return string.Format("Software\\{0}\\{1}", RegistryCompanyName, RegistryApplicationName); }
446 private readonly CECSetting.SettingChangedHandler _changedHandler;
447 public event CECSetting.SettingChangedHandler SettingChanged;
449 public static readonly string[] VendorNames = new string[15];