added GetAdapterProductId()+GetAdapterVendorId()/cec_get_adapter_product_id()+cec_get...
[deb_libcec.git] / src / LibCecTray / controller / applications / internal / XBMCController.cs
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33 using System;
34 using System.Globalization;
35 using System.IO;
36 using System.Text;
37 using System.Windows.Forms;
38 using System.Xml;
39 using CecSharp;
40 using LibCECTray.Properties;
41 using LibCECTray.settings;
42
43 namespace LibCECTray.controller.applications.@internal
44 {
45 internal class XBMCController : ApplicationController
46 {
47 public XBMCController(CECSettings settings) :
48 base(settings,
49 Resources.application_xbmc,
50 "XBMC",
51 "XBMC.exe",
52 Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\XBMC")
53 {
54 IsInternal = true;
55 AutoStartApplication.Value = false;
56 ControlApplication.Value = false;
57
58 LoadXMLConfiguration();
59 }
60
61 public override ApplicationAction DefaultValue(CecKeypress key)
62 {
63 return null;
64 }
65
66 public override ControllerTabPage UiControl
67 {
68 get { return UIControlInternal ?? (UIControlInternal = new XBMCControllerUI(this)); }
69 }
70
71 public bool LoadXMLConfiguration()
72 {
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";
79
80 if (File.Exists(file))
81 {
82 XmlTextReader reader = new XmlTextReader(file);
83 while (reader.Read())
84 {
85 gotConfig = true;
86 switch (reader.NodeType)
87 {
88 case XmlNodeType.Element:
89 if (reader.Name.ToLower() == "setting")
90 {
91 string name = string.Empty;
92 string value = string.Empty;
93
94 while (reader.MoveToNextAttribute())
95 {
96 if (reader.Name.ToLower().Equals("id"))
97 name = reader.Value.ToLower();
98 if (reader.Name.ToLower().Equals("value"))
99 value = reader.Value;
100 }
101
102 switch (name)
103 {
104 case "cec_hdmi_port":
105 {
106 byte iPort;
107 if (byte.TryParse(value, out iPort))
108 Settings.HDMIPort.Value = iPort;
109 }
110 break;
111 case "connected_device":
112 {
113 ushort iDevice;
114 if (ushort.TryParse(value, out iDevice))
115 Settings.ConnectedDevice.Value = (CecLogicalAddress)iDevice;
116 }
117 break;
118 case "cec_power_on_startup":
119 if (value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes"))
120 {
121 Settings.ActivateSource.Value = true;
122 Settings.WakeDevices.Value.Set(CecLogicalAddress.Tv);
123 }
124 break;
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);
128 break;
129 case "cec_standby_screensaver":
130 StandbyScreensaver.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
131 break;
132 case "standby_pc_on_tv_standby":
133 PowerOffOnStandby.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
134 break;
135 case "use_tv_menu_language":
136 UseTVLanguage.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
137 break;
138 // 1.5.0+ settings
139 case "physical_address":
140 {
141 ushort physicalAddress;
142 if (ushort.TryParse(value, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
143 Settings.PhysicalAddress.Value = physicalAddress;
144 }
145 break;
146 case "device_type":
147 {
148 ushort iType;
149 if (ushort.TryParse(value, out iType))
150 Settings.DeviceType.Value = (CecDeviceType)iType;
151 }
152 break;
153 case "tv_vendor":
154 {
155 UInt64 iVendor;
156 if (UInt64.TryParse(value, out iVendor))
157 Settings.TVVendor.Value = (CecVendorId)iVendor;
158 }
159 break;
160 case "wake_devices":
161 {
162 Settings.WakeDevices.Value.Clear();
163 string[] split = value.Split(new[] { ' ' });
164 foreach (string dev in split)
165 {
166 byte iLogicalAddress;
167 if (byte.TryParse(dev, out iLogicalAddress))
168 Settings.WakeDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
169 }
170 }
171 break;
172 case "standby_devices":
173 {
174 Settings.PowerOffDevices.Value.Clear();
175 string[] split = value.Split(new[] { ' ' });
176 foreach (string dev in split)
177 {
178 byte iLogicalAddress;
179 if (byte.TryParse(dev, out iLogicalAddress))
180 Settings.PowerOffDevices.Value.Set((CecLogicalAddress)iLogicalAddress);
181 }
182 }
183 break;
184 case "enabled":
185 break;
186 case "port":
187 //TODO
188 break;
189 // 1.5.1 settings
190 case "send_inactive_source":
191 SendInactiveSource.Value = value.Equals("1") || value.ToLower().Equals("true") || value.ToLower().Equals("yes");
192 break;
193 }
194 }
195 break;
196 }
197 }
198 }
199 return gotConfig;
200 }
201
202 public void SaveXMLConfiguration()
203 {
204 Settings.Persist();
205
206 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
207 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
208
209 SaveFileDialog dialog = new SaveFileDialog
210 {
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,
215 FilterIndex = 1
216 };
217 if (dialog.ShowDialog() != DialogResult.OK) return;
218
219 FileStream fs = null;
220 string error = string.Empty;
221 try
222 {
223 fs = (FileStream)dialog.OpenFile();
224 }
225 catch (Exception ex)
226 {
227 error = ex.Message;
228 }
229 if (fs == null)
230 {
231 MessageBox.Show(string.Format(Resources.cannot_open_file, dialog.FileName) + (error.Length > 0 ? ": " + error : string.Empty), Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Error);
232 }
233 else
234 {
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=\"\" />");
247
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() + "\" />");
254
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("\" />");
262
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("\" />");
270
271 // only supported by 1.5.1+ clients
272 output.AppendLine("<!-- the following lines are only supported by v1.5.1+ clients -->");
273 //TODO
274 //output.AppendLine("<setting id=\"send_inactive_source\" value=\"" + (config.SendInactiveSource ? 1 : 0) + "\" />");
275
276 output.AppendLine("</settings>");
277 writer.Write(output.ToString());
278 writer.Close();
279 fs.Close();
280 fs.Dispose();
281 MessageBox.Show(Resources.settings_stored, Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
282 }
283 }
284
285 public CECSettingBool UseTVLanguage
286 {
287 get
288 {
289 if (!Settings.ContainsKey(ProcessName + "_use_tv_language"))
290 {
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;
294 }
295 return Settings[ProcessName + "_use_tv_language"].AsSettingBool;
296 }
297 }
298
299 public CECSettingBool StandbyScreensaver
300 {
301 get
302 {
303 if (!Settings.ContainsKey(ProcessName + "_standby_screensaver"))
304 {
305 CECSettingBool setting = new CECSettingBool(ProcessName + "_standby_screensaver", Resources.app_standby_screensaver, true, null);
306 Settings.Load(setting);
307 Settings[ProcessName + "_standby_screensaver"] = setting;
308 }
309 return Settings[ProcessName + "_standby_screensaver"].AsSettingBool;
310 }
311 }
312
313 public CECSettingBool ActivateSource
314 {
315 get
316 {
317 if (!Settings.ContainsKey(ProcessName + "_activate_source"))
318 {
319 CECSettingBool setting = new CECSettingBool(ProcessName + "_activate_source", Resources.global_activate_source, true, null);
320 Settings.Load(setting);
321 Settings[ProcessName + "_activate_source"] = setting;
322 }
323 return Settings[ProcessName + "_activate_source"].AsSettingBool;
324 }
325 }
326
327 public CECSettingBool PowerOffOnStandby
328 {
329 get
330 {
331 if (!Settings.ContainsKey(ProcessName + "_standby_on_tv_standby"))
332 {
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;
336 }
337 return Settings[ProcessName + "_standby_on_tv_standby"].AsSettingBool;
338 }
339 }
340
341 public CECSettingBool SendInactiveSource
342 {
343 get
344 {
345 if (!Settings.ContainsKey(ProcessName + "_send_inactive_source"))
346 {
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;
350 }
351 return Settings[ProcessName + "_send_inactive_source"].AsSettingBool;
352 }
353 }
354 }
355 }