cec: added a .net CEC configuration tool
[deb_libcec.git] / src / cec-config-gui / CecConfigGUI.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Threading;
9 using CecSharp;
10 using CecConfigGui.actions;
11 using System.Globalization;
12 using System.IO;
13
14 namespace CecConfigGui
15 {
16 public partial class CecConfigGUI : Form
17 {
18 public CecConfigGUI()
19 {
20 Config = new LibCECConfiguration();
21 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
22 Config.DeviceName = "CEC Config";
23 Config.GetSettingsFromROM = true;
24 Config.ClientVersion = CecClientVersion.Version1_5_0;
25 Callbacks = new CecCallbackWrapper(this);
26 Config.SetCallbacks(Callbacks);
27
28 InitializeComponent();
29 Lib = new LibCecSharp(Config);
30
31 ActiveProcess = new ConnectToDevice(ref Lib);
32 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
33 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
34 }
35
36 public int ReceiveCommand(CecCommand command)
37 {
38 return 1;
39 }
40
41 public int ReceiveKeypress(CecKeypress key)
42 {
43 return 1;
44 }
45
46 delegate void AddLogMessageCallback(CecLogMessage message);
47 private void AddLogMessage(CecLogMessage message)
48 {
49 if (tbLog.InvokeRequired)
50 {
51 AddLogMessageCallback d = new AddLogMessageCallback(AddLogMessage);
52 try
53 {
54 this.Invoke(d, new object[] { message });
55 }
56 catch (Exception) { }
57 }
58 else
59 {
60 if (((int)message.Level & LogLevel) == (int)message.Level)
61 {
62 string strLevel = "";
63 switch (message.Level)
64 {
65 case CecLogLevel.Error:
66 strLevel = "ERROR: ";
67 break;
68 case CecLogLevel.Warning:
69 strLevel = "WARNING: ";
70 break;
71 case CecLogLevel.Notice:
72 strLevel = "NOTICE: ";
73 break;
74 case CecLogLevel.Traffic:
75 strLevel = "TRAFFIC: ";
76 break;
77 case CecLogLevel.Debug:
78 strLevel = "DEBUG: ";
79 break;
80 default:
81 break;
82 }
83 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message) + System.Environment.NewLine;
84 tbLog.Text += strLog;
85 }
86 }
87 }
88
89 public int ReceiveLogMessage(CecLogMessage message)
90 {
91 AddLogMessage(message);
92 return 1;
93 }
94
95 delegate void SetControlEnabledCallback(Control control, bool val);
96 private void SetControlEnabled(Control control, bool val)
97 {
98 if (control.InvokeRequired)
99 {
100 SetControlEnabledCallback d = new SetControlEnabledCallback(SetControlEnabled);
101 try
102 {
103 this.Invoke(d, new object[] { control, val });
104 }
105 catch (Exception) { }
106 }
107 else
108 {
109 control.Enabled = val;
110 }
111 }
112
113 private void SetControlsEnabled(bool val)
114 {
115 SetControlEnabled(cbPortNumber, val);
116 SetControlEnabled(cbConnectedDevice, val);
117 SetControlEnabled(tbPhysicalAddress, val);
118 SetControlEnabled(cbDeviceType, val);
119 SetControlEnabled(cbUseTVMenuLanguage, val);
120 SetControlEnabled(cbPowerOnStartup, val);
121 SetControlEnabled(cbPowerOffShutdown, val);
122 SetControlEnabled(cbPowerOffScreensaver, val);
123 SetControlEnabled(cbPowerOffOnStandby, val);
124 SetControlEnabled(bClose, val);
125 SetControlEnabled(bSave, val);
126 }
127
128 delegate void SetControlTextCallback(Control control, string val);
129 private void SetControlText(Control control, string val)
130 {
131 if (control.InvokeRequired)
132 {
133 SetControlTextCallback d = new SetControlTextCallback(SetControlText);
134 try
135 {
136 this.Invoke(d, new object[] { control, val });
137 }
138 catch (Exception) { }
139 }
140 else
141 {
142 control.Text = val;
143 }
144 }
145
146 delegate void SetCheckboxCheckedCallback(CheckBox control, bool val);
147 private void SetCheckboxChecked(CheckBox control, bool val)
148 {
149 if (control.InvokeRequired)
150 {
151 SetCheckboxCheckedCallback d = new SetCheckboxCheckedCallback(SetCheckboxChecked);
152 try
153 {
154 this.Invoke(d, new object[] { control, val });
155 }
156 catch (Exception) { }
157 }
158 else
159 {
160 control.Checked = val;
161 }
162 }
163
164 delegate void SetProgressValueCallback(ProgressBar control, int val);
165 private void SetProgressValue(ProgressBar control, int val)
166 {
167 if (control.InvokeRequired)
168 {
169 SetProgressValueCallback d = new SetProgressValueCallback(SetProgressValue);
170 try
171 {
172 this.Invoke(d, new object[] { control, val });
173 }
174 catch (Exception) { }
175 }
176 else
177 {
178 control.Value = val;
179 }
180 }
181
182 delegate void SetComboBoxItemsCallback(ComboBox control, string selectedText, object[] val);
183 private void SetComboBoxItems(ComboBox control, string selectedText, object[] val)
184 {
185 if (control.InvokeRequired)
186 {
187 SetComboBoxItemsCallback d = new SetComboBoxItemsCallback(SetComboBoxItems);
188 try
189 {
190 this.Invoke(d, new object[] { control, selectedText, val });
191 }
192 catch (Exception) { }
193 }
194 else
195 {
196 control.Items.Clear();
197 control.Items.AddRange(val);
198 control.Text = selectedText;
199 }
200 }
201
202 private void ProcessEventHandler(object src, UpdateEvent updateEvent)
203 {
204 switch (updateEvent.Type)
205 {
206 case UpdateEventType.StatusText:
207 SetControlText(lStatus, updateEvent.StringValue);
208 break;
209 case UpdateEventType.PhysicalAddress:
210 Config.PhysicalAddress = (ushort)updateEvent.IntValue;
211 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", updateEvent.IntValue));
212 break;
213 case UpdateEventType.ProgressBar:
214 SetProgressValue(pProgress, updateEvent.IntValue);
215 break;
216 case UpdateEventType.TVVendorId:
217 TVVendor = (CecVendorId)updateEvent.IntValue;
218 UpdateSelectedDevice();
219 break;
220 case UpdateEventType.BaseDevicePhysicalAddress:
221 SetControlText(lConnectedPhysicalAddress, string.Format("Address: {0,4:X}", updateEvent.IntValue));
222 break;
223 case UpdateEventType.BaseDevice:
224 Config.BaseDevice = (CecLogicalAddress)updateEvent.IntValue;
225 break;
226 case UpdateEventType.HDMIPort:
227 Config.HDMIPort = (byte)updateEvent.IntValue;
228 break;
229 case UpdateEventType.HasAVRDevice:
230 if (HasAVRDevice != updateEvent.BoolValue)
231 {
232 HasAVRDevice = updateEvent.BoolValue;
233 UpdateSelectedDevice();
234 }
235 break;
236 case UpdateEventType.AVRVendorId:
237 AVRVendor = (CecVendorId)updateEvent.IntValue;
238 UpdateSelectedDevice();
239 break;
240 case UpdateEventType.Configuration:
241 Config = updateEvent.ConfigValue;
242 SetControlText(tbPhysicalAddress, string.Format("{0,4:X}", Config.PhysicalAddress));
243 SetControlText(cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString);
244 SetControlText(cbPortNumber, Config.HDMIPort.ToString());
245 SetCheckboxChecked(cbUseTVMenuLanguage, Config.UseTVMenuLanguage);
246 SetCheckboxChecked(cbPowerOnStartup, Config.PowerOnStartup);
247 SetCheckboxChecked(cbPowerOffShutdown, Config.PowerOffShutdown);
248 SetCheckboxChecked(cbPowerOffScreensaver, Config.PowerOffScreensaver);
249 SetCheckboxChecked(cbPowerOffOnStandby, Config.PowerOffOnStandby);
250 UpdateSelectedDevice();
251 break;
252 case UpdateEventType.ProcessCompleted:
253 ActiveProcess = null;
254 SetControlsEnabled(true);
255 break;
256 }
257 }
258
259 private void UpdateSelectedDevice()
260 {
261 if (HasAVRDevice)
262 SetComboBoxItems(this.cbConnectedDevice, Config.BaseDevice == CecLogicalAddress.AudioSystem ? AVRVendorString : TVVendorString, new object[] { TVVendorString, AVRVendorString });
263 else
264 SetComboBoxItems(this.cbConnectedDevice, TVVendorString, new object[] { TVVendorString });
265 }
266
267 public string TVVendorString
268 {
269 get
270 {
271 return TVVendor != CecVendorId.Unknown ?
272 "Television (" + Lib.ToString(TVVendor) + ")" :
273 "Television";
274 }
275 }
276
277 public string AVRVendorString
278 {
279 get
280 {
281 return AVRVendor != CecVendorId.Unknown ?
282 "AVR (" + Lib.ToString(AVRVendor) + ")" :
283 "AVR";
284 }
285 }
286
287 protected bool HasAVRDevice;
288 protected CecVendorId TVVendor = CecVendorId.Unknown;
289 protected CecVendorId AVRVendor = CecVendorId.Unknown;
290 protected CecLogicalAddress SelectedConnectedDevice = CecLogicalAddress.Unknown;
291 protected int LogLevel = (int)CecLogLevel.All;
292
293 protected LibCECConfiguration Config;
294 protected LibCecSharp Lib;
295 private CecCallbackWrapper Callbacks;
296 private UpdateProcess ActiveProcess = null;
297
298 private void connectedDevice_SelectedIndexChanged(object sender, EventArgs e)
299 {
300 if (ActiveProcess == null)
301 {
302 SetControlsEnabled(false);
303 SelectedConnectedDevice = (this.cbConnectedDevice.Text.Equals(AVRVendorString)) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv;
304 int iPortNumber = 0;
305 if (!int.TryParse(cbPortNumber.Text, out iPortNumber))
306 iPortNumber = 1;
307 ActiveProcess = new UpdateConnectedDevice(ref Lib, cbConnectedDevice.Text.Equals(AVRVendorString) ? CecLogicalAddress.AudioSystem : CecLogicalAddress.Tv, iPortNumber);
308 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
309 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
310 }
311 }
312
313 private void bCancel_Click(object sender, EventArgs e)
314 {
315 this.Dispose();
316 }
317
318 private void bSave_Click(object sender, EventArgs e)
319 {
320 SetControlsEnabled(false);
321
322 Config.UseTVMenuLanguage = cbUseTVMenuLanguage.Checked;
323 Config.PowerOnStartup = cbPowerOnStartup.Checked;
324 Config.PowerOffShutdown = cbPowerOffShutdown.Checked;
325 Config.PowerOffScreensaver = cbPowerOffScreensaver.Checked;
326 Config.PowerOffOnStandby = cbPowerOffOnStandby.Checked;
327
328 if (!Lib.CanPersistConfiguration())
329 {
330 if (ActiveProcess == null)
331 {
332 SetControlsEnabled(false);
333 string xbmcDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\XBMC\userdata\peripheral_data";
334 string defaultDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
335
336 SaveFileDialog dialog = new SaveFileDialog()
337 {
338 Title = "Where do you want to store the settings?",
339 InitialDirectory = Directory.Exists(xbmcDir) ? xbmcDir : defaultDir,
340 FileName = "usb_2548_1001.xml",
341 Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*",
342 FilterIndex = 1
343 };
344
345 if (dialog.ShowDialog() == DialogResult.OK)
346 {
347 FileStream fs = (FileStream)dialog.OpenFile();
348 if (fs == null)
349 {
350 MessageBox.Show("Cannot open '" + dialog.FileName + "' for writing", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
351 }
352 else
353 {
354 StreamWriter writer = new StreamWriter(fs);
355 StringBuilder output = new StringBuilder();
356 output.AppendLine("<settings>");
357 output.AppendLine("<setting id=\"cec_hdmi_port\" value=\"" + Config.HDMIPort + "\" />");
358 output.AppendLine("<setting id=\"connected_device\" value=\"" + (Config.BaseDevice == CecLogicalAddress.AudioSystem ? 5 : 1) + "\" />");
359 output.AppendLine("<setting id=\"physical_address\" value=\"" + string.Format("{0,4:X}", Config.PhysicalAddress) + "\" />");
360 output.AppendLine("<setting id=\"device_type\" value=\"" + (int)Config.DeviceTypes.Types[0] + "\" />");
361 output.AppendLine("<setting id=\"cec_power_on_startup\" value=\"" + (Config.PowerOnStartup ? 1 : 0) + "\" />");
362 output.AppendLine("<setting id=\"cec_power_off_shutdown\" value=\"" + (Config.PowerOffShutdown ? 1 : 0) + "\" />");
363 output.AppendLine("<setting id=\"cec_standby_screensaver\" value=\"" + (Config.PowerOffScreensaver ? 1 : 0) + "\" />");
364 output.AppendLine("<setting id=\"standby_pc_on_tv_standby\" value=\"" + (Config.PowerOffOnStandby ? 1 : 0) + "\" />");
365 output.AppendLine("<setting id=\"use_tv_menu_language\" value=\"" + (Config.UseTVMenuLanguage ? 1 : 0) + "\" />");
366 output.AppendLine("<setting id=\"enabled\" value=\"1\" />");
367 output.AppendLine("<setting id=\"port\" value=\"\" />");
368 output.AppendLine("</settings>");
369 writer.Write(output.ToString());
370 writer.Close();
371 fs.Close();
372 fs.Dispose();
373 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
374 }
375 }
376 SetControlsEnabled(true);
377 }
378 }
379 else
380 {
381 if (!Lib.PersistConfiguration(Config))
382 MessageBox.Show("Could not persist the new settings.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error);
383 else
384 MessageBox.Show("Settings are stored.", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.OK, MessageBoxIcon.Information);
385 }
386 SetControlsEnabled(true);
387 }
388
389 private void tbPhysicalAddress_TextChanged(object sender, EventArgs e)
390 {
391 if (ActiveProcess == null)
392 {
393 if (tbPhysicalAddress.Text.Length != 4)
394 return;
395 ushort physicalAddress = 0;
396 if (!ushort.TryParse(tbPhysicalAddress.Text, NumberStyles.AllowHexSpecifier, null, out physicalAddress))
397 return;
398 SetControlsEnabled(false);
399 SetControlText(cbPortNumber, string.Empty);
400 SetControlText(cbConnectedDevice, string.Empty);
401 ActiveProcess = new UpdatePhysicalAddress(ref Lib, physicalAddress);
402 ActiveProcess.EventHandler += new EventHandler<UpdateEvent>(ProcessEventHandler);
403 (new Thread(new ThreadStart(ActiveProcess.Run))).Start();
404 }
405 }
406 }
407
408 internal class CecCallbackWrapper : CecCallbackMethods
409 {
410 public CecCallbackWrapper(CecConfigGUI gui)
411 {
412 Gui = gui;
413 }
414
415 public override int ReceiveCommand(CecCommand command)
416 {
417 return Gui.ReceiveCommand(command);
418 }
419
420 public override int ReceiveKeypress(CecKeypress key)
421 {
422 return Gui.ReceiveKeypress(key);
423 }
424
425 public override int ReceiveLogMessage(CecLogMessage message)
426 {
427 return Gui.ReceiveLogMessage(message);
428 }
429
430 private CecConfigGUI Gui;
431 }
432 }