2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
34 using System.Globalization;
37 using System.Windows.Forms;
40 using LibCECTray.Properties;
41 using LibCECTray.settings;
43 namespace LibCECTray.controller.applications.@internal
45 internal class XBMCController : ApplicationController
47 public XBMCController(CECSettings settings) :
49 Resources.application_xbmc,
52 Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\XBMC")
55 AutoStartApplication.Value = false;
56 ControlApplication.Value = false;
58 LoadXMLConfiguration();
61 public override ApplicationAction DefaultValue(CecKeypress key)
66 public override ControllerTabPage UiControl
68 get { return UIControlInternal ?? (UIControlInternal = new XBMCControllerUI(this)); }
71 public bool LoadXMLConfiguration()
73 bool gotConfig = false;
74 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
75 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
76 string file = defaultDir + @"\usb_2548_1001.xml";
77 if (File.Exists(xbmcDir + @"\usb_2548_1001.xml"))
78 file = xbmcDir + @"\usb_2548_1001.xml";
80 if (File.Exists(file))
82 XmlTextReader reader = new XmlTextReader(file);
86 switch (reader.NodeType)
88 case XmlNodeType.Element:
89 if (reader.Name.ToLower() == "setting")
91 string name = string.Empty;
92 string value = string.Empty;
94 while (reader.MoveToNextAttribute())
96 if (reader.Name.ToLower().Equals("id"))
97 name = reader.Value.ToLower();
98 if (reader.Name.ToLower().Equals("value"))
104 case "cec_hdmi_port":
107 if (byte.TryParse(value, out iPort))
108 Settings.HDMIPort.Value = iPort;
111 case "connected_device":
114 if (ushort.TryParse(value, out iDevice))
115 Settings.ConnectedDevice.Value = (CecLogicalAddress)iDevice;
118 case "cec_power_on_startup":
119 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
121 Settings.ActivateSource.Value = true;
122 Settings.WakeDevices.Value.Set(CecLogicalAddress.Tv);
125 case "cec_power_off_shutdown":
126 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
127 Settings.PowerOffDevices.Value.Set(CecLogicalAddress.Broadcast);
129 case "cec_standby_screensaver":
130 StandbyScreensaver.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
132 case "standby_pc_on_tv_standby":
133 PowerOffOnStandby.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
135 case "use_tv_menu_language":
136 UseTVLanguage.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
139 case "physical_address":
141 ushort physicalAddress;
142 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
143 Settings.PhysicalAddress.Value = physicalAddress;
149 if (ushort.TryParse(value, out iType))
150 Settings.DeviceType.Value = (CecDeviceType)iType;
156 if (UInt64.TryParse(value, out iVendor))
157 Settings.TVVendor.Value = (CecVendorId)iVendor;
162 Settings.WakeDevices.Value.Clear();
163 string[] split = value.Split(new[] { ' ' });
164 foreach (string dev in split)
166 byte iLogicalAddress;
167 if (byte.TryParse(dev, out iLogicalAddress))
168 Settings.WakeDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
172 case "standby_devices":
174 Settings.PowerOffDevices.Value.Clear();
175 string[] split = value.Split(new[] { ' ' });
176 foreach (string dev in split)
178 byte iLogicalAddress;
179 if (byte.TryParse(dev, out iLogicalAddress))
180 Settings.PowerOffDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
190 case "send_inactive_source":
191 SendInactiveSource.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
202 public void SaveXMLConfiguration()
206 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
207 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
209 SaveFileDialog dialog = new SaveFileDialog
211 Title = Resources.store_settings_where,
212 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
213 FileName = "usb_2548_1001.xml",
214 Filter = Resources.xml_file_filter,
217 if (dialog.ShowDialog() != DialogResult.OK) return;
219 FileStream fs = null;
220 string error = string.Empty;
223 fs = (FileStream)dialog.OpenFile();
231 MessageBox.Show(string.Format(Resources.cannot_open_file, dialog.FileName) + (error.Length > 0 ? ": " + error : string.Empty), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
235 StreamWriter writer = new StreamWriter(fs);
236 StringBuilder output = new StringBuilder();
237 output.AppendLine("<settings>");
238 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Settings.HDMIPort.Value + "\" />");
239 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Settings.ConnectedDevice.Value == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />");
240 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
241 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Settings.PowerOffDevices.Value.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
242 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (StandbyScreensaver.Value ? 1 : 0) + "\" />");
243 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (PowerOffOnStandby.Value ? 1 : 0) + "\" />");
244 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (UseTVLanguage.Value ? 1 : 0) + "\" />");
245 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
246 output.AppendLine("<setting id=\"port\" value=\"\" />");
248 // only supported by 1.5.0+ clients
249 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
250 output.AppendLine("<setting id=\"activate_source\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
251 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Settings.OverridePhysicalAddress.Value ? Settings.PhysicalAddress.Value : 0).Trim() + "\" />");
252 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Settings.DeviceType.Value + "\" />");
253 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", Settings.OverrideTVVendor.Value ? (int)Settings.TVVendor.Value : 0).Trim() + "\" />");
255 output.Append("<setting id=\"wake_devices\" value=\"");
256 StringBuilder strWakeDevices = new StringBuilder();
257 foreach (CecLogicalAddress addr in Settings.WakeDevices.Value.Addresses)
258 if (addr != CecLogicalAddress.Unknown)
259 strWakeDevices.Append(" " + (int)addr);
260 output.Append(strWakeDevices.ToString().Trim());
261 output.AppendLine("\" />");
263 output.Append("<setting id=\"standby_devices\" value=\"");
264 StringBuilder strSleepDevices = new StringBuilder();
265 foreach (CecLogicalAddress addr in Settings.PowerOffDevices.Value.Addresses)
266 if (addr != CecLogicalAddress.Unknown)
267 strSleepDevices.Append(" " + (int)addr);
268 output.Append(strSleepDevices.ToString().Trim());
269 output.AppendLine("\" />");
271 // only supported by 1.5.1+ clients
272 output.AppendLine("<!-- the following lines are only supported by v1.5.1+ clients -->");
274 //output.AppendLine("<setting id=\"send_inactive_source\" value=\"" + (config.SendInactiveSource ? 1 : 0) + "\" />");
276 output.AppendLine("</settings>");
277 writer.Write(output.ToString());
281 MessageBox.Show(Resources.settings_stored, Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
285 public CECSettingBool UseTVLanguage
289 if (!Settings.ContainsKey(ProcessName + "_use_tv_language"))
291 CECSettingBool setting = new CECSettingBool(ProcessName + "_use_tv_language", Resources.app_use_tv_language, true, null);
292 Settings.Load(setting);
293 Settings[ProcessName + "_use_tv_language"] = setting;
295 return Settings[ProcessName + "_use_tv_language"].AsSettingBool;
299 public CECSettingBool StandbyScreensaver
303 if (!Settings.ContainsKey(ProcessName + "_standby_screensaver"))
305 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_screensaver", Resources.app_standby_screensaver, true, null);
306 Settings.Load(setting);
307 Settings[ProcessName + "_standby_screensaver"] = setting;
309 return Settings[ProcessName + "_standby_screensaver"].AsSettingBool;
313 public CECSettingBool ActivateSource
317 if (!Settings.ContainsKey(ProcessName + "_activate_source"))
319 CECSettingBool setting = new CECSettingBool(ProcessName + "_activate_source", Resources.global_activate_source, true, null);
320 Settings.Load(setting);
321 Settings[ProcessName + "_activate_source"] = setting;
323 return Settings[ProcessName + "_activate_source"].AsSettingBool;
327 public CECSettingBool PowerOffOnStandby
331 if (!Settings.ContainsKey(ProcessName + "_standby_on_tv_standby"))
333 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_on_tv_standby", Resources.app_standby_on_tv_standby, true, null);
334 Settings.Load(setting);
335 Settings[ProcessName + "_standby_on_tv_standby"] = setting;
337 return Settings[ProcessName + "_standby_on_tv_standby"].AsSettingBool;
341 public CECSettingBool SendInactiveSource
345 if (!Settings.ContainsKey(ProcessName + "_send_inactive_source"))
347 CECSettingBool setting = new CECSettingBool(ProcessName + "_send_inactive_source", Resources.app_send_inactive_source, true, null);
348 Settings.Load(setting);
349 Settings[ProcessName + "_send_inactive_source"] = setting;
351 return Settings[ProcessName + "_send_inactive_source"].AsSettingBool;