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();
60 ApplicationRunningChanged += RunningChanged;
63 static void RunningChanged(bool running)
67 // XBMC is running, close the application, or we'll block communication
72 public override ApplicationAction DefaultValue(CecKeypress key)
77 public override ControllerTabPage UiControl
79 get { return UIControlInternal ?? (UIControlInternal = new XBMCControllerUI(this)); }
82 public bool LoadXMLConfiguration()
84 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
85 return LoadXMLConfiguration(xbmcDir + string.Format(@"\usb_{0:X}_{1:X}.xml", Program.Instance.Controller.AdapterVendorId, Program.Instance.Controller.AdapterProductId)) ||
86 LoadXMLConfiguration(xbmcDir + @"\usb_2548_1001.xml") ||
87 LoadXMLConfiguration(xbmcDir + @"\usb_2548_1002.xml");
90 public bool LoadXMLConfiguration(string filename)
92 bool gotConfig = false;
93 if (File.Exists(filename))
95 XmlTextReader reader = new XmlTextReader(filename);
99 switch (reader.NodeType)
101 case XmlNodeType.Element:
102 if (reader.Name.ToLower() == "setting")
104 string name = string.Empty;
105 string value = string.Empty;
107 while (reader.MoveToNextAttribute())
109 if (reader.Name.ToLower().Equals("id"))
110 name = reader.Value.ToLower();
111 if (reader.Name.ToLower().Equals("value"))
112 value = reader.Value;
117 case "cec_hdmi_port":
120 if (byte.TryParse(value, out iPort))
121 Settings.HDMIPort.Value = iPort;
124 case "connected_device":
127 if (ushort.TryParse(value, out iDevice))
128 Settings.ConnectedDevice.Value = (CecLogicalAddress)iDevice;
131 case "cec_power_on_startup":
132 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
134 Settings.ActivateSource.Value = true;
135 Settings.WakeDevices.Value.Set(CecLogicalAddress.Tv);
138 case "cec_power_off_shutdown":
139 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
140 Settings.PowerOffDevices.Value.Set(CecLogicalAddress.Broadcast);
142 case "cec_standby_screensaver":
143 StandbyScreensaver.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
145 case "standby_pc_on_tv_standby":
146 PowerOffOnStandby.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
148 case "use_tv_menu_language":
149 UseTVLanguage.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
152 case "physical_address":
154 ushort physicalAddress;
155 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
156 Settings.PhysicalAddress.Value = physicalAddress;
162 if (ushort.TryParse(value, out iType))
163 Settings.DeviceType.Value = (CecDeviceType)iType;
169 if (UInt64.TryParse(value, out iVendor))
170 Settings.TVVendor.Value = (CecVendorId)iVendor;
175 Settings.WakeDevices.Value.Clear();
176 string[] split = value.Split(new[] { ' ' });
177 foreach (string dev in split)
179 byte iLogicalAddress;
180 if (byte.TryParse(dev, out iLogicalAddress))
181 Settings.WakeDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
185 case "standby_devices":
187 Settings.PowerOffDevices.Value.Clear();
188 string[] split = value.Split(new[] { ' ' });
189 foreach (string dev in split)
191 byte iLogicalAddress;
192 if (byte.TryParse(dev, out iLogicalAddress))
193 Settings.PowerOffDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
203 case "send_inactive_source":
204 SendInactiveSource.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
215 public void SaveXMLConfiguration()
219 var xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
220 if (!Directory.Exists(xbmcDir))
221 Directory.CreateDirectory(xbmcDir);
223 if (!Directory.Exists(xbmcDir))
225 // couldn't create directory
226 MessageBox.Show(string.Format(Resources.could_not_create_directory, xbmcDir), Resources.error,
227 MessageBoxButtons.OK, MessageBoxIcon.Error);
231 SaveFileDialog dialog = new SaveFileDialog
233 Title = Resources.store_settings_where,
234 InitialDirectory = xbmcDir,
235 FileName = string.Format("usb_{0:X}_{1:X}.xml", Program.Instance.Controller.AdapterVendorId, Program.Instance.Controller.AdapterProductId),
236 Filter = Resources.xml_file_filter,
239 if (dialog.ShowDialog() != DialogResult.OK) return;
241 FileStream fs = null;
242 string error = string.Empty;
245 fs = (FileStream)dialog.OpenFile();
253 MessageBox.Show(string.Format(Resources.cannot_open_file, dialog.FileName) + (error.Length > 0 ? ": " + error : string.Empty), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
257 StreamWriter writer = new StreamWriter(fs);
258 StringBuilder output = new StringBuilder();
259 output.AppendLine("<settings>");
260 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Settings.HDMIPort.Value + "\" />");
261 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Settings.ConnectedDevice.Value == CecLogicalAddress.AudioSystem ? 5 : 0) + "\" />");
262 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
263 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Settings.PowerOffDevices.Value.IsSet(CecLogicalAddress.Broadcast) ? 1 : 0) + "\" />");
264 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (StandbyScreensaver.Value ? 1 : 0) + "\" />");
265 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (PowerOffOnStandby.Value ? 1 : 0) + "\" />");
266 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (UseTVLanguage.Value ? 1 : 0) + "\" />");
267 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
268 output.AppendLine("<setting id=\"port\" value=\"\" />");
270 // only supported by 1.5.0+ clients
271 output.AppendLine("<!-- the following lines are only supported by v1.5.0+ clients -->");
272 output.AppendLine("<setting id=\"activate_source\" value=\"" + (Settings.ActivateSource.Value ? 1 : 0) + "\" />");
273 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Settings.OverridePhysicalAddress.Value ? Settings.PhysicalAddress.Value : 0).Trim() + "\" />");
274 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Settings.DeviceType.Value + "\" />");
275 output.AppendLine("<setting id=\"tv_vendor\" value=\"" + string.Format("{0,6:X}", Settings.OverrideTVVendor.Value ? (int)Settings.TVVendor.Value : 0).Trim() + "\" />");
277 output.Append("<setting id=\"wake_devices\" value=\"");
278 StringBuilder strWakeDevices = new StringBuilder();
279 foreach (CecLogicalAddress addr in Settings.WakeDevices.Value.Addresses)
280 if (addr != CecLogicalAddress.Unknown)
281 strWakeDevices.Append(" " + (int)addr);
282 output.Append(strWakeDevices.ToString().Trim());
283 output.AppendLine("\" />");
285 output.Append("<setting id=\"standby_devices\" value=\"");
286 StringBuilder strSleepDevices = new StringBuilder();
287 foreach (CecLogicalAddress addr in Settings.PowerOffDevices.Value.Addresses)
288 if (addr != CecLogicalAddress.Unknown)
289 strSleepDevices.Append(" " + (int)addr);
290 output.Append(strSleepDevices.ToString().Trim());
291 output.AppendLine("\" />");
293 // only supported by 1.5.1+ clients
294 output.AppendLine("<!-- the following lines are only supported by v1.5.1+ clients -->");
295 output.AppendLine("<setting id=\"send_inactive_source\" value=\"" + (SendInactiveSource.Value ? 1 : 0) + "\" />");
297 output.AppendLine("</settings>");
298 writer.Write(output.ToString());
302 MessageBox.Show(Resources.settings_stored, Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
306 public CECSettingBool UseTVLanguage
310 if (!Settings.ContainsKey(ProcessName + "_use_tv_language"))
312 CECSettingBool setting = new CECSettingBool(ProcessName + "_use_tv_language", Resources.app_use_tv_language, true, null);
313 Settings.Load(setting);
314 Settings[ProcessName + "_use_tv_language"] = setting;
316 return Settings[ProcessName + "_use_tv_language"].AsSettingBool;
320 public CECSettingBool StandbyScreensaver
324 if (!Settings.ContainsKey(ProcessName + "_standby_screensaver"))
326 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_screensaver", Resources.app_standby_screensaver, true, null);
327 Settings.Load(setting);
328 Settings[ProcessName + "_standby_screensaver"] = setting;
330 return Settings[ProcessName + "_standby_screensaver"].AsSettingBool;
334 public CECSettingBool ActivateSource
338 if (!Settings.ContainsKey(ProcessName + "_activate_source"))
340 CECSettingBool setting = new CECSettingBool(ProcessName + "_activate_source", Resources.global_activate_source, true, null);
341 Settings.Load(setting);
342 Settings[ProcessName + "_activate_source"] = setting;
344 return Settings[ProcessName + "_activate_source"].AsSettingBool;
348 public CECSettingBool PowerOffOnStandby
352 if (!Settings.ContainsKey(ProcessName + "_standby_on_tv_standby"))
354 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_on_tv_standby", Resources.app_standby_on_tv_standby, true, null);
355 Settings.Load(setting);
356 Settings[ProcessName + "_standby_on_tv_standby"] = setting;
358 return Settings[ProcessName + "_standby_on_tv_standby"].AsSettingBool;
362 public CECSettingBool SendInactiveSource
366 if (!Settings.ContainsKey(ProcessName + "_send_inactive_source"))
368 CECSettingBool setting = new CECSettingBool(ProcessName + "_send_inactive_source", Resources.app_send_inactive_source, true, null);
369 Settings.Load(setting);
370 Settings[ProcessName + "_send_inactive_source"] = setting;
372 return Settings[ProcessName + "_send_inactive_source"].AsSettingBool;