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 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
74 return LoadXMLConfiguration(xbmcDir + @"\usb_2548_1001.xml") || LoadXMLConfiguration(xbmcDir + @"\usb_2548_1002.xml");
77 public bool LoadXMLConfiguration(string filename)
79 bool gotConfig = false;
80 if (File.Exists(filename))
82 XmlTextReader reader = new XmlTextReader(filename);
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 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
207 if (!Directory.Exists(xbmcDir))
208 Directory.CreateDirectory(xbmcDir);
210 if (!Directory.Exists(xbmcDir))
212 // couldn't create directory
213 MessageBox.Show(string.Format(Resources.could_not_create_directory, xbmcDir), Resources.error,
214 MessageBoxButtons.OK, MessageBoxIcon.Error);
218 SaveFileDialog dialog = new SaveFileDialog
220 Title = Resources.store_settings_where,
221 InitialDirectory = xbmcDir,
222 FileName = string.Format("usb_{0}.xml", Program.Instance.Controller.USBDescriptor.Replace(':', '_')),
223 Filter = Resources.xml_file_filter,
226 if (dialog.ShowDialog() != DialogResult.OK) return;
228 FileStream fs = null;
229 string error = string.Empty;
232 fs = (FileStream)dialog.OpenFile();
240 MessageBox.Show(string.Format(Resources.cannot_open_file, dialog.FileName) + (error.Length > 0 ? ": " + error : string.Empty), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
244 StreamWriter writer = new StreamWriter(fs);
245 StringBuilder output = new StringBuilder();
246 output.AppendLine("<settings>");
247 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Settings.HDMIPort.Value + "\" />");
248 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Settings.ConnectedDevice.Value == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />");
249 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
250 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Settings.PowerOffDevices.Value.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
251 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (StandbyScreensaver.Value ? 1 : 0) + "\" />");
252 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (PowerOffOnStandby.Value ? 1 : 0) + "\" />");
253 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (UseTVLanguage.Value ? 1 : 0) + "\" />");
254 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
255 output.AppendLine("<setting id=\"port\" value=\"\" />");
257 // only supported by 1.5.0+ clients
258 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
259 output.AppendLine("<setting id=\"activate_source\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
260 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Settings.OverridePhysicalAddress.Value ? Settings.PhysicalAddress.Value : 0).Trim() + "\" />");
261 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Settings.DeviceType.Value + "\" />");
262 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", Settings.OverrideTVVendor.Value ? (int)Settings.TVVendor.Value : 0).Trim() + "\" />");
264 output.Append("<setting id=\"wake_devices\" value=\"");
265 StringBuilder strWakeDevices = new StringBuilder();
266 foreach (CecLogicalAddress addr in Settings.WakeDevices.Value.Addresses)
267 if (addr != CecLogicalAddress.Unknown)
268 strWakeDevices.Append(" " + (int)addr);
269 output.Append(strWakeDevices.ToString().Trim());
270 output.AppendLine("\" />");
272 output.Append("<setting id=\"standby_devices\" value=\"");
273 StringBuilder strSleepDevices = new StringBuilder();
274 foreach (CecLogicalAddress addr in Settings.PowerOffDevices.Value.Addresses)
275 if (addr != CecLogicalAddress.Unknown)
276 strSleepDevices.Append(" " + (int)addr);
277 output.Append(strSleepDevices.ToString().Trim());
278 output.AppendLine("\" />");
280 // only supported by 1.5.1+ clients
281 output.AppendLine("<!-- the following lines are only supported by v1.5.1+ clients -->");
283 //output.AppendLine("<setting id=\"send_inactive_source\" value=\"" + (config.SendInactiveSource ? 1 : 0) + "\" />");
285 output.AppendLine("</settings>");
286 writer.Write(output.ToString());
290 MessageBox.Show(Resources.settings_stored, Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
294 public CECSettingBool UseTVLanguage
298 if (!Settings.ContainsKey(ProcessName + "_use_tv_language"))
300 CECSettingBool setting = new CECSettingBool(ProcessName + "_use_tv_language", Resources.app_use_tv_language, true, null);
301 Settings.Load(setting);
302 Settings[ProcessName + "_use_tv_language"] = setting;
304 return Settings[ProcessName + "_use_tv_language"].AsSettingBool;
308 public CECSettingBool StandbyScreensaver
312 if (!Settings.ContainsKey(ProcessName + "_standby_screensaver"))
314 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_screensaver", Resources.app_standby_screensaver, true, null);
315 Settings.Load(setting);
316 Settings[ProcessName + "_standby_screensaver"] = setting;
318 return Settings[ProcessName + "_standby_screensaver"].AsSettingBool;
322 public CECSettingBool ActivateSource
326 if (!Settings.ContainsKey(ProcessName + "_activate_source"))
328 CECSettingBool setting = new CECSettingBool(ProcessName + "_activate_source", Resources.global_activate_source, true, null);
329 Settings.Load(setting);
330 Settings[ProcessName + "_activate_source"] = setting;
332 return Settings[ProcessName + "_activate_source"].AsSettingBool;
336 public CECSettingBool PowerOffOnStandby
340 if (!Settings.ContainsKey(ProcessName + "_standby_on_tv_standby"))
342 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_on_tv_standby", Resources.app_standby_on_tv_standby, true, null);
343 Settings.Load(setting);
344 Settings[ProcessName + "_standby_on_tv_standby"] = setting;
346 return Settings[ProcessName + "_standby_on_tv_standby"].AsSettingBool;
350 public CECSettingBool SendInactiveSource
354 if (!Settings.ContainsKey(ProcessName + "_send_inactive_source"))
356 CECSettingBool setting = new CECSettingBool(ProcessName + "_send_inactive_source", Resources.app_send_inactive_source, true, null);
357 Settings.Load(setting);
358 Settings[ProcessName + "_send_inactive_source"] = setting;
360 return Settings[ProcessName + "_send_inactive_source"].AsSettingBool;